Descriptiongiven an initial empty integer sequence (the element is labeled by 1) and some queries:Type 1: Add the number x after the array. Type 2: In the range l ... Y is found in R, maximizing (x xor y). Type 3: Delete the last k elements of the array. Type 4: In the range l ... R, counts the number of elements that are less than or equal to X. Type 5: In the range l ... R, find the small number of K. Inputinput data first behavior an integer q, which indicates the number of queries, followed by Q line, each line of a query corresponding to the title description. The query format for type 1 is "1 x". The query format for type 2 is "2 L R x". The query format for Type 3 is "3 K". The query format for type 4 is "4 L R x". The query format for type 5 is "5 L R K". Outputfor each 2, 4, 5 query output one line corresponds to the answer
The number of pre-maintenance I can be trie by binary persistence
In fact, there is no essential difference between trie and degenerate tree segment trees, which can be interlinked in most operations.
The trie can support querying the K-decimal value, less than the number of x, and all other operations of the segment tree, while supporting the maximum value of the query XOR specified number that is not normally supported by the segment tree
#include <cstdio>#include<algorithm>#include<cstring>inlineintinput () {intx=0, C=getchar (), f=1; while(c> $|| c< -){ if(c=='-') f=-1; C=GetChar (); } while(c> -&&c< -) x=x*Ten+c- -, c=GetChar (); returnx*F;}Const intn=520050;intq,rt[n],ap=1;intlc[n* -],rc[n* -],sz[n* -],pp=1; inlineintKmin (intRintLintk) { intL=0, r=1<< -, M; ints=sz[lc[r]]-Sz[lc[l]]; Do{M=l+r>>1; if(s<k) {R=rc[r];l=Rc[l]; L=l; S+=sz[lc[r]]-Sz[lc[l]]; }Else{R=lc[r];l=Lc[l]; R=M; S-=sz[rc[r]]-Sz[rc[l]]; } } while(L +1<s); returnL;}intLessintRintLintXintL=0,intR=1<< -){ if(x>=r-1)returnsz[r]-Sz[l]; intM=l+r>>1; if(x<m)returnLess (lc[r],lc[l],x,l,m); returnsz[lc[r]]-sz[lc[l]]+Less (rc[r],rc[l],x,m,r);} InlineintXins (intXintW) { intU=pp++,r=u; Sz[u]=sz[w]+1; for(intI=1<< +; i;i>>=1){ if(x&i) {Lc[u]=Lc[w]; U=rc[u]=pp++; W=Rc[w]; Sz[u]=sz[w]+1; }Else{Rc[u]=Rc[w]; U=lc[u]=pp++; W=Lc[w]; Sz[u]=sz[w]+1; } } returnR;} InlineintXmax (intRintLinty) { intx=0; for(intI=1<< +; i;i>>=1){ if(y&i) { if(Sz[lc[r]]!=sz[lc[l]]) x|=i,r=lc[r],l=Lc[l]; ElseR=rc[r],l=Rc[l]; }Else{ if(Sz[rc[r]]!=sz[rc[l]]) x|=i,r=rc[r],l=Rc[l]; ElseR=lc[r],l=Lc[l]; } } returnx^y;}intMain () {Q=input (); for(intI=0; i<q;i++){ Switch(Input ()) { Case 1: {Rt[ap]=xins (Input (), rt[ap-1]); AP++; Break; } Case 2:{ intL=input (), R=input (), x=input (); printf ("%d\n", Xmax (rt[r],rt[l-1],x)); Break; } Case 3:{ intk=input (); AP-=K; for(RegisterintI=rt[ap];i<pp;++i) lc[i]=rc[i]=sz[i]=0; PP=Rt[ap]; Break; } Case 4:{ intL=input (), R=input (), x=input (); printf ("%d\n", Less (rt[r],rt[l-1],x)); Break; } Case 5:{ intL=input (), R=input (), x=input (); printf ("%d\n", Kmin (rt[r],rt[l-1],x)); Break; } } } return 0;}
bzoj4546 Codechef XRQRS