//树状数组加二分查找。。这可是我自己想出来的啊。。看了一下网上的东东,发现也是这个思路。。窃喜中。。哈哈。我的树状数组和网上的不太一样。。他是求比其小的,我是求比其大的。。很奇怪,我觉得求比其大的容易,但是不知怎么的,网上的全是求比其小得。。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <fstream>
#define N 100008
using namespace std;
int tree[N],n,succ;
int use[N];
int lowbit(int k)
{
return k&(-k);
}
void Modify(int x,int v)
{
while (x>0)
{
tree[x] += v;
x -= lowbit(x);
}
}
int sum(int x)
{
int ans = 0;
while (x <= N)
{
ans += tree[x];
x += lowbit(x);
}
return ans;
}
void bs(int x,int y)
{
int i,k,mi,s,e;
s = x;e = N;
while (s<=e)
{
mi = (s+e)>>1;
k = sum(mi);
if(use[mi]&&(succ-k<y&&succ-sum(mi+1)>= y))
break;
if(succ-k>=y)
{
e = mi-1;
}
else
{
s = mi+1;
}
}
printf("%d\n",mi);
}
int main()
{
int m,i,j,k,x,y;
while (scanf("%d",&m)!=EOF)
{
memset(use,0,sizeof(use));
memset(tree,0,sizeof(tree));
while (m--)
{
scanf("%d",&k);
if(k==0)
{
scanf("%d",&x);
//x++;
use[x] ++;
Modify(x,1);
}
else if(k == 1)
{
scanf("%d",&x);
//x++;
if(!use[x])
printf("No Elment!\n");
else
{
use[x] --;
Modify(x,-1);
}
}
else
{
scanf("%d%d",&x,&y);
x++;
succ = sum(x);
if(succ < y)
{
printf("Not Find!\n");
}
else
{
bs(x,y);
}
}
}
}
return 0;
}