Constructs a point tree that records the size of each point's governing subtree VC, which governs the cost of all points to this point within the subtree and the Cos and the subtree contribution to the cost of the upper center of gravity SCOs
Modify the direct point tree on each modification
Query a point, if the size of the maximum subtree of the point is greater than half of the full image, the point in addition to the largest sub-tree part of the sub-tree to paste the corresponding point and then recursive return query that subtree
If the maximum subtree is less than half the full figure, then this point is the center of gravity of the current graph, and the answer is COS
Code:
#include <bits/stdc++.h>#definePA pair<int,int>#defineINF 0x3f3f3f3f#defineLL Long Longusing namespaceStd;inlineint_max (intAintb) {returnA>b?a:b;}#defineMAXN 100005intn,m;inthead[maxn],cnt;structedge{intNext,to,w;} E[MAXN<<1];inlinevoidInsertintAintBintc) {e[++cnt].next=head[a];head[a]=cnt;e[cnt].to=b;e[cnt].w=C; e[++cnt].next=head[b];head[b]=cnt;e[cnt].to=a;e[cnt].w=C;}BOOLVIS[MAXN];intsz[maxn],weight[2];voidFINDW (intVintAll ) {Vis[v]=1; sz[v]=0;intmx=0; for(intI=head[v];i;i=E[i].next) { if(!Vis[e[i].to]) {FINDW (E[i].to,all); MX=_max (mx,sz[e[i].to]); sz[v]+=Sz[e[i].to]; }} Sz[v]++;mx=_max (mx,all-Sz[v]); if(weight[0]>MX) weight[0]=mx,weight[1]=v; VIS[V]=0;}intanc[maxn][ -],LEN[MAXN],TO[MAXN],TDIS[MAXN]; LL dis[maxn][ -];voidComp (intVintAncintd) {Vis[v]=1; sz[v]=0; anc[v][++len[v]]=anc;dis[v][len[v]]=D; for(intI=head[v];i;i=E[i].next) { if(!Vis[e[i].to]) {Comp (e[i].to,anc,d+E[I].W); SZ[V]+=Sz[e[i].to]; }} Sz[v]++;vis[v]=0;}voidSolve (intW) {vis[w]=1;if(sz[w]==1)return; for(intI=head[w];i;i=E[i].next) { if(!Vis[e[i].to]) {weight[0]=INF; FINDW (e[i].to,sz[e[i].to]); to[weight[1]]=e[i].to; tdis[weight[1]]=E[I].W; Comp (weight[1],weight[1],0); Solve (weight[1]); }}}ll vc[maxn],cos[maxn],scos[maxn],all;inlinevoidModify (intXinty) { all+=y; for(inti=len[x];i;i--) {Vc[anc[x][i]]+=y; Cos[anc[x][i]]+=y*Dis[x][i]; Scos[anc[x][i]]+=y*dis[x][i-1]; }}voidChange (intXintSp,ll V,ll C,intFX) { for(inti=len[x];i;i--) { if(ANC[X][I]==SP)return; Vc[anc[x][i]]+=v; Cos[anc[x][i]]+=v* (DIS[X][I]+FX) +C; Scos[anc[x][i]]+=v* (dis[x][i-1]+FX) +C; }}ll Query (intVintd) {LL ans,go,s=0; for(intI=head[v];i;i=e[i].next)if(anc[e[i].to][d]&&vc[anc[e[i].to][d]]>s) s=vc[anc[e[i].to][d]],go=Anc[e[i].to][d]; if((s<<1) >All ) {LL T=vc[v]-vc[go],c=cos[v]-Scos[go]; Change (To[go],v,t,c,tdis[go]); Ans=query (go,d+1); Change (To[go],v,-t,-C,tdis[go]); returnans; } Else returncos[v];}intMain () {scanf ("%d%d",&n,&m); for(intA,b,c,i=1; i<n;i++) {scanf ("%d%d%d",&a,&b,&c); Insert (A,B,C); } weight[0]=inf; FINDW (1, N); Comp (weight[1],weight[1],0); Solve (weight[1]); for(intX,y,i=1; i<=m;i++) {scanf ("%d%d",&x,&y); Modify (x, y); printf ("%lld\n", Query (anc[1][1],2)); } return 0;}
BZOJ3924--FJOI2015 Fantasy Township Strategy game