#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#defineREP (I,A,B) for (int i=a;i<=b;i++)#defineMS0 (a) memset (A,0,sizeof (a))using namespaceStd;typedefLong Longll;Const intmaxn=1000100;Const intinf=1e9+Ten;intn,m;intA[MAXN];Charop[5];intl,r,k,p,c;///---treapstructnode{intch[2]; intr,v; intsz;}; Node TR[MAXN];intRT[MAXN];inttot1;intS[maxn],tot2;intNewNode () {into; if(Tot2) o=s[tot2--]; Elseo=++tot1; tr[o].ch[0]=tr[o].ch[1]=-1; TR[O].R=rand (); tr[o].v=0; Tr[o].sz=1; returno;}voidUpinto) {TR[O].SZ=1; if(~tr[o].ch[0]) tr[o].sz+=tr[tr[o].ch[0]].sz; if(~tr[o].ch[1]) tr[o].sz+=tr[tr[o].ch[1]].sz;}voidTo2 (int&o,intd) { intk=tr[o].ch[d^1]; Tr[o].ch[d^1]=Tr[k].ch[d]; TR[K].CH[D]=N; Up (o); up (k); o=K;}voidInsert (int&o,intx) { if(o==-1) {o=NewNode (); TR[O].V=x; return; } intD=X<TR[O].V?0:1; Insert (TR[O].CH[D],X); if(tr[tr[o].ch[0]].R>TR[O].R) Rot (o,d^1); Up (o);}voidRemove (int&o,intx) { if(o==-1)return; if(tr[o].v==x) { if(tr[o].ch[0]==-1) s[++tot2]=o,o=tr[o].ch[1]; Else if(tr[o].ch[1]==-1) s[++tot2]=o,o=tr[o].ch[0]; Else{ intd=tr[tr[o].ch[0]].r>tr[tr[o].ch[1]].R?0:1; Rot (O,d^1); Remove (Tr[o].ch[d^1],x); } if(~o) up (o); return; } if(X<TR[O].V) Remove (tr[o].ch[0],x); ElseRemove (tr[o].ch[1],x); if(~o) up (o);}intGETCNT (int&o,inty) { if(o==-1)return 0; if(Y<=TR[O].V)returnGETCNT (tr[o].ch[0],y); intls=tr[o].ch[0]==-1?0: tr[tr[o].ch[0]].sz; returnls+1+GETCNT (tr[o].ch[1],y);}intGetcnt2 (int&o,inty) { if(o==-1)return 0; if(Y<TR[O].V)returnGETCNT (tr[o].ch[0],y); intls=tr[o].ch[0]==-1?0: tr[tr[o].ch[0]].sz; returnls+1+getcnt2 (tr[o].ch[1],y);}///---BITintLowbit (intx) { returnx&-x;}voidAddintPintc) { while(p<=N) {Insert (rt[p],c); P+=lowbit (P); }}voidDelintPintc) { while(p<=N) {Remove (rt[p],c); P+=lowbit (P); }}intSumintPinty) { intres=0; while(p) {res+=getcnt (rt[p],y); P-=lowbit (P); } returnRes;}intSUM2 (intPinty) { intres=0; while(p) {res+=Getcnt2 (rt[p],y); P-=lowbit (P); } returnRes;}intBinintLintRintLintRintk) { while(l<=R) { intM= (l+r) >>1; intCnt=sum (r,m)-sum (l1, M); intCnt2=sum2 (r,m)-sum2 (l1, M); if(cnt<=k-1&&CNT2>=K)returnm; if(K>cnt2) l=m+1; Elser=m-1; }}voidInit () {memset (RT,-1,sizeof(RT)); TOT1=tot2=0;}intMain () {//freopen ("In.txt", "R", stdin); intT;cin>>T; while(t--) {scanf ("%d%d",&n,&m); Init (); REP (i,1, N) scanf ("%d",&A[i]); REP (i,1, N) Add (I,a[i]); while(m--) {scanf ("%s", op); if(op[0]=='C') {scanf ("%d%d",&p,&c); Del (p,a[p]); Add (P,C); A[P]=B; } Else{scanf ("%d%d%d",&l,&r,&k); printf ("%d\n", Bin (L,r,0, inf,k)); } } } return 0;}
View Code
Note the next two points to calculate two, less than and less than equal to the ...
Then a tree-like array or line tree is set up to balance the tree.
zoj2112 Dynamic Rankings Dynamics interval K-large, tree-shaped array set of balance tree