Statistics count of the original tree
LCT Dynamic Maintenance tree information. Better than the chain, but speed really does not have the advantage ...
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#define LL Long Long#define MAXN 50010#define MAXINT 0x7fffffffusing namespace STD;structsplay{intfa,ch[2]; LL sum,maxn,w;BOOLRev;} TREE[MAXN];intQ[maxn],top;Charch[Ten];voidPrint ();Long LongU,V,N,M,U[MAXN],V[MAXN];inline voidInLong Long&X) {intflag=1;CharCh=getchar (); x=0; while(! (ch>=' 0 '&&ch<=' 9 ')) flag=ch=='-'?-1:1, Ch=getchar (); while(ch>=' 0 '&&ch<=' 9 ') x=x*Ten+ch-' 0 ', Ch=getchar (); x*=flag;}inline BOOLIs_root (intx) {returntree[tree[x].fa].ch[0]!=x&&tree[tree[x].fa].ch[1]!=x;}inline voidPUSH_UP (intx) {tree[x].sum=tree[tree[x].ch[0]].sum+tree[tree[x].ch[1]].SUM+TREE[X].W; Tree[x].maxn=max (Tree[x].w,max (tree[tree[x].ch[0]].maxn,tree[tree[x].ch[1]].MAXN));}inline voidPush_down (intx) {if(Tree[x].rev) {tree[x].rev^=1; tree[tree[x].ch[0]].rev^=1; tree[tree[x].ch[1]].rev^=1; Swap (tree[x].ch[0],tree[x].ch[1]); }}inline voidRotintx) {intY=tree[x].fa,z=tree[y].fa,l,r; L= (tree[y].ch[1]==X); r=l^1;if(!is_root (y)) tree[z].ch[(tree[z].ch[1]==y)]=x; Tree[tree[x].ch[r]].fa=y;tree[y].fa=x;tree[x].fa=z; Tree[y].ch[l]=tree[x].ch[r];tree[x].ch[r]=y; Push_up (y);p ush_up (x);}inline voidSplay (intx) {q[++top]=x; for(inti=x;! Is_root (i); i=tree[i].fa) Q[++top]=tree[i].fa; while(top) Push_down (q[top--]); while(!is_root (x)) {intY=TREE[X].FA,Z=TREE[Y].FA;if(!is_root (y)) {if(tree[y].ch[0]==x^tree[z].ch[0]==y) rot (x);ElseRot (y); } rot (x); }}inline voidAccessintx) { for(intt=0; x;t=x,x=tree[x].fa) splay (x), tree[x].ch[1]=T,PUSH_UP (x);}inline voidMake_root (intx) {access (x); Splay (x); tree[x].rev^=1;}inline voidLinkintXintY) {make_root (x); tree[x].fa=y;}inline voidCutintXintY) {make_root (x); Access (y); Splay (y);}intMain () {in (n); tree[0].maxn=-maxint; for(intI=1; i<n;i++) in (U[i]), in (V[i]); for(intI=1; i<=n;i++) in (TREE[I].W), TREE[I].SUM=TREE[I].MAXN=TREE[I].W; for(intI=1; i<n;i++) Link (u[i],v[i]); In (m); while(m--) {scanf('%s ', ch); in (U); in (V);if(ch[1]==' H ') splay (U), tree[u].w=v,push_up (U);if(ch[1]==' M ') Cut (U,V),printf("%lld\n", TREE[V].MAXN);if(ch[1]==' S ') Cut (U,V),printf("%lld\n", tree[v].sum); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Template consolidation" LCT template