http://www.elijahqi.win/archives/855
Problem Description
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N
There are N - 1 edges numbered from 1 to N - 1.
Each node has a value and each edge has a value. The initial value is 0.
There are two kind of operation as follows:
● ADD1 u v k: for nodes on the path from u to v, the value of these nodes increase by k.
● ADD2 u v k: for edges on the path from u to v, the value of these edges increase by k.
After finished M operation on the tree, please output the value of each node and edge.
Input
The first line of the input is T (1 ≤ T ≤ 20), which stands for the number of test cases you need to solve.
The first line of each case contains two integers N ,M (1 ≤ N, M ≤105),denoting the number of nodes and operations, respectively.
The next N - 1 lines, each lines contains two integers u, v(1 ≤ u, v ≤ N ), denote there is an edge between u,v and its initial value is 0.
For the next M line, contain instructions “ADD1 u v k” or “ADD2 u v k”. (1 ≤ u, v ≤ N, -105 ≤ k ≤ 105)
Output
For each test case, print a line “Case #t:”(without quotes, t means the index of the test case) at the beginning.
The second line contains N integer which means the value of each node.
The third line contains N - 1 integer which means the value of each edge according to the input order.
Sample Input
2
4 2
1 2
2 3
2 4
ADD1 1 4 1
ADD2 3 4 2
4 2
1 2
2 3
1 4
ADD1 1 4 5
ADD2 3 2 4
Sample Output
Case #1:
1 1 0 1
0 2 2
Case #2:
5 0 0 5
0 4 0
正解应该不是这个,我是卡着时间过去的
树链剖分,然后针对节点进行差分修改
线段树估计是卡不过去的吧
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 110000
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct node{
int y,next;
}data[N<<1];
int size[N],fa[N],tp[N],dep[N],h[N],son[N],id[N],num,n,m,son1[N];
long long ans1[N],ans2[N];
void dfs1(int x){
size[x]=1;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if (fa[x]==y) continue;son1[i>>1]=y;
dep[y]=dep[x]+1;fa[y]=x ;dfs1(y);size[x]+=size[y];
if (size[y]>size[son[x]]) son[x]=y;
}
}
void dfs2(int x,int top){
id[x]=++num;tp[x]=top;
if (son[x]) dfs2(son[x],top);
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if (fa[x]==y||son[x]==y) continue;
dfs2(y,y);
}
}
int main(){
freopen("hdu5044.in","r",stdin);
int T=read(),tot=0;
while (++tot<=T){
n=read();m=read();num=1;memset(h,0,sizeof(h));memset(son,0,sizeof(son));
printf("Case #%d:\n",tot);memset(ans1,0,sizeof(ans1));memset(ans2,0,sizeof(ans2));
for (int i=1;i<n;++i){
int x=read(),y=read();
data[++num].y=y;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].next=h[y];h[y]=num;
}char str1[10];num=0;dep[1]=1;
dfs1(1);dfs2(1,1);
for (int i=1;i<=m;++i){
scanf("%s",str1);int x=read(),y=read(),z=read();
if (str1[3]=='1'){
while (tp[x]!=tp[y]){
if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
ans1[id[tp[x]]]+=z;ans1[id[x]+1]-=z;
x=fa[tp[x]];
}
if (id[x]>id[y]) swap(x,y);
ans1[id[x]]+=z;ans1[id[y]+1]-=z;
}else {
while (tp[x]!=tp[y]){
if (dep[tp[x]]<dep[tp[y]]) swap(x,y);
ans2[id[tp[x]]]+=z;ans2[id[x]+1]-=z;
x=fa[tp[x]];
}
if (id[x]>id[y]) swap(x,y);
ans2[id[x]+1]+=z;ans2[id[y]+1]-=z;
}
}
//for (int i=1;i<=n;++i) printf("%d ",id[i]);printf("\n");
for (int i=1;i<=n;++i) ans1[i]+=ans1[i-1],ans2[i]+=ans2[i-1];
for (int i=1;i<=n;++i) if (i<n) printf("%lld ",ans1[id[i]]);else printf("%lld",ans1[id[i]]);printf("\n");
for (int i=1;i<n;++i) if (i<n-1) printf("%lld ",ans2[id[son1[i]]]);else printf("%lld",ans2[id[son1[i]]]);printf("\n");
}
return 0;
}