题意:给定一个容器,里面存放各种数值,规定三个操作,一个是在容器中增加一个数值,一个是在容器中删掉一个数值,一个是询问容器中比a大的数中第k大的数,将其输出。如果在删除过程中没有这个数,则输出"No Elment!",如果容器中没有比a大的第k个数,则输出"Not Find!".
要多次查询大于某个值得第k个值。所以可以用树状数组来维护这个容器,用二分来查询
这个值。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
ll a[maxn], tree[maxn];
int lowbit(int x) { return x&(-x); }
void update(int pos, int val)
{
while(pos < maxn)
{
tree[pos] += val;
pos += lowbit(pos);
}
}
ll query(int pos)
{
ll sum = 0;
while(pos)
{
sum += tree[pos];
pos -= lowbit(pos);
}
return sum;
}
int solve(int x, int k)
{
int l = x+1, r = maxn-1;
while(l < r)
{
int mid = (l+r)/2;
if(query(mid)-query(x) >= k) r = mid;
else l = mid+1;
}
return r;
}
int main(void)
{
ll m;
while(cin >> m)
{
memset(a, 0, sizeof(a));
memset(tree, 0, sizeof(tree));
while(m--)
{
int cmd, x, y;
scanf("%d%d", &cmd, &x);
if(!cmd)
{
update(x, 1);
a[x]++;
}
else if(cmd == 1)
{
if(!a[x]) printf("No Elment!\n");
else update(x, -1), a[x]--;
}
else
{
scanf("%d", &y);
if(query(maxn-1)-query(x) < y) printf("Not Find!\n");
else printf("%d\n", solve(x, y));
}
}
}
return 0;
}
KiKi's K-Number
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3849 Accepted Submission(s): 1709
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?
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.
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