hdu 2852 树状数组+二分

/*
题意:每次输入p,0表示插入e,1表示删除e,2表示找到数组中比a大的第k大的数

题解:树状数组+二分
用树状数组插入点,删除点以及求比a大的第k大的数,需要用二分进行加速,否则会超时;
用树状数组记录时通过记录点的个数,然后找到满足比a大的点x,sum(a+1~x)>=k即可
*/
#include <cstdio>
#include <algorithm>
#include <cstring>

#define MAXVER 100005

using namespace std;

int treec[MAXVER];
int arrayNum;

int lowbit( int x )
{
    return x & (-x);
}

void modify(int n, int value)
{
    while (n <= arrayNum)
    {
        treec[n] += value;
        n += lowbit(n);
    }
}

int sum(int n)
{
    int ret = 0;
    while (n != 0)
    {
        ret += treec[n];
        n -= lowbit(n);
    }
    return ret;
}

int main(void)
{
    int m;
    while (~scanf("%d",&m))
    {
        arrayNum = 100000;
        int maxelement = 0;
        memset(treec,0,sizeof(treec));
        while (m--)
        {
            int p,e;
            scanf("%d",&p);
            if (0 == p)
            {
                scanf("%d",&e);
                maxelement = max(maxelement, e);
                modify(e,1);
            }
            else if (1 == p)
            {
                scanf("%d",&e);
                if (sum(e) - sum(e-1))
                    modify(e,-1);
                else
                    printf("No Elment!\n");
            }
            else
            {
                int a,k;
                scanf("%d%d",&a,&k);
                int tsum = sum(a);
                int mid,fro,tail;
                fro = a+1;
                tail = maxelement;
                mid = (fro + tail) / 2;
                int csum = 0;
                while (fro < tail)
                {
                    csum = sum(mid);
                    if (csum - tsum >= k)
                        tail = mid ;
                    else
                        fro = mid + 1;
                    mid = (fro + tail) / 2;
                }
                if (sum(tail) - tsum < k)
                    printf("Not Find!\n");
                else
                    printf("%d\n",tail);
            }
        }
    }
    return 0;
}

 

posted @ 2014-03-22 18:31  辛力啤  阅读(198)  评论(0编辑  收藏  举报