题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5044
题意:给一棵树,在点和边上操作
题解:树链剖分,剖完后用树状数组维护即可,因为只有加减操作,连树状的部分都不用写,最后要注意当n等于1的情况
#include<cstdio>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define F(i,a,b) for(int i=a;i<=b;++i) const int N=;char op[];
int t,ic=,n,m,x,y,c,egu[N],egv[N],g[N],nxt[*N],v[*N],ed,dep[N],sz[N],fa[N],hs[N],tid[N],top[N],idx,fid[N],fid2[N];
__int64 a[N],b[N],ans1[N],ans2[N],all; inline void adg(int x,int y){v[++ed]=y,nxt[ed]=g[x],g[x]=ed;} void dfs1(int u,int pre){
sz[u]=,fa[u]=pre,hs[u]=,dep[u]=dep[pre]+;
for(int i=g[u];i;i=nxt[i]){
int vv=v[i];
if(vv!=pre){
dfs1(vv,u);
if(sz[vv]>hs[u])hs[u]=vv;
sz[u]+=sz[vv];
}
}
} void dfs2(int u,int tp){
tid[u]=++idx,fid[idx]=u,top[u]=tp;
if(hs[u])dfs2(hs[u],tp);
for(int i=g[u];i;i=nxt[i]){
int vv=v[i];
if(vv!=fa[u]&&vv!=hs[u])dfs2(vv,vv);
}
} void up(__int64*a,int x,int y,int c,int k){
int fx=top[x],fy=top[y];
while(fx!=fy){
if(dep[fx]>dep[fy])
a[tid[fx]]+=c,a[tid[x]+]-=c,x=fa[fx],fx=top[x];
else a[tid[fy]]+=c,a[tid[y]+]-=c,y=fa[fy],fy=top[y];
}
if(dep[x]>dep[y])x=x^y,y=x^y,x=x^y;
a[tid[x]+k]+=c,a[tid[y]+]-=c;
} int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
F(i,,n)g[i]=;ed=;
F(i,,n-)scanf("%d%d",egu+i,egv+i),adg(egu[i],egv[i]),adg(egv[i],egu[i]);
dfs1(,),idx=,dfs2(,);
F(i,,n)a[i]=,b[i]=;all=;
while(m--){
scanf("%s%d%d%d",op,&x,&y,&c);
if(op[]=='')up(a,x,y,c,);
else up(b,x,y,c,);
}
F(i,,n-)if(dep[egu[i]]>=dep[egv[i]])fid2[tid[egu[i]]]=i;
else fid2[tid[egv[i]]]=i;
F(i,,n)all+=a[i],ans1[fid[i]]=all;all=;
F(i,,n)all+=b[i],ans2[fid2[i]]=all;
printf("Case #%d:\n",ic++);
F(i,,n)printf("%I64d%c",ans1[i],i==n?'\n':' ');
F(i,,n-)printf("%I64d%c",ans2[i],(i==n-?'\n':' '));
if(n==)puts("");//PE
}
return ;
}