CF1380E.Merging Towers

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You have a set of nn discs, the ii-th disc has radius ii. Initially, these discs are split among mm towers: each tower contains at least one disc, and the discs in each tower are sorted in descending order of their radii from bottom to top.

You would like to assemble one tower containing all of those discs. To do so, you may choose two different towers ii and jj (each containing at least one disc), take several (possibly all) top discs from the tower ii and put them on top of the tower jj in the same order, as long as the top disc of tower jj is bigger than each of the discs you move. You may perform this operation any number of times.

For example, if you have two towers containing discs [6,4,2,1][6, 4, 2, 1] and [8,7,5,3][8, 7, 5, 3] (in order from bottom to top), there are only two possible operations:

  • move disc 11 from the first tower to the second tower, so the towers are [6,4,2][6, 4, 2] and [8,7,5,3,1][8, 7, 5, 3, 1];
  • move discs [2,1][2, 1] from the first tower to the second tower, so the towers are [6,4][6, 4] and [8,7,5,3,2,1][8, 7, 5, 3, 2, 1].

Let the difficulty of some set of towers be the minimum number of operations required to assemble one tower containing all of the discs. For example, the difficulty of the set of towers [[3,1],[2]][[3, 1], [2]] is 22: you may move the disc 11 to the second tower, and then move both discs from the second tower to the first tower.

You are given m−1m - 1 queries. Each query is denoted by two numbers aia_i and bib_i, and means "merge the towers aia_i and bib_i" (that is, take all discs from these two towers and assemble a new tower containing all of them in descending order of their radii from top to bottom). The resulting tower gets index aia_i.

For each k∈[0,m−1]k \in [0, m - 1], calculate the difficulty of the set of towers after the first kk queries are performed.

你有一组 nn 个圆盘,其中第 ii 个圆盘的半径为 ii。初始时,这些圆盘被分配在 mm 座塔中:每座塔至少包含一个圆盘,且每座塔内的圆盘按半径从下到上严格递减排列。

你希望将所有圆盘组装成一座塔。为此,你可以执行如下操作:任选两座不同的塔 ii 和 jj(每座塔均至少含一个圆盘),从塔 ii 的顶部取出若干(可能全部)圆盘,并以相同顺序叠放在塔 jj 的顶部;该操作合法当且仅当塔 jj 当前顶部圆盘的半径严格大于你所移动的每一个圆盘的半径。你可以执行该操作任意多次。

例如,若你有两座塔,分别包含圆盘 [6,4,2,1][6, 4, 2, 1] 和 [8,7,5,3][8, 7, 5, 3](均按从下到上顺序列出),则仅有两种合法操作:

  • 将第一座塔顶部的圆盘 11 移至第二座塔,得到塔 [6,4,2][6, 4, 2] 和 [8,7,5,3,1][8, 7, 5, 3, 1];
  • 将第一座塔顶部的圆盘 [2,1][2, 1] 移至第二座塔,得到塔 [6,4][6, 4] 和 [8,7,5,3,2,1][8, 7, 5, 3, 2, 1]。

定义某组塔的难度为:将其合并为一座包含全部圆盘的塔所需的最少操作次数。例如,塔集合 [[3,1],[2]][[3, 1], [2]] 的难度为 22:可先将圆盘 11 移至第二座塔,再将第二座塔中的两个圆盘整体移至第一座塔。

现给出 m−1m - 1 个查询。每个查询由两个数 aia_i 和 bib_i 表示,含义为“合并塔 aia_i 和塔 bib_i”(即取出这两座塔的所有圆盘,按半径从大到小自上而下重新组成一座新塔)。新塔的编号为 aia_i。

对每个 k∈[0,m−1]k \in [0, m - 1],计算执行前 kk 个查询后所得塔集合的难度。

输入格式

The first line of the input contains two integers nn and mm (2≤m≤n≤2⋅1052 \le m \le n \le 2 \cdot 10^5) — the number of discs and the number of towers, respectively.

The second line contains nn integers t1t_1, t2t_2, ..., tnt_n (1≤ti≤m1 \le t_i \le m), where tit_i is the index of the tower disc ii belongs to. Each value from 11 to mm appears in this sequence at least once.

Then m−1m - 1 lines follow, denoting the queries. Each query is represented by two integers aia_i and bib_i (1≤ai,bi≤m1 \le a_i, b_i \le m, ai≠bia_i \ne b_i), meaning that, during the ii-th query, the towers with indices aia_i and bib_i are merged (aia_i and bib_i are chosen in such a way that these towers exist before the ii-th query).

输入的第一行包含两个整数 nn 和 mm(2≤m≤n≤2⋅1052 \le m \le n \le 2 \cdot 10^5),分别表示圆盘的数量和塔的数量。

第二行包含 nn 个整数 t1t_1, t2t_2, ..., tnt_n(1≤ti≤m1 \le t_i \le m),其中 tit_i 表示第 ii 个圆盘所属的塔的编号。该序列中,从 11 到 mm 的每个值至少出现一次。

随后是 m−1m - 1 行,表示查询操作。每个查询由两个整数 aia_i 和 bib_i(1≤ai,bi≤m1 \le a_i, b_i \le m,且 ai≠bia_i \ne b_i)组成,表示在第 ii 次查询中,将编号为 aia_i 和 bib_i 的塔合并(aia_i 和 bib_i 的选取保证了这二者在第 ii 次查询前均存在)。

输出格式

Print mm integers. The kk-th integer (00-indexed) should be equal to the difficulty of the set of towers after the first kk queries are performed.

输出 mm 个整数。其中第 kk 个整数(从 00 开始编号)应等于执行前 kk 个查询后塔集合的难度值。

输入输出样例

  • 输入#1

    7 4
    1 2 3 3 1 4 3
    3 1
    2 3
    2 4

    输出#1

    5
    4
    2
    0

说明/提示

The towers in the example are:

  • before the queries: [[5,1],[2],[7,4,3],[6]][[5, 1], [2], [7, 4, 3], [6]];
  • after the first query: [[2],[7,5,4,3,1],[6]][[2], [7, 5, 4, 3, 1], [6]];
  • after the second query: [[7,5,4,3,2,1],[6]][[7, 5, 4, 3, 2, 1], [6]];
  • after the third query, there is only one tower: [7,6,5,4,3,2,1][7, 6, 5, 4, 3, 2, 1].

示例中的塔为:

  • 查询前:[[5,1],[2],[7,4,3],[6]][[5, 1], [2], [7, 4, 3], [6]];
  • 第一次查询后:[[2],[7,5,4,3,1],[6]][[2], [7, 5, 4, 3, 1], [6]];
  • 第二次查询后:[[7,5,4,3,2,1],[6]][[7, 5, 4, 3, 2, 1], [6]];
  • 第三次查询后,仅剩一座塔:[7,6,5,4,3,2,1][7, 6, 5, 4, 3, 2, 1]。

输入解题思路,AI测评打分。不知道怎么写?

首页