QOJ.ac

QOJ

Limite de temps : 3 s Limite de mémoire : 500 MB Points totaux : 100

#7474. Panicked SCOI2016

Statistiques

You are given a tree with $n$ nodes, where each node has a color. There are $m$ updates. After each update, you need to output the sum of the number of distinct colors on all directed simple paths in the tree.

Input

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

The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq n$), where $c_i$ is the initial color of node $i$.

The next $n-1$ lines each contain two integers $u, v$ ($1 \leq u, v \leq n$), representing an edge between $u$ and $v$.

The following $m$ lines each contain two integers $u, x$ ($1 \leq u, x \leq n$), indicating that the color of node $u$ is changed to $x$.

Output

Output $m+1$ lines, each containing an integer representing the sum of the number of distinct colors on all directed simple paths in the tree, initially and after each update.

Constraints

For $100\%$ of the data, $2 \leq n \leq 4 \times 10^5$ and $1 \leq m \leq 4 \times 10^5$.

Examples

Input 1

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

Output 1

47
51
49
45

Input 2

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

Output 2

36
46

Note

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

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.