Under construction

Still being built. Things may change

Tree and Queries 18

Time limit5sMemory limit512 MB

Summary
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 NN vertices. The vertices are numbered from 1 to NN, and vertex ii stores an integer AiA_i. Initially, every AiA_i is 0. (1≤i≤N1 \le i \le N)

You must perform a total of QQ queries of the following forms.

  • 1 u v: Root the tree at vertex uu. Add 1 to AiA_i for every vertex ii in the subtree rooted at vertex vv.
  • 2 u v: Add 1 to AiA_i for every vertex ii on the unique path from vertex uu to vertex vv.
  • 3 v: Print ∑i=1NAi×dist(v,i)\sum_{i=1}^{N} A_i \times dist(v, i). dist(x,y)dist(x, y) is the number of edges on the path from vertex xx to vertex yy.

Input

The first line contains NN. (1≤N≤200,0001 \le N \le 200{,}000)

Each of the next N−1N-1 lines contains two integers uu and vv, separated by a space, meaning there is an edge between uu and vv. (1≤u,v≤N1 \le u, v \le N)

The next line contains QQ, the number of queries. (1≤Q≤200,0001 \le Q \le 200{,}000)

Each of the next QQ lines contains one query in one of the formats above. (1≤u,v≤N1 \le u, v \le N)

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.

Examples1

  1. Example 1

    Input
    5
    4 2
    2 5
    1 5
    1 3
    5
    2 2 4
    3 4
    2 1 5
    2 5 5
    3 2
    
    Expected output
    1
    5