CF1830E.Bully Sort

NOI/NOI+/CTSC

通过率:0%

时间限制:10.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

On a permutation pp of length nn, we define a bully swap as follows:

  • Let ii be the index of the largest element pip_i such that pi≠ip_i \neq i.
  • Let jj be the index of the smallest element pjp_j such that i<ji \lt j.
  • Swap pip_i and pjp_j.

We define f(p)f(p) as the number of bully swaps we need to perform until pp becomes sorted. Note that if pp is the identity permutation, f(p)=0f(p)=0.

You are given nn and a permutation pp of length nn. You need to process the following qq updates.

In each update, you are given two integers xx and yy. You will swap pxp_x and pyp_y and then find the value of f(p)f(p).

Note that the updates are persistent. Changes made to the permutation pp will apply when processing future updates.

对于一个长度为 nn 的排列 pp,我们定义一次“霸凌交换”(bully swap)如下:

  • 设 ii 是满足 pi≠ip_i \neq i 的最大元素 pip_i 的下标;
  • 设 jj 是满足 i<ji \lt j 的最小元素 pjp_j 的下标;
  • 交换 pip_i 和 pjp_j。

我们定义 f(p)f(p) 为将 pp 变为升序排列(即恒等排列)所需执行的霸凌交换次数。注意:若 pp 本身就是恒等排列,则 f(p)=0f(p)=0。

给定 nn 和一个长度为 nn 的排列 pp,你需要处理接下来的 qq 次更新。

每次更新中,你将收到两个整数 xx 和 yy;你需要先交换 pxp_x 和 pyp_y,然后计算当前 pp 对应的 f(p)f(p) 的值。

注意:这些更新是持久化的,即对排列 pp 所做的修改将在后续更新中持续生效。

输入格式

The first line of the input contains two integers nn and qq (2≤n≤5⋅1052 \le n \le 5 \cdot 10^5, 1≤q≤5⋅1041 \le q \le 5 \cdot 10^4) — the length of the permutation and the number of updates.

The second line of input contains nn integer p1,p2,…,pnp_1,p_2,\ldots,p_n (1≤pi≤n1 \leq p_i \leq n) — the permutation pp. All elements of pp are distinct.

The ii-th of the next qq lines of input contains two integers xix_i and yiy_i (1≤xi<yi≤n1 \le x_i \lt y_i \le n) — describing the ii-th update.

输入的第一行包含两个整数 nn 和 qq(2≤n≤5⋅1052 \le n \le 5 \cdot 10^5,1≤q≤5⋅1041 \le q \le 5 \cdot 10^4)—— 分别表示排列的长度和更新操作的次数。

输入的第二行包含 nn 个整数 p1,p2,…,pnp_1,p_2,\ldots,p_n(1≤pi≤n1 \leq p_i \leq n)—— 表示排列 pp。pp 中所有元素互不相同。

接下来 qq 行中的第 ii 行包含两个整数 xix_i 和 yiy_i(1≤xi<yi≤n1 \le x_i \lt y_i \le n)—— 描述第 ii 次更新操作。

输出格式

After each update, output f(p)f(p).

每次更新后,输出 f(p)f(p)。

输入输出样例

  • 输入#1

    8 5
    6 2 1 5 3 4 7 8
    1 8
    2 3
    4 7
    7 8
    3 6

    输出#1

    5
    6
    9
    8
    7

说明/提示

After the first update, we have f(p)=5f(p)=5. The 55 bully swaps are illustrated below.

  • [1,2,8,5,3,4,7,6][\mathbf{1}, 2, \mathbf{8}, 5, 3, 4, 7, 6],
  • [1,2,3,5,8,4,7,6][1, 2, \mathbf{3}, 5, \mathbf{8}, 4, 7, 6],
  • [1,2,3,5,4,8,7,6][1, 2, 3, 5, \mathbf{4}, \mathbf{8}, 7, 6],
  • [1,2,3,5,4,6,7,8][1, 2, 3, 5, 4, \mathbf{6}, 7, \mathbf{8}],
  • [1,2,3,4,5,6,7,8][1, 2, 3, \mathbf{4}, \mathbf{5}, 6, 7, 8].

第一次更新后,我们有 f(p)=5f(p)=5。这 55 次“霸凌交换”如下所示:

  • [1,2,8,5,3,4,7,6][\mathbf{1}, 2, \mathbf{8}, 5, 3, 4, 7, 6],
  • [1,2,3,5,8,4,7,6][1, 2, \mathbf{3}, 5, \mathbf{8}, 4, 7, 6],
  • [1,2,3,5,4,8,7,6][1, 2, 3, 5, \mathbf{4}, \mathbf{8}, 7, 6],
  • [1,2,3,5,4,6,7,8][1, 2, 3, 5, 4, \mathbf{6}, 7, \mathbf{8}],
  • [1,2,3,4,5,6,7,8][1, 2, 3, \mathbf{4}, \mathbf{5}, 6, 7, 8]。

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

首页