#ds901. 树的统计
树的统计
Tree Statistics
Problem Source
ZJOI 2008
Problem Description
There is a tree with nodes numbered to , each node carrying a weight . You must support the following operations on the tree:
CHANGE u t: change the weight of node to .QMAX u v: query the maximum weight among the nodes on the path from to .QSUM u v: query the sum of weights of the nodes on the path from to . (The path from to includes both and themselves.)
Input Format
The first line of the input contains an integer , the number of nodes.
The next lines each contain two integers and , indicating that there is an edge between node and node .
The next lines each contain one integer; the -th line contains , the weight of node .
Then one line contains an integer , the total number of operations.
The next lines each describe one operation, given in the form CHANGE u t, QMAX u v, or QSUM u v.
It is guaranteed that $1 \leq n \leq 3 \times 10^4,\ 0 \leq q \leq 2 \times 10^4$, and that during the operations every node weight remains between and .
Output Format
For each QMAX or QSUM operation, output one integer per line: the requested result.
Sample Input
4
1 2
2 3
4 1
4 2 1 3
12
QMAX 3 4
QMAX 3 3
QMAX 3 2
QMAX 2 3
QSUM 3 4
QSUM 2 1
CHANGE 1 5
QMAX 3 4
CHANGE 3 6
QMAX 3 4
QMAX 2 4
QSUM 3 4
Sample Output
4
1
2
2
10
6
5
6
5
16