bzoj2843 Polar Travel Agency
Test instructions
Some points, each point has a weighted value. There are three kinds of operations: Point and Point Edge, single-point modification weights, two points between the points on the right value and (need to determine whether the input is legal)
Exercises
I've never wondered why God Ben's templates lct the link and cut after the root node flips the tag. Now understand, because only in this way can maintain depth of correctness, previously did not explode because I used to be LCT to be used as a chain profile, do not link and cut~~ This problem is LCT template problem is nothing to say. But CCZ has a faster approach, that is, offline read all the connected edge operation, and then build a tree with a chain profile, determine whether the input is legitimate online and check set query and maintenance. orzccz!
Code:
1#include <cstdio>2#include <algorithm>3#include <cstring>4 #defineInc (I,J,K) for (int i=j;i<=k;i++)5 #defineMAXN 500006 using namespacestd;7 8 intfa[maxn],ch[maxn][2],SM[MAXN],V[MAXN];BOOLREV[MAXN];9InlinevoidUpdateintx) {Ten if(x==0)return; sm[x]=V[x]; One if(ch[x][0]) sm[x]+=sm[ch[x][0]];if(ch[x][1]) sm[x]+=sm[ch[x][1]]; A } -InlineBOOLIs_root (intx) { - if(x==0|| fa[x]==0)return 1; the returnx!=ch[fa[x]][0]&&x!=ch[fa[x]][1]; - } -InlinevoidPushdown (intx) { - if(Rev[x]) { +Swap (ch[x][0],ch[x][1]);if(ch[x][0]) rev[ch[x][0]]^=1;if(ch[x][1]) rev[ch[x][1]]^=1; rev[x]^=1; - } + } A voidRotateintx) { at if(x==0|| Is_root (x))return; - intA1=FA[X],A2=FA[FA[X]],A3;BOOLB1= (x==ch[a1][1]), b2= (a1==ch[a2][1]), B3=is_root (A1); a3=ch[x][!B1]; - if(!B3) ch[a2][b2]=x; FA[X]=A2; CH[A1][B1]=A3;if(A3) Fa[a3]=a1; CH[X][!B1]=A1; fa[a1]=x; -Update (A1); Update (x);if(!b3) update (A2); - } - intdt[maxn],dts,y; in voidSplay (intx) { - if(x==0)return; dts=0; Y=x; while(! Is_root (y)) dt[++dts]=y,y=Fa[y]; toDt[++dts]=y; while(dts) pushdown (Dt[dts]), dts--; + while(!is_root (x)) { - if(!is_root (fa[x])) ((x==ch[fa[x]][1]) ^ (fa[x]==ch[fa[fa[x]]][1]))?Rotate (x): Rotate (fa[x]); the rotate (x); * } $ }Panax Notoginseng intAccessintx) { - if(x==0)return 0;intt=0; the while(x) {splay (x); ch[x][1]=t; Update (x);if(t) fa[t]=x; T=x; x=fa[x];} + returnT; A } the BOOLLinkintXinty) { + if(x==0|| y==0)return 0; Access (x); Splay (x); rev[x]^=1; Fa[x]=y;return 1; - } $ intQuerysum (intXinty) { $ if(x==0|| y==0)return 0;if(x==y)returnV[X]; Access (x);intA=access (y); splay (x); - if(x==a)returnv[a]+sm[ch[a][1]];Else returnsm[x]+v[a]+sm[ch[a][1]]; - } the voidChangeintXinty) { -Splay (x); v[x]=y; update (x);Wuyi } the intFindintx) { -Access (x); Splay (x); while(ch[x][0]) x=ch[x][0];returnx; Wu } - intN,m;Chars[ -]; About intMain () { $ //freopen ("Test.txt", "R", stdin); -scanf"%d", &n); Inc (I,1, N) scanf ("%d", &v[i]), fa[i]=0, ch[i][0]=ch[i][1]=rev[i]=0, Sm[i]=v[i]; scanf"%d",&m); -Inc (I,1, M) { - intb; scanf"%s%d%d",s,&a,&b); A if(s[0]=='b'){if(Find (a) ==find (b)) puts ("No");ElseLink (A, b), puts ("Yes");} + if(s[0]=='P') Change (a, b); the if(s[0]=='e')if(Find (a)!=find (b)) puts ("Impossible");Elseprintf"%d\n", Querysum (A, b)); - } $ return 0; the}
20160420
bzoj2843 Polar Travel Agency