Finally the Great Magical Lamp was in Aladdin's hand. Now he wanted to return home. But he didn't want to take any help from the Genie because he thought that it might be another adventure for him. All he remembered was the paths he had taken to reach there. But since he took the lamp, all the genies in the cave became angry and they were planning to attack. As Aladdin was not afraid, he wondered how many genies were there. He summoned the Genie from the lamp and asked this.
Now you are given a similar problem. For simplicity assume that, you are given a tree (a connected graph with no cycles) with n nodes, nodes represent places, edges represent roads. In each node, initially there are an arbitrary number of genies. But the numbers of genies change in time. So, you are given a tree, the number of genies in each node and several queries of two types. They are:
1) 0 i j, it means that you have to find the total number of genies in the nodes that occur in path from node i to j (0 ≤ i, j < n).
2) 1 i v, it means that number of genies in node i is changed to v (0 ≤ i < n, 0 ≤ v ≤ 1000).
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a blank line. Next line contains an integer n (2 ≤ n ≤ 30000). The next line contains n space separated integers between 0 and 1000, denoting the number of genies in the nodes respectively. Then there are n-1 lines each containing two integers: u v (0 ≤ u, v < n, u ≠ v)meaning that there is an edge from node u and v. Assume that the edges form a valid tree. Next line contains an integer q (1 ≤ q ≤ 105) followed by q lines each containing a query as described above.
For each case, print the case number in a single line. Then for each query 0 i j, print the total number of genies in the nodes that occur in path i to j.
1
4
10 20 30 40
0 1
1 2
1 3
3
0 2 3
1 1 100
0 2 3
Case 1:
90
170
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <queue>
#define mem(p,k) memset(p,k,sizeof(p));
#define rep(a,b,c) for(int a=b;a<c;a++)
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x6fffffff
#define ll long long
using namespace std;
const int N=30010;
struct node{
int to,next;
}e[N*2];
int edge,root,id,n,q;
int first[N],siz[N],dep[N],fa[N],son[N],tp[N],c[N],d[N],w[N];
int tree[N<<2];
void add(int a,int b){
e[++edge].to=b;
e[edge].next=first[a];
first[a]=edge;
}
void dfs(int x){
siz[x]=1;son[x]=0;
for(int i=first[x]; ~i ;i = e[i].next){
if(e[i].to==fa[x])continue;
fa[e[i].to]=x;
dep[e[i].to]=dep[x]+1;
dfs(e[i].to);
if(siz[e[i].to]>siz[son[x]]){
son[x]=e[i].to;
}
siz[x]+=siz[e[i].to];
}
}
void build_tree(int x,int top){
w[x]=++id;
tp[x]=top;
if(son[x]){
build_tree(son[x],top);
}
for(int i=first[x]; ~i ;i = e[i].next){
if(e[i].to==fa[x] || e[i].to==son[x])continue;
build_tree(e[i].to,e[i].to);
}
}
void build(int l,int r,int rt){
if(l==r){
tree[rt]=d[l];return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void pushdown(int rt){
if(tree[rt]){
tree[rt<<1]+=tree[rt];
tree[rt<<1|1]+=tree[rt];
tree[rt]=0;
}
}
void update(int L,int R,int c,int l,int r,int rt){
if(L<=l && R>=r){
tree[rt]=c;return;
}
int m=(l+r)>>1;
//pushdown(rt);
if(L<=m)update(L,R,c,lson);
if(R>m) update(L,R,c,rson);
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
int query(int L,int R,int l,int r,int rt){
if(L<=l && R>=r){
return tree[rt];
}
int m=(l+r)>>1,sum=0;
//pushdown(rt);
if(L<=m) sum+=query(L,R,lson);
if(R>m) sum+=query(L,R,rson);
return sum;
}
void Query(int x,int y){
int fx=tp[x],fy=tp[y],ans=0;
while(fx!=fy){
if(dep[fx]<dep[fy]){
swap(fx,fy);swap(x,y);
}
ans+=query(w[fx],w[x],1,id,1);
x=fa[fx];
fx=tp[x];
}
if(dep[x]<dep[y]){
swap(x,y);
}
ans+=query(w[y],w[x],1,id,1);
printf("%d\n",ans);
}
void init(){
int x,y;
scanf("%d",&n);
edge=0;
id=0;
root=n/2+1;
fa[root]=dep[root]=tp[root]=0;
mem(first,-1);
for(int i=1;i<=n;i++)scanf("%d",c+i);
for(int i=1;i<n;i++){
scanf("%d%d",&x,&y);
x++;y++;
add(x,y);
add(y,x);
}
dfs(root);
build_tree(root,root);
for(int i=1;i<=n;i++){
d[w[i]]=c[i];
}
build(1,id,1);
}
void solve(){
char s[11];
int x,y,z;
scanf("%d",&q);
while(q--){
scanf("%d%d%d",&z,&x,&y);
if(z==0){
Query(x+1,y+1);
}
else{
update(w[x+1],w[x+1],y,1,id,1);
}
}
}
int main(){
int t,cur=1;
cin>>t;
while(t--){
printf("Case %d:\n",cur++);
init();
solve();
}
return 0;
}
/*
1
5
1 1 1 1 1
0 1
1 2
1 3
3 4
*/