HDU2852 KiKi's K-Number(值域线段树,求线段树第k大)

Problem Description

For the k-th number, we all should be very familiar with it. Of
course,to kiki it is also simple. Now Kiki meets a very similar
problem, kiki wants to design a container, the container is to support
the three operations.

Push: Push a given element e to container

Pop: Pop element of a given e from container

Query: Given two elements a and k, query the kth larger number which
greater than a in container;

Although Kiki is very intelligent, she can not think of how to do it,
can you help her to solve this problem?

Input

Input some groups of test data ,each test data the first number is an
integer m (1 <= m <100000), means that the number of operation to do.
The next m lines, each line will be an integer p at the beginning, p
which has three values: If p is 0, then there will be an integer e (0

Output

For each deletion, if you want to delete the element which does not
exist, the output “No Elment!”. For each query, output the suitable
answers in line .if the number does not exist, the output “Not Find!”.

Sample Input

5
0 5
1 2
0 6
2 3 2
2 8 1
7
0 2
0 2
0 4
2 1 1
2 1 2
2 1 3
2 1 4

Sample Output

No Elment!
6
Not Find!
2
2
4
Not Find!

思路

题目让你实现一种容器支持3个操作:

  1. 0 x:把x插入容器中
  2. 1 x:把x从容器中删除,如果有相同的多个,则只删除一个,如果容器中没有要删除的元素,就输出No Elment!
  3. 2 x k:求这个容器中大于x这个数的第k个元素的值\

我们可以开一个线段树来维护,插入和删除就相当于在线段树的对应位置上给权值+1或者-1,重要的是如何求大于x的第k个元素,我们可以先求出小于等于x的数有多少个(cnt),然后问题就转化成了,在整个容器中,第cnt+k个元素的值是多少。

第k大:如1 2 3 3 3 4 4 5第4大的数是3,第5大的数还是3

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <stdlib.h>
#include <string>
#include <map>
#include <iostream>
#include <sstream>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;
#define mem(a, b) memset(a, b, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int inf = 0x3f3f3f3f;
int sum[N << 2], vis[N];
void pushup(int rt)
{
    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}
void build(int l, int r, int rt)
{
    if (l == r)
    {
        sum[rt] = 0;
        return;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int p, int add, int l, int r, int rt)
{
    if (l == r)
    {
        sum[rt] += add;
        return;
    }
    int m = (l + r) >> 1;
    if (p <= m)
        update(p, add, lson);
    else
        update(p, add, rson);
    pushup(rt);
}
int query(int k, int l, int r, int rt) //求整个线段树中第k大
{
    if (l == r)
        return l;
    int m = (l + r) >> 1;
    if (sum[rt << 1] >= k)
        return query(k, lson);
    return query(k - sum[rt << 1], rson);
}
int Rank(int p, int l, int r, int rt) //<=p的数出现的总次数
{
    if (p >= r)
        return sum[rt];
    int m = (l + r) >> 1;
    int ans = 0;
    ans += Rank(p, lson);
    if (m <= p - 1)
        ans += Rank(p, rson);
    return ans;
}
int main()
{
    //freopen("in.txt", "r", stdin);
    int n;
    while (~scanf("%d", &n))
    {
        mem(vis, 0);
        int op, x, k;
        build(1, N, 1);
        while (n--)
        {
            scanf("%d", &op);
            if (op == 0)
            {
                scanf("%d", &x);
                update(x, 1, 1, N, 1);
                vis[x]++;
            }
            else if (op == 1)
            {
                scanf("%d", &x);
                if (!vis[x])
                    puts("No Elment!");
                else
                {
                    update(x, -1, 1, N, 1);
                    vis[x]--;
                }
            }
            else if (op == 2)
            {
                scanf("%d%d", &x, &k);
                int ans = Rank(x, 1, N, 1); //查找<=x的数个个数
                if (sum[1] < ans + k)
                    puts("Not Find!");
                else
                    printf("%d\n", query(ans + k, 1, N, 1));
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值