Tree and Queries 13
Time limit3sMemory limit512 MB
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 vertices is given. The vertices are numbered from 1 to . The weight of vertex is . Initially, vertex is the root.
Write a program that processes the following queries.
0 x y: Change the weights of all vertices in the subtree of to .1 r: Change the root of the tree to .2 x y z: Change the weights of all vertices on the path between and to .3 x: Print the minimum weight among the vertices in the subtree of .4 x: Print the maximum weight among the vertices in the subtree of .5 x y: Add to the weights of all vertices in the subtree of .6 x y z: Add to the weights of all vertices on the path between and .7 x y: Print the minimum weight among the vertices on the path between and .8 x y: Print the maximum weight among the vertices on the path between and .9 x y: Change the parent of to . If vertex is in the subtree of , ignore this query.10 x y: Print the sum of the weights of the vertices on the path between and .11 x: Print the sum of the weights of the vertices in the subtree of .
Input
The first line contains two integers and ().
Each of the next lines contains two integers and (), the endpoints of an edge.
Each of the next lines contains the weight of vertex .
The next line contains the initial root ().
The next 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.