Segment tree only with leaf nodes feel inexplicable waste,,,
It feels good to write (someone who just escaped from a future program)
1#include <cstdio>2 #defineMid ((l+r) >>1)3 intn,m,time,n=0, Ca,x,y;4 introot[500000],ls[4000000],rs[4000000],a[4000000];5 intChangeintXinty)6 {7 intL=1, r=n,now=++n,past=Root[time];8root[++time]=N;9 while(l!=R)Ten if(X<=mid) rs[now]=rs[past],ls[now]=++n,r=mid,now=ls[now],past=Ls[past]; One ElseLs[now]=ls[past],rs[now]=++n,l=mid+1, now=rs[now],past=Rs[past]; Aa[now]=y; - } - intQueintx) the { - intL=1, r=n,now=Root[time]; - while(l!=R) - if(X<=mid) r=mid,now=Ls[now]; + ElseL=mid+1, now=Rs[now]; - returnA[now]; + } A int Get(intp) at { - for(intT=que (P); T!=p;t=que (p)) p=T; - returnp; - } - intMain () - { inscanf"%d%d",&n,&m); -Root[time=0]=1; to for(intI=1; i<=n;i++) + Change (i,i); - intstart=Time ; the for(intI=1; i<=m;i++) * { $scanf"%d%d",&ca,&x);Panax Notoginseng if(ca==2) root[++time]=root[x+start]; - Else thescanf"%d", &y), (ca==1)? Change (Get(y),Get(x)):( root[time+1]=root[time++],printf ("%d\n",Get(x) = =Get(y))); + } A return 0; the}
BZOJ3673 durable array implementations can be persisted and checked (very long) by the persistence of the segment tree