QOJ.ac

QOJ

Time Limit: 5 s Memory Limit: 512 MB Total points: 100

#18821. Tree and Query 18

Statistics

There is a tree consisting of $N$ vertices. The vertices are numbered from $1$ to $N$, and each vertex $i$ stores an integer $A_i$. Initially, $A_i = 0$ for all $i$ ($1 \le i \le N$).

You need to process a total of $Q$ queries of the following types:

  • 1 u v: When the root of the tree is vertex $u$, add $1$ to $A_i$ for every vertex $i$ in the subtree rooted at vertex $v$.
  • 2 u v: Add $1$ to $A_i$ for every vertex $i$ on the unique path from vertex $u$ to vertex $v$.
  • 3 v: Output $\sum_{i = 1}^{N} A_i \times dist(v, i)$. Here, $dist(x, y)$ denotes the number of edges on the path from vertex $x$ to vertex $y$.

Input

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

The next $N-1$ lines describe the edges of the tree. Each line contains two space-separated integers $u$ and $v$, indicating an edge connecting $u$ and $v$ ($1 \le u, v \le N$).

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

The next $Q$ lines each contain one query in one of the following formats:

  • 1 u v ($1 \le u, v \le N$)
  • 2 u v ($1 \le u, v \le N$)
  • 3 v ($1 \le v \le N$)

At least one type‑3 query is given.

Output

For each type‑3 query, output the result on a separate line in order.

Examples

Input 1

5
4 2
2 5
1 5
1 3
5
2 2 4
3 4
2 1 5
2 5 5
3 2

Output 1

1
5

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.