题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852
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
No Elment! 6 Not Find! 2 2 4 Not Find!
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstdio>
#include <cmath>
const int N=1e5+100;
const int M=1e5+10;
using namespace std;
int c[N],m;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int d)
{
while(x<=M)
{
c[x]+=d;
x+=lowbit(x);
}
}
int getsum(int x)
{
int ans=0;
while(x>0)
{
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
int find(int a,int k)
{
int ct=getsum(a);
int l=a+1,r=M;
while(l<=r)
{
int mid=(l+r)/2;
int sum=getsum(mid);
if(sum-ct==k)
{
if(getsum(mid-1)-ct<k) return mid;
else r=mid-1;
}
else if(l==r&&sum-ct>k)return l;
else if(sum-ct<k) l=mid+1;
else if(sum-ct>k) r=mid-1;
}
if(getsum(min(l,r))-ct>k)return min(l,r);
if(getsum(max(l,r))-ct>k)return max(l,r);
return 0;
}
int main()
{
int option,e,a,k;
while(scanf("%d",&m)!=EOF)
{
memset(c,0,sizeof(c));
for(int i=0;i<m;i++)
{
scanf("%d",&option);
if(option==0)
{
scanf("%d",&e);
update(e,1);
}
else if(option==1)
{
scanf("%d",&e);
if(getsum(e)-getsum(e-1)==0)
{
printf("No Elment!\n");
continue;
}
update(e,-1);
}
else if(option==2)
{
scanf("%d%d",&a,&k);
int temp=find(a,k);
if(temp==0)
{
printf("Not Find!\n");
}
else printf("%d\n",temp);
}
}
}
return 0;
}
原文地址:http://blog.csdn.net/liusuangeng/article/details/39371893