Luogu P3919 [Template] can persist array (persisted segment tree/Balance Tree) topic
#include <iostream>#include<cstdlib>#include<cstdio>#include<cmath>#include<cstring>#include<iomanip>#include<algorithm>#include<ctime>#include<queue>#include<stack>#defineRG Register#defineIl inline#defineLST Long Long#defineN 1000050using namespacestd;intn,q,cnt;intVal[n];intRoot[n];structtree{intLs,rs,v;} Ljl[n* -];ilintRead () {RGints=0, m=1; RGCharCh=GetChar (); while(ch!='-'&& (ch<'0'|| Ch>'9')) ch=GetChar (); if(ch=='-') m=-1, ch=GetChar (); while(ch>='0'&&ch<='9') s= (s<<3) + (s<<1) +ch-'0', ch=GetChar (); returns*m;} IlvoidBuild (RGint&now,rgintLe,rgintRI)//now the point (can be passed back), left end, right end{ljl[++cnt]=ljl[now];//Open a new point ...now=cnt;//Make sure now refers to this point, because it is also sent back to "Dad's left/right son" ... Look at the back of the recursive build if(Le==ri) {Ljl[now].v=val[le];return;}//if the assignment to a single point is OJBK (same segment tree)RgintMid= (Le+ri) >>1; Build (Ljl[now].ls,le,mid), build (Ljl[now].rs,mid+1, RI); //building the left son and right son, this node assigns values to their pointers when the recursive function is passed back (all from a Beautiful "&")}voidModify (RGint&now,rgintLe,rgintRi,rgintKk,rgintX//It's like build .//now the point (can be passed back), the left end, the right endpoint, the point number to be modified, the modified value{ljl[++cnt]=ljl[now];//It's a little bit more open ...now=cnt;//Make sure now refers to this point, because it is also sent back to "Dad's left/right son" ... Look at the back of the recursive modify if(Le==ri) {ljl[now].v=x;return;}//Assign ValuesRgintMid= (Le+ri) >>1; if(kk<=mid) Modify (LJL[NOW].LS,LE,MID,KK,X); ElseModify (ljl[now].rs,mid+1, ri,kk,x); //KK on the left side of mid, build left child, or build right child ... Need to simulate OH ...}intQuery (RGintNow,rgintLe,rgintRi,rgintKK) { if(Le==ri)returnljl[now].v; RGintMid= (Le+ri) >>1; if(Kk<=mid)returnQuery (LJL[NOW].LS,LE,MID,KK); Else returnQuery (ljl[now].rs,mid+1, RI,KK);}intMain () {n=read (), q=read (); for(RGintI=1; i<=n;++i) val[i]=read (); Build (root[0],1, n);//first, build a line tree on the root of number No. 0 . for(RGintI=1; i<=q;++i) {RGintEdi=read (), Type=read ();//historical version edi, asking for type if(type==1) {RGintKk=read (), X=read ();//Change the KK Val to xRoot[i]=root[edi];//first connect the root, and then modify! Modify (Root[i],1, n,kk,x);//starting from the root, left, right endpoint, modified number, modified to the value } Else{RGintkk=read (); printf ("%d\n", Query (Root[edi],1, N,KK));//The current node (that is, the root of the EDI), the left and right endpoint, asks for the value of the KK numberRoot[i]=root[edi];//This depends on the question, or get it over ... } } return 0;}
Luogu P3919 [Template] Persistent array (can persist segment tree/Balance Tree) (Chairman tree)