The main topic: for a sequence, each query interval [l,r] k tree.
Analysis:
Chairman Tree Template title
ProgramKthtree;type Point=RecordL,r,s:longint; End;varT:Array[0..100000* -] ofPoint ; A,b,id,root:Array[0..100000] ofLongint; N,i,m,x,y,k,v,len:longint;procedureqsort (l,h:longint);varI,j,t,m:longint;beginI:=l; j:=h; M:=a[(I+J)Div 2]; Repeat whileA[i]<m DoInc (i); whileM<A[J] DoDec (j);ifI<=j Then beginT:=a[i]; A[I]:=A[J]; a[j]:=t; T:=ID[I];ID[I]:=ID[J]; id[j]:=T;inc (i); Dec (j); End;untilI>J;ifI ThenQsort (I,H);ifJ>l ThenQsort (L,J);End;procedureAdd (l,r,v:longint;varp:longint);varMid:longint;beginInc (LEN); T[len]:=T[P]; p:=Len; Inc (T[P].S); ifL=r Thenexit; Mid:= (l+r)Div 2; ifV<=mid ThenAdd (L,MID,V,T[P].L)ElseAdd (mid+1, R,V,T[P].R);End;functionquery (x,y,l,r,k:longint): Longint;varMid,s:longint;begin ifL=r Thenexit (L); Mid:= (l+r)Div 2; s:=t[t[y].l].s-T[t[x].l].s; ifK<=s Thenexit (Query (T[X].L,T[Y].L,L,MID,K))ElseExit (Query (t[x].r,t[y].r,mid+1, r,k-s));End;beginreadln (n,m); fori:=1 toN - Dot[i].s:=0; fori:=1 toN Do beginRead (A[i]); Id[i]:=i;End; Qsort (1, N); fori:=1 toN Dob[id[i]]:=i; Len:=0; fori:=1 toN Do beginRoot[i]:=root[i-1]; Add (1, N,b[i],root[i]); End; fori:=1 toM Do beginreadln (x,y,k); Writeln (A[query (root[x-1],root[y],1, N,k)]); End;End.
View Code
POJ 2104:k-th Number (Chairman tree static interval K Large)