https://uva.onlinejudge.org/index.php
Fhp-treap can be persisted. As long as the line tree is persisted from top to bottom, a new log (n) node is created each time.
#include <bits/stdc++.h>Const intmaxn=50015, maxlen= the;using namespacestd;intn,cnt;structttreap{intTot,ver; Static Const intMaxnode=10000015; structtnode{tnode*c[2]; intKey,siz;Charch; voidUpdate () {siz=c[0]->siz+c[1]->siz+1;} }t[maxnode],*NULL,*ROOT[MAXN]; intSeed () {return1ll*rand () *rand ()%19990211;} voidClear () {tot=ver=0;NULL=T; NULL->c[0]=NULL->c[1]=NULL;NULL->siz=0; for(intI=0; i<=n;++i) root[i]=NULL; } tnode*newnode (Tnode *x) { if(x==NULL)return NULL; Tnode*cur=t+ (+ +)tot); *cur=*x;returncur; } tnode*newnode (Charch) {Tnode*cur=t+ (+ +)tot); Cur->c[0]=cur->c[1]=NULL; Cur->ch=ch;cur->siz=1; cur->key=seed (); returncur; } tnode*merge (tnode *x,tnode *y) {Tnode*T; if(x==NULL)returnnewnode (y); if(y==NULL)returnnewnode (x); if(X->key<y->key) {T=newnode (x); t->c[1]=merge (x->c[1],y);} Else{T=newnode (y); t->c[0]=merge (x,y->c[0]);} T->update ();returnT; } voidSplit (Tnode *x,tnode *&a,tnode *&b,intrank) { if(!rank) {a=NULL; b=newnode (x);} Else if(X->siz<=rank) {A=newnode (x); b=NULL;} Else if(rank<=x->c[0]->siz) {b=newnode (x); Split (x->c[0],a,b->c[0],rank);b->update ();} Else{A=newnode (x); Split (x->c[1],a->c[1],b,rank-x->c[0]->siz-1);a->update ();} } tnode*build (Chars[]) { StaticTnode *stk[maxlen];inttop=0; for(intI=1; s[i];++i) {Tnode*cur=newnode (S[i]), *last=NULL; while(Top&&cur->key<stk[top]->key) {stk[top]->update (); last=stk[top--];} Cur->c[0]=last;cur->update (); if(top) {stk[top]->c[1]=cur;stk[top]->update ();} stk[++top]=cur; } while(top) stk[top--]->update (); returnstk[1]; } voidInsertintPosChars[]) {Tnode*a,*b; root[++ver]=build (s); Split (Root[ver-1],a,b,pos); Root[ver]=merge (A,root[ver]); Root[ver]=merge (ROOT[VER],B); } voidRemoveintPosintl) { ++ver; Tnode*a,*b,*c,*D; Split (Root[ver-1],a,b,pos-1); split (b,c,d,l); Root[ver]=merge (a,d); } voidPrint (Tnode *x) { if(x->c[0]!=NULL) Print (x->c[0]); Putchar (x->CH); cnt+= (x->ch=='C'); if(x->c[1]!=NULL) Print (x->c[1]); } voidQueryintVintPosintl) {Tnode*a,*b,*c,*D; Split (Root[v],a,b,pos-1); Split (b,c,d,l);p rint (c);p Utchar ('\ n'); }}treap;voidInsert () {intPCharQ[maxlen]; scanf ("%d%s", &p,q+1); Q[strlen (q+1)+1]=0; P-=Cnt;treap.insert (p,q);}voidRemove () {intP,C;SCANF ("%d%d",&p,&c); P-=cnt;c-=Cnt;treap.remove (p,c);}voidquery () {intV,P,C;SCANF ("%d%d%d",&v,&p,&c); V-=cnt;p-=cnt;c-=cnt;treap.query (v,p,c);}intMain () {srand (unsigned) time (NULL)); scanf ("%d",&n); Treap.clear (); for(intT,i=1; i<=n;++i) {scanf ("%d",&t); Switch(t) { Case 1: Insert (); Break; Case 2: Remove (); Break; Case 3: Query (); Break; } } return 0;}
My Code
Uva12538-version controlled IDE