QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#13832. Tree Statistics

Statistics

There is a tree with $n$ nodes, numbered from $1$ to $n$. Each node has a weight $w$.

We will ask you to perform the following operations on this tree:

I. CHANGE u t: Change the weight of node $u$ to $t$. II. QMAX u v: Query the maximum weight among the nodes on the path between node $u$ and node $v$. III. QSUM u v: Query the sum of the weights of the nodes on the path between node $u$ and node $v$.

Note: The path between node $u$ and node $v$ includes $u$ and $v$ themselves.

Input

The first line of the input contains an integer $n$, representing the number of nodes.

The next $n-1$ lines each contain two integers $a$ and $b$, representing an edge between node $a$ and node $b$.

The next $n$ lines each contain an integer, where the integer on the $i$-th line represents the weight $w_i$ of node $i$.

The next line contains an integer $q$, representing the total number of operations.

The next $q$ lines each contain an operation in the form of CHANGE u t, QMAX u v, or QSUM u v.

For $100\%$ of the data, $1 \le n \le 30000$ and $0 \le q \le 200000$. During the operations, the weight $w$ of each node is guaranteed to be between $-30000$ and $30000$.

Output

For each QMAX or QSUM operation, output the result on a new line.

Examples

Input 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

Output 1

4
1
2
2
10
6
5
6
5
16

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.