#abc329f. F - Colored Ball

F - Colored Ball

Score : 500500 points

问题描述

NN 个编号为 1,2,,N1, 2, \ldots, N 的盒子。最初,第 ii 个盒子包含一个颜色为 CiC_i 的球。

你将收到 QQ 个查询请求,并需要按照顺序处理它们。

每个查询由一对整数 (a,b)(a,b) 组成,要求你执行以下操作:

  • 将盒子 aa 中的所有球移动到盒子 bb,然后打印出盒子 bb 中不同颜色球的数量。

注意,盒子 aabb 可能是空的。

以上为通义千问 qwen-max 翻译,仅供参考。

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

  • 1N,Q2000001 \leq N, Q \leq 200000
  • 1CiN1 \leq C_i \leq N
  • 1a,bN1 \leq a, b \leq N
  • aba \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:

NN QQ

C1C_1 C2C_2 \ldots CNC_N

query1\text{query}_1

query2\text{query}_2

\vdots

queryQ\text{query}_Q

Each query is given in the following format:

aa bb

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

update @ 2024/3/10 01:54:41