#P2590. [ZJOI2008] 树的统计

    ID: 1603 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2008线段树各省省选浙江树链剖分,树剖

[ZJOI2008] 树的统计

Description

There is a tree with nn nodes, numbered from 11 to nn, and each node has a weight ww.

You are asked to perform the following operations on the tree:

I. CHANGE u t: Set the weight of node uu to tt.

II. QMAX u v: Query the maximum weight among the nodes on the path from uu to vv.

III. QSUM u v: Query the sum of the weights of the nodes on the path from uu to vv.

Note: The nodes on the path from uu to vv include uu and vv themselves.

Input Format

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

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

The next line contains nn integers, where the ii-th integer wiw_i is the weight of node ii.

The next line contains an integer qq, the total number of operations.

The next qq lines each contain one operation, given as CHANGE u t, QMAX u v, or QSUM u v.

Output Format

For each QMAX or QSUM operation, output one integer on its own line representing the result.

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

Hint

Constraints:

For 100%100\% of the testdata, it is guaranteed that 1n3×1041 \le n \le 3\times 10^4 and 0q2×1050 \le q \le 2\times 10^5.

Throughout the operations, each node’s weight ww is guaranteed to be between 3×104-3\times 10^4 and 3×1043\times 10^4.

Translated by ChatGPT 5