CF1830E.Bully Sort

普及/提高-

通过率:0%

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 piip_i \neq i .
  • Let jj be the index of the smallest element pjp_j such that i<ji < 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.

输入格式

The first line of the input contains two integers nn and qq ( 2n51052 \le n \le 5 \cdot 10^5 , 1q51041 \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 ( 1pin1 \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 ( 1xi<yin1 \le x_i < y_i \le n ) — describing the ii -th update.

输出格式

After each update, output 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] .
首页