QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 512 MB Puntuación total: 100

#9085. This is my own invention

Estadísticas

Given a tree with $n$ nodes, each having a weight, the initial root is $1$.

There are $m$ operations of the following types:

1 x: Change the root of the tree to $x$.

2 x y: Given two nodes $x$ and $y$, consider every node in the subtree of $x$ and every node in the subtree of $y$. Calculate the number of pairs of nodes (one from each subtree) that have equal weights.

Input

The first line contains two integers $n$ and $m$.

The second line contains $n$ integers, representing the weight $a_i$ of each node.

The next $n-1$ lines each contain two integers $x$ and $y$, representing an edge.

The next $m$ lines each represent an operation.

Output

For each query, output a single integer representing the answer.

Examples

Input 1

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

Output 1

0
1
1
1

Subtasks

Idea: nzhtl1477, Solution: nzhtl1477, Code: nzhtl1477, Data: nzhtl1477

For $100\%$ of the data, $1 \le n \le 10^5$, $1 \le m \le 5 \times 10^5$, $1 \le a_i \le 10^9$.

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.