#ds901. 树的统计

树的统计

Tree Statistics

Problem Source

ZJOI 2008

Problem Description

There is a tree with nn nodes numbered 11 to nn, each node carrying a weight ww. You must support the following operations on the tree:

  1. CHANGE u t: change the weight of node uu to tt.
  2. QMAX u v: query the maximum weight among the nodes on the path from uu to vv.
  3. QSUM u v: query the sum of weights of the nodes on the path from uu to vv. (The path from uu to vv includes both uu and vv themselves.)

Input Format

The first line of the input contains an integer nn, the number of nodes.

The next n−1n-1 lines each contain two integers aa and bb, indicating that there is an edge between node aa and node bb.

The next nn lines each contain one integer; the ii-th line contains wiw_i, the weight of node ii.

Then one line contains an integer qq, the total number of operations.

The next qq 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 ww remains between −3×104-3 \times 10^4 and 3×1043 \times 10^4.

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

Constraints

  • 1≤n≤3×1041 \leq n \leq 3 \times 10^4
  • 0≤q≤2×1040 \leq q \leq 2 \times 10^4
  • −3×104≤w≤3×104-3 \times 10^4 \leq w \leq 3 \times 10^4