KiKi's K-Number
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2393 Accepted Submission(s): 1101
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?
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.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 100000
#define lowbit(x) ((x)&(-x))
int aa[maxn+];
int lab[maxn+];
void ope(int x,int val)
{
while(x<=maxn)
{
aa[x]+=val;
x+=lowbit(x);
}
}
int cont(int x)
{
int ans=;
while(x>)
{
ans+=aa[x];
x-=lowbit(x);
}
return ans;
}
int main()
{
int n,p,a,k;
int left,right,mid,res;
bool tag=true;
// freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
while(scanf("%d",&n)!=EOF)
{
memset(aa,,sizeof(aa));
memset(lab,,sizeof(lab));
while(n--)
{
scanf("%d",&p);
if(p==)
{
scanf("%d%d",&a,&k);
left=a, right=maxn;
tag=true;
while(left<=right)
{
mid=left+(right-left)/;
res=cont(mid)-cont(a);
if(res<k)
left=mid+;
else if(res-lab[mid]>=k)
right=mid-;
else
if(res>=k&&res-lab[mid]<k)
{
printf("%d\n",mid);
tag=false;
break;
}
}
if(tag)
printf("Not Find!\n"); }
else
{
scanf("%d",&a);
if(p==)
{
if(lab[a]==)
printf("No Elment!\n");
else
{
lab[a]--;
ope(a,-); //1代表insert
}
}
else
{
lab[a]++;
ope(a,); //0代表elete
}
}
}
}
return ;
}
并给出一些数据来帮助验证吧....