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!
发现 删除的那个不会写,后来看了博客。才明白 只要是sum(a)-sum(a-1)
即可,若为0 则没有。 然后是 添加,直接是更新就行, 最后是输出,输出的话,
有一种情况是 没有对应的值,然后 本题是通过二分,当到sum right 减sum left
是小于等于k之后,输出ans,若ans等于maxn 则输出未找到
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#define maxn 100002
using namespace std;
int ss[maxn]; //定义 保存书
int lowbit(int x)
{
return x&(-x);
}
int sum(int x) //求和
{
int suM=0;
while(x>0)
{
suM+=ss[x];
x-=lowbit(x);
}
return suM;
}
void updata(int x,int y) //加减
{
while(x<maxn)
{
ss[x]+=y;
x+=lowbit(x);
}
}
int find(int x,int y)
{
int ans=maxn;
int l=x+1;
int r=maxn-1;
int SUM=sum(x);
while(l<=r)
{
int w=(l+r)>>1;
int SUM2=sum(w);
if(SUM2-SUM>=y)
{
r=w-1;
if(w<ans)
ans=w;
}
else l=w+1;
}
return ans;
}
int main()
{
int n,m,a,b,c;
while(~scanf("%d",&m))
{
memset(ss,0,sizeof(ss));
for(int i=0;i<m;i++)
{
scanf("%d",&a);
if(a==0) //添加
{
scanf("%d",&b);
updata(b,1);
}
if(a==1) //减少
{
scanf("%d",&b);
if(sum(b)-sum(b-1)==0) printf("No Elment!\n");
else updata(b,-1);
}
if(a==2) //输出
{
scanf("%d%d",&b,&c);
int num=find(b,c);
if(num==maxn) printf("Not Find!\n");
else printf("%d\n",num);
}
}
}
return 0;
}