Maintain a sum array, a little bit of tree thinking, write a tree should be able to see
#include <cstdio>#include<algorithm>#include<iostream>#include<cstring>#include<vector>#include<stack>#include<cmath>#include<queue>#include<map>using namespacestd;Const intmaxn=500005;intsum[maxn<<2], is[MAXN];voidPushup (intRT) {Sum[rt]=sum[rt*2]+sum[rt*2+1];}voidBuildintRtintLintR) { if(l==r) { is[l]=sum[rt]=1; return; } intM= (l+r) >>1; Build (Rt*2, l,m); Build (Rt*2+1, m+1, R); Pushup (RT);}voidChangeintRtintLintRintPosintc) { if(l==R) {Sum[rt]=C; return; } intM= (l+r) >>1; if(pos<=m) Change (rt*2, L,m,pos,c); ElseChange (rt*2+1, m+1, R,pos,c); Pushup (RT);}intQueryintRtintLintRintk) { if(l==r) {returnl; } intM= (l+r) >>1; if(sum[rt*2]>=K)returnQuery (rt*2, l,m,k); Else returnQuery (rt*2+1, m+1, r,k-sum[rt*2]);}intMain () {intn,m; scanf ("%d%d",&n,&m); Build (1,1, N); for(intI=0; i<m;++i) {Chars[5]; intK; scanf ("%s%d",s,&k); if(s[0]=='L') { intX=query (1,1, n,k); is[x]=0; Change (1,1, N,x,0); } Else if(s[0]=='R') { if( is[k]==1)Continue; is[k]=1; Change (1,1, N,k,1); } Else { intX=query (1,1, n,k); printf ("%d\n", x); } } return 0;}
View Code
FOJ 1962 new Drumming Flower Game line tree