#Q140. 「一本通 4.5 例 1」树的统计
「一本通 4.5 例 1」树的统计
Description
Original source: ZJOI 2008
There is a tree with nodes, numbered from to . Each node has a weight . You are required to perform the following operations on the tree:
CHANGE u t: Change the weight of node to ;QMAX u v: Query the maximum weight of nodes on the path from node to node ;QSUM u v: Query the sum of weights of nodes on the path from node to node .
Note: The nodes on the path from node to node include both and themselves.
Input Format
The first line contains an integer , representing the number of nodes;
Next, lines follow, each containing two integers and , indicating an edge between node and node ;
The following line contains integers, where the -th integer represents the weight of node ;
The next line contains an integer , representing the total number of operations;
Subsequent lines each describe an operation in the form of CHANGE u t, QMAX u v, or QSUM u v.
Output Format
For each QMAX or QSUM operation, output an integer representing the result.
Sample 1
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
4
1
2
2
10
6
5
6
5
16
Constraints & Hints
For of the data, , . It is guaranteed that during the operations, each node's weight is between and .