标签:
Description
Input
Output
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!
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <sstream> #include <algorithm> #include <set> #include <queue> #include <stack> #include <map> using namespace std; typedef long long LL; const int inf=0x3f3f3f3f; const double pi= acos(-1.0); #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 const int MAXN=100010; int sum[MAXN<<2]; void PushUp(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } void Update(int p, int x, int l, int r, int rt) { if(l==r) { sum[rt]+=p; return ; } int mid=(l+r)>>1; if(x<=mid) Update(p, x, lson); else Update(p, x, rson); PushUp(rt); } int Query(int ll, int rr, int l, int r, int rt) { if(ll<=l&&rr>=r) { return sum[rt]; } int mid=l+r>>1; int ans=0; if(ll<=mid) ans+=Query(ll,rr,lson); if(rr>mid) ans+=Query(ll,rr,rson); return ans; } int binsearch(int k) { int high=100010, low =1, mid; int ans=-1; int res; while(low<=high) { mid=(low+high)>>1; res=Query(1,mid,1,100010,1); if(res>=k) { high=mid-1; ans=mid; } else if(res<k) { low=mid+1; } } return ans; } int main() { int n,i; int p,a,k; while(~scanf("%d",&n)) { memset(sum,0,sizeof(sum)); while(n--) { scanf("%d",&p); if(p==0) { scanf("%d",&a); Update(1,a,1,100010,1); } else if(p==1) { scanf("%d",&a); if(!Query(a,a,1,100010,1)) { printf("No Elment!\n"); continue ; } Update(-1,a,1,100010,1); } else { scanf("%d%d",&a,&k); int s=Query(1,a,1,100010,1); int ans=binsearch(s+k); if(ans==-1) printf("Not Find!\n"); else printf("%d\n",ans); } } } return 0; }
标签:
原文地址:http://blog.csdn.net/u013486414/article/details/45081401