Jamie and Tree
Contest: Codeforces Round 457 (Div. 2)
Difficulty rating: 2400
Tags: data structures, trees

To your surprise, Jamie is the final boss! Ehehehe.

Jamie has given you a tree with n vertices, numbered from 1 to n. Initially, the root of the tree is the vertex with number 1. Also, each vertex has a value on it.

Jamie also gives you three types of queries on the tree:

1 v — Change the tree's root to vertex with number v.

2 u v x — For each vertex in the subtree of smallest size that contains u and v, add x to its value.

3 v — Find sum of values of vertices in the subtree of vertex with number v.

A subtree of vertex v is a set of vertices such that v lies on shortest path from this vertex to root of the tree. Pay attention that subtree of a vertex can change after changing the tree's root.

Show your strength in programming to Jamie by performing the queries accurately!

Input format:
The first line of input contains two space-separated integers n and q (1 ≤ n ≤ 105, 1 ≤ q ≤ 105) — the number of vertices in the tree and the number of queries to process respectively.

The second line contains n space-separated integers a1, a2, ..., an ( - 108 ≤ ai ≤ 108) — initial values of the vertices.

Next n - 1 lines contains two space-separated integers ui, vi (1 ≤ ui, vi ≤ n) describing edge between vertices ui and vi in the tree.

The following q lines describe the queries.

Each query has one of following formats depending on its type:

1 v (1 ≤ v ≤ n) for queries of the first type.

2 u v x (1 ≤ u, v ≤ n,  - 108 ≤ x ≤ 108) for queries of the second type.

3 v (1 ≤ v ≤ n) for queries of the third type.

All numbers in queries' descriptions are integers.

The queries must be carried out in the given order. It is guaranteed that the tree is valid.

Output format:
For each query of the third type, output the required answer. It is guaranteed that at least one query of the third type is given by Jamie.

Constraints:
Time limit: 2.5 s
Memory limit: 256.0 MB

Note:
The following picture shows how the tree varies after the queries in the first sample.

Example input:
6 7
1 4 2 8 5 7
1 2
3 1
4 3
4 5
3 6
3 1
2 4 6 3
3 4
1 6
2 2 4 -5
1 4
3 3

Example output:
27
19
5

Example input:
4 6
4 3 5 6
1 2
2 3
3 4
3 1
1 3
2 2 4 3
1 1
2 2 4 -3
3 1

Example output:
18
21

Editorial:
Precalculate all things needed to query LCA when the root is vertex 1, and we won't calculate them again after the root is changed (always let the 'real' root be vertex 1 but we keep the value of the current root)
For operation 2, we need to find the 'origin' of the update (the LCA). Let the two nodes be u and v, the current root be r, there are 3 cases:
1. both u and v are in r's subtree (use the precalculated table to find LCA)
2. exactly 1 of u or v is in r's subtree (LCA is r)
3. otherwise (for both u and v, find the lowest ancestor of it such that it is also r's ancestor (binary search may be needed). Let them be p and q respectively, if p! = q the deeper one from vertex 1 is the LCA. Otherwise we should use the precalculated table to find the LCA directly)
For operation 3, the 'origin' is given.
To actually do the update/query there are 4 cases too: (let the 'origin' be x)
1. x = r (update/query the whole tree)
2. x is in the subtree of r (update/query the subtree of x)
3. x is one of r's ancestors (update/query the whole tree, and undo update/query on the subtree that a children of x is the root and the subtree contains r (you can find this by finding the distance between x and r, and the required vertex is the (dist - 1)th ancestor of r)
4. otherwise (update/query the subtree of x)