CF1685E.The Ultimate LIS Problem

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

It turns out that this is exactly the 100100 -th problem of mine that appears in some programming competition. So it has to be special! And what can be more special than another problem about LIS...

You are given a permutation p1,p2,,p2n+1p_1, p_2, \ldots, p_{2n+1} of integers from 11 to 2n+12n+1 . You will have to process qq updates, where the ii -th update consists in swapping pui,pvip_{u_i}, p_{v_i} .

After each update, find any cyclic shift of pp with LISnLIS \le n , or determine that there is no such shift. (Refer to the output section for details).

Here LIS(a)LIS(a) denotes the length of longest strictly increasing subsequence of aa .

Hacks are disabled in this problem. Don't ask why.

输入格式

The first line of the input contains two integers n,qn, q ( 2n1052 \le n \le 10^5 , 1q1051 \le q \le 10^5 ).

The second line of the input contains 2n+12n+1 integers p1,p2,,p2n+1p_1, p_2, \ldots, p_{2n+1} ( 1pi2n+11 \le p_i \le 2n+1 , all pip_i are distinct) — the elements of pp .

The ii -th of the next qq lines contains two integers ui,viu_i, v_i ( 1ui,vi2n+11 \le u_i, v_i \le 2n+1 , uiviu_i \neq v_i ) — indicating that you have to swap elements pui,pvip_{u_i}, p_{v_i} in the ii -th update.

输出格式

After each update, output any kk (0k2n)(0 \le k \le 2n) , such that the length of the longest increasing subsequence of (pk+1,pk+2,,p2n+1,p1,,pk)(p_{k+1}, p_{k+2}, \ldots, p_{2n+1}, p_1, \ldots, p_k) doesn't exceed nn , or 1-1 , if there is no such kk .

输入输出样例

  • 输入#1

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

    输出#1

    -1
    -1
    2
    -1
    4
    0

说明/提示

After the first update, our permutation becomes (5,2,3,4,1)(5, 2, 3, 4, 1) . We can show that all its cyclic shifts have LIS3LIS \ge 3 .

After the second update, our permutation becomes (1,2,3,4,5)(1, 2, 3, 4, 5) . We can show that all its cyclic shifts have LIS3LIS \ge 3 .

After the third update, our permutation becomes (1,2,3,5,4)(1, 2, 3, 5, 4) . Its shift by 22 is (3,5,4,1,2)(3, 5, 4, 1, 2) , and its LIS=2LIS = 2 .

After the fourth update, our permutation becomes (1,2,3,4,5)(1, 2, 3, 4, 5) . We can show that all its cyclic shifts have LIS3LIS \ge 3 .

After the fifth update, our permutation becomes (4,2,3,1,5)(4, 2, 3, 1, 5) . Its shift by 44 is (5,4,2,3,1)(5, 4, 2, 3, 1) , and its LIS=2LIS = 2 .

After the fifth update, our permutation becomes (4,5,3,1,2)(4, 5, 3, 1, 2) . Its shift by 00 is (4,5,3,1,2)(4, 5, 3, 1, 2) , and its LIS=2LIS = 2 .

首页