#include <iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<map>#include<Set>#include<vector>#include<cstdio>using namespacestd;Const intn=50010;structedge{intTo,next;} Eg[n*2];intHead[n],tot,top[n],fa[n],deep[n],num[n],p[n],fp[n],son[n],pos;voidinit () {tot=0; memset (Head,-1,sizeof(head)); POS=1; memset (son,-1,sizeof(son));voidAddedge (intUintv) {eg[tot].to=v; Eg[tot].next=Head[u]; Head[u]=tot++;}voidDFS1 (intUintPreintd) {Deep[u]=D; Fa[u]=Pre; Num[u]=1; for(inti=head[u];i!=-1; i=Eg[i].next) { intv=eg[i].to; if(v!=pre) {DFS1 (v,u,d+1); Num[u]+=Num[v]; if(son[u]==-1|| Num[v]>num[son[u]]) son[u]=v; } }}voidGetPosintUintsp) {Top[u]=sp; P[u]=pos++; Fp[p[u]]=u; if(son[u]==-1)return ; GetPos (SON[U],SP); for(inti=head[u];i!=-1; i=Eg[i].next) { intv=eg[i].to; if(v!=son[u]&&v!=Fa[u]) GetPos (V,V); }}intLowbit (intx) { returnx& (-x);}intC[n],n;intSuminti) { ints=0; while(i>0) {s+=B[i]; I-=lowbit (i); } returns;}voidAddintIintval) { while(i<=N) {C[i]+=Val; I+=lowbit (i); }}voidChangeintUintVintval) { intf1=top[u],f2=top[v],tmp=0; while(f1!=F2) { if(deep[f1]<Deep[f2]) {swap (F1,F2); Swap (U,V); } add (P[f1],val); Add (P[u]+1,-val); U=FA[F1]; F1=Top[u]; } if(deep[u]>Deep[v]) swap (U,V); Add (P[u],val); Add (P[v]+1,-val);}intA[n];intMain () {//freopen ("In.txt", "R", stdin); intm,p; while(~SCANF ("%d%d%d",&n,&m,&P)) {intu,v,c1,c2,k; Charop[Ten]; Init (); for(intI=1; i<=n;i++) scanf ("%d",&A[i]); while(m--) {scanf ("%d%d",&u,&v); Addedge (U,V); Addedge (V,u); } DFS1 (1,0,0); GetPos (1,1); Memset (c,0,sizeof(c)); for(intI=1; i<=n;i++) {Add (P[i],a[i]); Add (P[i]+1,-A[i]); } while(p--) {scanf ("%s", op); if(op[0]=='Q') {scanf ("%d",&T); printf ("%d\n", SUM (P[u])); } Else{scanf ("%d%d%d",&c1,&c2,&k); if(op[0]=='D') k=-K; Change (C1,C2,K); } } } return 0;}
HDU3966 Aragorn's Story (tree chain-right template)