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

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

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
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
 
这道题是一题,题意是要找a的第k大的数,由于想到树状数组,也有这么一种解法,所以就拿来练练手,首先采用传统的树状数组上升模式,进行
累加和,不过这里的和表示的是比a比大的个数,所以在我们查询的时候只要将要查找的的数sum(val)-sum(a)==k来进行二分,当然由于数据是离散的,而我们这里有无法进行离散化处理,因此我们只需要加入一个辅助数组lab[],进行判断就可以了.....这道题,第一次遇到可算是想了一段时间,毕竟是个菜鸟....做出来还是很幸福的说....
代码如下:
 #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 ;
}

并给出一些数据来帮助验证吧....


 
上一篇:搞定所有的跨域请求问题 jsonp CORS


下一篇:Google C++ Coding Style 学习笔记