Tree and Queries 18
Time limit5sMemory limit512 MB
Given a tree, support adding 1 to a rerooted subtree, adding 1 along a path, and querying the sum of A_i times dist(v, i).
- Level
Hard8 of 10
- Topics
- Tree, Segment tree, DFS
- Solved
- No attempts yet
Problem
There is a tree with vertices. The vertices are numbered from 1 to , and vertex stores an integer . Initially, every is 0. ()
You must perform a total of queries of the following forms.
1 u v: Root the tree at vertex . Add 1 to for every vertex in the subtree rooted at vertex .2 u v: Add 1 to for every vertex on the unique path from vertex to vertex .3 v: Print . is the number of edges on the path from vertex to vertex .
Input
The first line contains . ()
Each of the next lines contains two integers and , separated by a space, meaning there is an edge between and . ()
The next line contains , the number of queries. ()
Each of the next lines contains one query in one of the formats above. ()
At least one query of type 3 is given.
Output
For each query of type 3, print the result on its own line, in the order the queries are given.