首页 > 代码库 > HDU 2852 KiKi's K-Number(树状数组+二分)

HDU 2852 KiKi's K-Number(树状数组+二分)

KiKi‘s K-Number

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2598    Accepted Submission(s): 1199


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 <e <100000), means press element e into Container.

If p is 1, then there will be an integer e (0 <e <100000), indicated that delete the element e from the container  

If p is 2, then there will be two integers a and k (0 <a <100000, 0 <k <10000),means the inquiries, the element is greater than a, and the k-th larger number.
 

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!
 

Source

0 表示添加节点

1表示删除节点

2 找到比b大的第k大的数

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#define init(a) memset(a,0,sizeof(a))
using namespace std;
#define MAX INT_MAX
#define MIN INT_MIN
#define LL __int64
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 300010;
const int maxm = 100010;
using namespace std;
int c[maxn];
int hash[maxm];
int lowbit(int x)
{ 
    return x&(-x);
}
void add(int x,int w)
{
    while(x<=maxn)
    {
        c[x]+=w;
        x+=lowbit(x);
    }
}
int sum(int i)
{
    int s=0;
    while(i>0)
    {
        s += c[i];
        i =i - lowbit(i);
    }
    return s;
}
void B_search(int b,int k)
{
     int tem = sum(b);
    int low = b;
    int high = maxm;
    int mid;
    while(low <= high)
    {
        mid = (low + high) /2;
        int st = sum(mid);
        if(hash[mid]>0 && st-hash[mid]-tem<k&&st-tem>=k)//减hash是因为可能存在多个相同元素的情况
        {
            break;
        }
        if(st-tem<k)
        {
            low = mid + 1;
        }
        else
            high = mid - 1;
    }
    printf("%d\n",mid);
}
bool query(int b,int k)
{
    if(sum(maxn)-sum(b)<k)return 0;
    B_search(b,k);
    return 1;
}

int main()
{
    int n,a,b,k;
    while(scanf("%d",&n)!=EOF)
    {
        init(c);
        init(hash);

        for(int i=0;i<n;i++)
        {
            scanf("%d",&a);
            if(a==0)
            {
                scanf("%d",&b);
                add(b,1);
                hash[b]++;
            }
            else if(a==1)
            {
                scanf("%d",&b);
                if(hash[b]>0)
                {
                     add(b,-1);
                     hash[b]--;
                }
                else
                    puts("No Elment!");
            }
            else if(a==2)
            {
                scanf("%d%d",&b,&k);
               bool tep =  query(b,k);
               if(!tep)
                puts("Not Find!");
            }
        }
    }
    return 0;
}