HDU 2852 KiKi's K-Number(单点更新,求值)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2852

题意:0 e 往容器里翻一个数e   

1 e 从容器里删除一个数e 

2 a k 求第k个比a大的数

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>
const int inf = 0x3f3f3f3f;//1061109567
typedef long long LL;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 100003;
int sum[maxn<<2];
using namespace std;
void pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void update(int c,int p,int l,int r,int rt)
{
    if(l == r)
    {
        if(p == 1)
        {
            if(sum[rt] >= 1)
                sum[rt]--;//注意这里是rt
            else
                printf("No Elment!\n");
        }
        else
            sum[rt]++;//注意这里是rt
        return;
    }
    int m = (l+r)>>1;
    if(c <= m) update(c,p,lson);
    else update(c,p,rson);
    pushup(rt);
}
int cal(int L,int R,int l,int r,int rt)//计算出比a小的数字有多少个
{
    if(l >= L && r <= R) return sum[rt];
    int m =(l+r)>>1;
    int ans = 0;
    if(L <= m) ans += cal(L,R,lson);
    if(R > m) ans += cal(L,R,rson);
    return ans;
}
void query(int q,int l,int r,int rt)
{
    if(l == r)
    {
        printf("%d\n",l);//叶子节点的左边界即为所要求的值
        return;
    }
    int m = (l+r)>>1;
    if(q <= sum[rt<<1])
        query(q,lson);
    else
        query(q-sum[rt<<1],rson);
}
int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        int op,a,b;
        memset(sum,0,sizeof(sum));
        while(n--)
        {
            scanf("%d",&op);
            if(op == 2)
            {
                scanf("%d%d",&a,&b);
                int x = cal(1,a,1,maxn,1);
                if(x+b<=sum[1])
                    query(x+b,1,maxn,1);
                else
                    printf("Not Find!\n");
            }
            else
            {
                scanf("%d",&a);
                update(a,op,1,maxn,1);
            }
        }
    }
    return 0;
}


你可能感兴趣的:(HDU 2852 KiKi's K-Number(单点更新,求值))