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

来源:互联网 发布:一淘跟淘宝有关系吗 编辑:程序博客网 时间:2024/06/06 08:38

KiKi's K-Number

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


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
50 51 20 62 3 22 8 170 20 20 42 1 12 1 22 1 32 1 4
 

Sample Output
No Elment!6Not Find!224Not Find!
 

Source
2009 Multi-University Training Contest 4 - Host by HDU

思路:可以用一个数组记录每个数值的个数,删除的时候判断一下是否存在,使用树状数组使得整个容器是有序的,可以用二分找出比a大的第k大的数。

#include <stdio.h>#include <string.h>#define maxn 100005#define ll long long intint vis[maxn];int n, k;ll bit[maxn];ll sum(int i){    ll s = 0;    while(i > 0){        s += bit[i];        i -= i & -i;    }    return s;}void add(int i, int x){    while(i < n){        bit[i] += x;        i += i & -i;    }}int binary(int x, int k){    int l = x, r = n - 1, mid;    ll tmp;    if(sum(r) - sum(x) < k)return 0;    while(l <= r){        mid = (l + r)>>1;        tmp = sum(mid) - sum(x);        if(tmp == k){            if(vis[mid]) return mid;            else r = mid - 1;        }        else if(tmp > k){            if(sum(mid - 1) - sum(x) < k)return mid;            r = mid - 1;        }        else l = mid + 1;    }    return 0;}int main(){    int m, p, e, a;    while(scanf("%d", &m) != EOF){        n = 100000;        memset(bit, 0, sizeof bit);        memset(vis, 0, sizeof vis);        while(m--){            scanf("%d", &p);            if(p == 0){                scanf("%d", &e);                add(e, 1);                vis[e]++;            }else if(p == 1){                scanf("%d", &e);                if(vis[e] == 0) printf("No Elment!\n");                else{                    add(e, -1);                    vis[e]--;                }            }else{                scanf("%d %d", &a, &k);                int res = binary(a, k);                if(res == 0) printf("Not Find!\n");                else printf("%d\n", res);            }        }    }}


0 0
原创粉丝点击