#P1685E. The Ultimate LIS Problem

    ID: 7669 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>data structuresgreedy*3500

The Ultimate LIS Problem

No submission language available for this problem.

Description

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.

Input

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.

Output

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.

Samples

Sample Input 1

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

Sample Output 1

-1
-1
2
-1
4
0

Note

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.