#P1375H. Set Merging

    ID: 5457 Type: RemoteJudge 4000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>constructive algorithmsdivide and conquer*3300

Set Merging

No submission language available for this problem.

Description

You are given a permutation a1,a2,,ana_1, a_2, \dots, a_n of numbers from 11 to nn. Also, you have nn sets S1,S2,,SnS_1,S_2,\dots, S_n, where Si={ai}S_i=\{a_i\}. Lastly, you have a variable cntcnt, representing the current number of sets. Initially, cnt=ncnt = n.

We define two kinds of functions on sets:

f(S)=minuSuf(S)=\min\limits_{u\in S} u;

g(S)=maxuSug(S)=\max\limits_{u\in S} u.

You can obtain a new set by merging two sets AA and BB, if they satisfy g(A)<f(B)g(A)<f(B) (Notice that the old sets do not disappear).

Formally, you can perform the following sequence of operations:

  • cntcnt+1cnt\gets cnt+1;

  • Scnt=SuSvS_{cnt}=S_u\cup S_v, you are free to choose uu and vv for which 1u,v<cnt1\le u, v < cnt and which satisfy g(Su)<f(Sv)g(S_u)<f(S_v).

You are required to obtain some specific sets.

There are qq requirements, each of which contains two integers lil_i,rir_i, which means that there must exist a set SkiS_{k_i} (kik_i is the ID of the set, you should determine it) which equals {auliuri}\{a_u\mid l_i\leq u\leq r_i\}, which is, the set consisting of all aia_i with indices between lil_i and rir_i.

In the end you must ensure that cnt2.2×106cnt\leq 2.2\times 10^6. Note that you don't have to minimize cntcnt. It is guaranteed that a solution under given constraints exists.

The first line contains two integers n,qn,q (1n212,1q216)(1\leq n \leq 2^{12},1 \leq q \leq 2^{16})  — the length of the permutation and the number of needed sets correspondently.

The next line consists of nn integers a1,a2,,ana_1,a_2,\cdots, a_n (1ain1\leq a_i\leq n, aia_i are pairwise distinct)  — given permutation.

ii-th of the next qq lines contains two integers li,ril_i,r_i (1lirin)(1\leq l_i\leq r_i\leq n), describing a requirement of the ii-th set.

It is guaranteed that a solution under given constraints exists.

The first line should contain one integer cntEcnt_E (ncntE2.2×106)(n\leq cnt_E\leq 2.2\times 10^6), representing the number of sets after all operations.

cntEncnt_E-n lines must follow, each line should contain two integers uu, vv (1u,vcnt1\leq u, v\leq cnt', where cntcnt' is the value of cntcnt before this operation), meaning that you choose SuS_u, SvS_v and perform a merging operation. In an operation, g(Su)<f(Sv)g(S_u)<f(S_v) must be satisfied.

The last line should contain qq integers k1,k2,,kqk_1,k_2,\cdots,k_q (1kicntE)(1\leq k_i\leq cnt_E), representing that set SkiS_{k_i} is the iith required set.

Please notice the large amount of output.

Input

The first line contains two integers n,qn,q (1n212,1q216)(1\leq n \leq 2^{12},1 \leq q \leq 2^{16})  — the length of the permutation and the number of needed sets correspondently.

The next line consists of nn integers a1,a2,,ana_1,a_2,\cdots, a_n (1ain1\leq a_i\leq n, aia_i are pairwise distinct)  — given permutation.

ii-th of the next qq lines contains two integers li,ril_i,r_i (1lirin)(1\leq l_i\leq r_i\leq n), describing a requirement of the ii-th set.

Output

It is guaranteed that a solution under given constraints exists.

The first line should contain one integer cntEcnt_E (ncntE2.2×106)(n\leq cnt_E\leq 2.2\times 10^6), representing the number of sets after all operations.

cntEncnt_E-n lines must follow, each line should contain two integers uu, vv (1u,vcnt1\leq u, v\leq cnt', where cntcnt' is the value of cntcnt before this operation), meaning that you choose SuS_u, SvS_v and perform a merging operation. In an operation, g(Su)<f(Sv)g(S_u)<f(S_v) must be satisfied.

The last line should contain qq integers k1,k2,,kqk_1,k_2,\cdots,k_q (1kicntE)(1\leq k_i\leq cnt_E), representing that set SkiS_{k_i} is the iith required set.

Please notice the large amount of output.

Samples

Sample Input 1

3 2
1 3 2
2 3
1 3

Sample Output 1

6
3 2
1 3
5 2
4 6

Sample Input 2

2 4
2 1
1 2
1 2
1 2
1 1

Sample Output 2

5
2 1
2 1
2 1
5 3 3 1

Note

In the first sample:

We have S1={1},S2={3},S3={2}S_1=\{1\},S_2=\{3\},S_3=\{2\} initially.

In the first operation, because g(S3)=2<f(S2)=3g(S_3)=2<f(S_2)=3, we can merge S3,S2S_3,S_2 into S4={2,3}S_4=\{2,3\}.

In the second operation, because g(S1)=1<f(S3)=2g(S_1)=1<f(S_3)=2, we can merge S1,S3S_1,S_3 into S5={1,2}S_5=\{1,2\}.

In the third operation, because g(S5)=2<f(S2)=3g(S_5)=2<f(S_2)=3, we can merge S5,S2S_5,S_2 into S6={1,2,3}S_6=\{1,2,3\}.

For the first requirement, S4={2,3}={a2,a3}S_4=\{2,3\}=\{a_2,a_3\}, satisfies it, thus k1=4k_1=4.

For the second requirement, S6={1,2,3}={a1,a2,a3}S_6=\{1,2,3\}=\{a_1,a_2,a_3\}, satisfies it, thus k2=6k_2=6

Notice that unused sets, identical sets, outputting the same set multiple times, and using sets that are present initially are all allowed.