395 传统题

[ABC329F] 彩球盒子合并 (Colored Ball)

时间限制
4000ms
内存限制
1024MiB
登顶 / 尝试
0/ 0
难度
5
    上传者 标签AtCoderABC329提高-启发式合并集合

[ABC329F] 彩球盒子合并 (Colored Ball)

Score : 500500 points

Problem Statement

There are NN boxes numbered 1,2,…,N1, 2, \ldots, N. Initially, box ii contains one ball of color CiC_i.

You are given QQ queries, which you should process in order.

Each query is given by a pair of integers (a,b)(a,b) and asks you to do the following:

  • Move all the balls from box aa to box bb, and then print the number of different colors of balls in box bb.

Here, the boxes aa and bb may be empty.

Constraints

  • 1≤N,Q≤2000001 \leq N, Q \leq 200000

  • 1≤Ci≤N1 \leq C_i \leq N

  • 1≤a,b≤N1 \leq a, b \leq N

  • a≠ba \neq b

  • All input values are integers.

Input

The input is given from Standard Input in the following format, where queryi\text{query}_i represents the ii-th query:

$N$ $Q$
$C_1$ $C_2$ $\ldots$ $C_N$
$\text{query}_1$
$\text{query}_2$
$\vdots$
$\text{query}_Q$

Each query is given in the following format:

$a$ $b$

Output

Print QQ lines. The ii-th line should contain the response to the ii-th query.

Sample Input 1

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

Sample Output 1

1
2
1
1
3
  • For the first query, move all the balls from box 11 to box 22. Box 22 now contains two balls of color 11, so print 11.

  • For the second query, move all the balls from box 66 to box 44. Box 44 now contains one ball of color 22 and one ball of color 33, so print 22.

  • For the third query, move all the balls from box 55 to box 11. Box 11 now contains one ball of color 22, so print 11.

  • For the fourth query, move all the balls from box 33 to box 66. Box 66 now contains one ball of color 11, so print 11.

  • For the fifth query, move all the balls from box 44 to box 66. Box 66 now contains one ball of color 11, one ball of color 22, and one ball of color 33, so print 33.

Sample Input 2

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

Sample Output 2

1
2
0