Title Address: POJ 2763
or a tree-chain template problem ... Not much to say.
The code is as follows:
#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace STD;#define LL Long Long#define PI ACOs ( -1.0)#pragma COMMENT (linker, "/stack:1024000000,1024000000")Const intMod=1e9+7;Const intinf=0x3f3f3f3f;Const Doubleeqs=1e-3;Const intmaxn=100000+Ten;#define Root 1, N, 1#define Lson L, Mid, rt<<1#define Rson mid+1, R, Rt<<1|1intN, Head[maxn], CNT, tot;intDEP[MAXN], FA[MAXN], SON[MAXN], SIZ[MAXN], W[MAXN], TOP[MAXN];intsum[maxn<<2];structnode{intU, V, W, next;} edge[maxn<<1];voidAddintUintVintW) {edge[cnt].u=u; Edge[cnt].v=v; Edge[cnt].w=w; Edge[cnt].next=head[u]; head[u]=cnt++;}voidInit () {memset(head,-1,sizeof(head)); Cnt=0;memset(DEP,0,sizeof(DEP));memset(Son,0,sizeof(son));memset(Sum,0,sizeof(sum)); tot=0;}voidDFS1 (intUintP) {siz[u]=1; for(inti=head[u];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(v==p)Continue; Fa[v]=u; dep[v]=dep[u]+1; DFS1 (V,u); SIZ[U]+=SIZ[V];if(Siz[son[u]]<siz[v]) son[u]=v; }}voidDFS2 (intUintTP) {W[U]=++TOT;TOP[U]=TP;if(Son[u]) DFS2 (SON[U],TP); for(inti=head[u];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(V!=son[u]&&v!=fa[u]) {DFS2 (v,v); } }}structline_tree{voidPushup (intRT) {sum[rt]=sum[rt<<1]+sum[rt<<1|1]; }voidUpdate (intPintXintLintRintRT) {if(L==R) {sum[rt]=x;return; }intMid=l+r>>1;if(P<=mid) Update (P,x,lson);ElseUpdate (P,x,rson); Pushup (RT); }intQuery (intllintRrintLintRintRT) {if(LL<=L&&RR>=R) {returnSUM[RT]; }intMid=l+r>>1, ans=0;if(Ll<=mid) Ans+=query (Ll,rr,lson);if(Rr>mid) Ans+=query (Ll,rr,rson);returnAns }}lt;intSolveintUintV) {intF1=top[u], F2=top[v], ans=0; while(F1!=F2) {if(Dep[f1]<dep[f2]) {swap (U,V); Swap (F1,F2); } ans+=lt. Query (W[f1],w[u],root); U=fa[f1];f1=top[u]; }if(U==V)returnAnsif(Dep[u]<dep[v]) {swap (U,V); }returnAns+lt. Query (w[son[v]],w[u],root);}intMain () {intQ, S, u, V, c, I, X; while(scanf("%d%d%d", &n,&q,&s)!=eof) {init (); for(i=1; i<n;i++) {scanf("%d%d%d", &u,&v,&c); Add (U,V,C); Add (V,U,C); } DFS1 (1,-1); DFS2 (1,1); for(i=0; i<cnt;i+=2) {u=edge[i].u; V=EDGE[I].V;if(Dep[u]<dep[v]) swap (U,V); Lt. Update (W[u],edge[i].w,root); } while(q--) {scanf("%d", &x);if(!x) {scanf("%d", &v);printf("%d\n", Solve (s,v)); S=v; }Else{scanf("%d%d", &x,&c); u=edge[x-1<<1].u; v=edge[x-1<<1].V;if(Dep[u]<dep[v]) swap (U,V); Lt. Update (W[u],c,root); } } }return 0;}
POJ 2763 Housewife Wind (tree-chain split)