Under construction

Still being built. Things may change

Tree and Queries 13

Time limit3sMemory limit512 MB

Summary
On a tree whose root and parent links change, process 12 query types that assign or add to subtrees and paths, and report min, max, or sum over them.
Level

Hard9 of 10

Topics
Tree, Segment tree, DFS
Solved
No attempts yet

Problem

A rooted tree with NN vertices is given. The vertices are numbered from 1 to NN. The weight of vertex ii is AiA_i. Initially, vertex rr is the root.

Write a program that processes the following queries.

  • 0 x y: Change the weights of all vertices in the subtree of xx to yy.
  • 1 r: Change the root of the tree to rr.
  • 2 x y z: Change the weights of all vertices on the path between xx and yy to zz.
  • 3 x: Print the minimum weight among the vertices in the subtree of xx.
  • 4 x: Print the maximum weight among the vertices in the subtree of xx.
  • 5 x y: Add yy to the weights of all vertices in the subtree of xx.
  • 6 x y z: Add zz to the weights of all vertices on the path between xx and yy.
  • 7 x y: Print the minimum weight among the vertices on the path between xx and yy.
  • 8 x y: Print the maximum weight among the vertices on the path between xx and yy.
  • 9 x y: Change the parent of xx to yy. If vertex yy is in the subtree of xx, ignore this query.
  • 10 x y: Print the sum of the weights of the vertices on the path between xx and yy.
  • 11 x: Print the sum of the weights of the vertices in the subtree of xx.

Input

The first line contains two integers NN and MM (1≤N,M≤1051 \le N, M \le 10^5).

Each of the next N−1N-1 lines contains two integers uu and vv (1≤u,v≤N1 \le u, v \le N), the endpoints of an edge.

Each of the next NN lines contains the weight AiA_i of vertex ii.

The next line contains the initial root rr (1≤r≤N1 \le r \le N).

The next MM lines contain the queries described above.

All integers in the input fit in the C++ int type. The input is given so that the sum of the weights of all vertices never exceeds the int range while the queries are processed.

Output

For each query, print its result on its own line, in the order the queries are given.

Examples2

  1. Example 1

    Input
    5 5
    2 1
    3 1
    4 1
    5 2
    4
    1
    4
    1
    2
    1
    10 2 3
    3 1
    7 3 4
    6 3 3 2
    9 5 1
    
    Expected output
    9
    1
    1
    
  2. Example 2

    Input
    10 12
    2 1
    3 2
    4 2
    5 3
    6 4
    7 5
    8 2
    9 4
    10 9
    791
    868
    505
    658
    860
    623
    393
    717
    410
    173
    4
    0 8 800
    1 4
    2 8 2 103
    3 9
    4 4
    5 7 304
    6 8 8 410
    7 10 8
    8 1 8
    9 6 9
    10 2 3
    11 5
    
    Expected output
    173
    860
    103
    791
    608
    1557