Split Edge update, Segment tree point update Interval to find the extremum. Learn the tree chain to feel or do a little more fine, rather than edge update. In fact, the most important is also used to maintain the data structure, the split is basically a template.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineLson l,m,rt<<16 #defineRson m+1,r,rt<<1|17 Const intMAXN =10005;8 Const intINF =0x3f3f3f3f;9 intN;Ten intSIZ[MAXN],FA[MAXN],SON[MAXN],DEP[MAXN]; One inttop[maxn],tid[maxn],lable; A inthead[maxn],cnt; - structEdge - { the intV,w,en,next; -}e[maxn*2]; - intsum[maxn<<2]; - voidInit () + { -memset (head,-1,sizeof(head)); +CNT = lable =0; A } at voidAddintUintVintW) - { -E[CNT].V =v; -E[CNT].W =W; -E[cnt].next =Head[u]; -Head[u] = cnt++; in } - voidFind_heavy (intRtintFatherintdepth) to { +FA[RT] =father; -SON[RT] =0; theSIZ[RT] =1; *DEP[RT] =depth; $ intMaxSize =0;Panax Notoginseng for(inti = head[rt];i!=-1; i = e[i].next)if(e[i].v!=father) - { theE[i].en = e[i^1].en =e[i].v; +Find_heavy (e[i].v,rt,depth+1); Asiz[rt]+=SIZ[E[I].V]; the if(siz[e[i].v]>maxsize) +MaxSize = Siz[e[i].v],son[rt] =e[i].v; - } $ } $ voidPushup (intRT) - { -SUM[RT] = max (sum[rt<<1],sum[rt<<1|1]); the } - voidUpdateintPosintValintLintRintRT)Wuyi { the if(l==R) { -SUM[RT] =Val; Wu return; - } About intm = (l+r) >>1; $ if(pos<=m) update (Pos,val,lson); - ElseUpdate (Pos,val,rson); - pushup (RT); - } A voidConnectintRtintANC) + { theTID[RT] = + +lable; -TOP[RT] =ANC; $ if(Son[rt]) connect (SON[RT],ANC); the for(intI= head[rt];i!=-1; i =e[i].next) the if(e[i].v!=fa[rt]&&e[i].v!=Son[rt]) the Connect (E[I].V,E[I].V); the } - intQueryintLintRintLintRintRT) in { the if(L<=L&&R<=R)returnSum[rt]; the intm = (l+r) >>1, ret =-INF; About if(l<=m) ret =Max (Ret,query (L,r,lson)); the if(m<r) ret =Max (Ret,query (L,r,rson)); the returnret; the } + intGetmax (intXinty) - { the intAns =-INF;Bayi while(top[x]!=Top[y]) the { the if(dep[top[x]]<Dep[top[y]]) swap (x, y); -ans = max (Ans,query (tid[top[x]],tid[x),1N1)); -x =Fa[top[x]]; the } the if(x==y)returnans; the if(dep[x]>Dep[y]) swap (x, y); theans = max (Ans,query (tid[son[x]],tid[y),1N1)); - returnans; the } the intMain () the {94 //freopen ("OUT.txt", "R", stdin); the intT;SCANF ("%d",&T); the while(t--) the {98scanf"%d",&n); About init (); - for(inti =1; i<n;++i)101 {102 intu,v,w;103scanf"%d%d%d",&u,&v,&W);104 Add (u,v,w); the Add (v,u,w);106 }107Find_heavy (1,1,1);108Connect1,1);109 the for(inti =0; i<cnt;i+=2) Update (TID[E[I].EN],E[I].W,1N1);111Update1,0,1N1); the while(1)113 { the Chars[Ten];SCANF ("%s", s); the if(s[0]=='D') Break; the intu,v;117scanf"%d%d",&u,&v);118 if(s[0]=='Q') printf ("%d\n", Getmax (u,v));119 ElseUpdate (tid[e[u*2-1].en],v,1N1); - }121 }122 return 0;123}
SPOJ375 Query on a tree classic introductory question