0%

HDU_2852

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 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.

输出描述:

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!”.

示例1
输入

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

输出

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

HDU_2852.cppview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <cstring>
using namespace std;

const int N = 100000;

int sum[N+10];

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

void add(int x, int d){
for(int i=x;i<=N;i+=lowbit(i))
sum[i] +=d;
}

int query(int x){
int ans = 0;
for(int i=x;i>0;i-=lowbit(i)){
ans += sum[i];
}
return ans;
}


int main(){
int m, t, e, a, k;
while(~scanf("%d", &m)){
memset(sum, 0, sizeof sum);
while(m--){
scanf("%d", &t);
if(t==0){
scanf("%d", &e);
add(e, 1);
}else if(t==1){
scanf("%d", &e);
if(!(query(e) - query(e-1))) printf("No Elment!\n");
else add(e, -1);
}
else{
scanf("%d%d", &a, &k);
int v = query(a)+ k;
int l = 0, r = N;
while(l<r){
int mid = l+r>>1;
if(query(mid)>=v) r = mid;
else l = mid+1;
}
if(r==0 || r==N) printf("Not Find!\n");
else printf("%d\n", r);
}
}
}
return 0;
}