Analysis: Note that the queue holds the subscript in the sorted array. The tree array is also processed according to the subscript.
#include <iostream> #include <queue> #include <algorithm>using namespace std; #define N 10005int C[n <<2];int N,a[n],b[n];char str[n][10];int lowbit (int x) {return x& (-X);} void Update (int x,int val) {while (x<n) {c[x]+=val; X+=lowbit (x); }}int sum (int x) {int s=0; while (x>0) {s+=c[x]; X-=lowbit (x); } return s;} int solve (int x) {int id,l,r,mid; id=x/2+1; l=1; R=n; while (l<=r) {mid= (l+r) >>1; if (sum (mid) >=id) r=mid-1; else l=mid+1; } return b[l];} int main () {int x,t=0,i,cnt; while (scanf ("%d", &n) ==1) {queue<int> q; Memset (C,0,sizeof (c)); Cnt=1; for (i=1;i<=n;i++) {scanf ("%s", Str[i]); if (str[i][0]== ' I ') {scanf ("%d", &x); B[cnt++]=x; A[i]=x; }} sort (b+1,b+cnt+1); Sort Cnt=unique (b+1,b+cnt+1)-(b+1); Go to the heavy printf ("Case #%d:\n", ++t); for (i=1;i<=n;i++) {if (str[i][0]== ' I ') {X=lower_bound (b+1,b+cnt+1,a[i])-B; Update (x,1); Q.push (x); The queue holds the subscript} else if (str[i][0]== ' O ') {x=lower_bound (B+1,b+cnt+1,b[q.fron T ()])-B; Update (X,-1); Q.pop (); } else if (str[i][0]== ' Q ') printf ("%d\n", Solve (Q.size ())); }} return 0; }
HDU ACM 5249 kpi-> tree-shaped array + two-point