树状数组+二分查找
/* ***********************************************
Author :xryz
Email :523689985@qq.com
Created Time :4-26 22:23:18
File Name :KiKi's K-Number.cpp
************************************************ */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int N=100000+5;
int p[N];
int lowbit(int x)
{
return x&-x;
}
int sum(int x)
{
int ret=0;
while(x>0)
{
ret+=p[x];
x-=lowbit(x);
}
return ret;
}
void add(int x,int d)
{
while(x<=N)
{
p[x]+=d;
x+=lowbit(x);
}
}
int binaryfind(int a,int k)
{
int low=a+1,high=N-1,mid;
int t=sum(a);
int ans=N,num;
while(high>=low)
{
mid=(low+high)>>1;
num=sum(mid);
if(num-t>=k)
{
high=mid-1;
if(mid<ans)
ans=mid;
}
else low=mid+1;
}
return ans;
}
int main()
{
int k,m,t,a;
while(~scanf("%d",&k))
{
memset(p,0,sizeof(p));
while(k--)
{
scanf("%d",&m);
if(m==0)
{
scanf("%d",&t);
add(t,1);
}
else if(m==1)
{
scanf("%d",&t);
if(sum(t)-sum(t-1)>=1)
{
add(t,-1);
}
else
printf("No Elment!
");
}
else if(m==2)
{
scanf("%d%d",&a,&t);
int f=binaryfind(a,t);
if(f==N)
printf("Not Find!
");
else printf("%d
",f);
}
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/