PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000206. Đổi chỗ đúng một lần – Bản 2 (One Time Swap 2)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đổi chỗ đúng một lần – Bản 2 (One Time Swap 2)

One Time Swap 2

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Given an integer sequence A=(A1,A2,…,AN)A=(A_1,A_2,\ldots,A_N) of length NN, define f(l,r)f(l,r), for every 1≤l<r≤N1\le l<r\le N, as the sequence obtained by swapping the elements at positions ll and rr, leaving all other elements unchanged.

Construct one result for each of the N(N−1)/2N(N-1)/2 index pairs, then sort all resulting sequences in nondecreasing lexicographical order. Call the sorted list B1,B2,…,BN(N−1)/2B_1,B_2,\ldots,B_{N(N-1)/2}.

Two different sequences are compared at the first position where their elements differ; the sequence with the smaller element is lexicographically smaller. Identical resulting sequences remain separate entries in BB when generated by different index pairs.

For each query containing kk, output any pair 1≤l<r≤N1\le l<r\le N satisfying f(l,r)=Bkf(l,r)=B_k. If several index pairs produce the requested sequence, any one of them is accepted.

Input

  • The first line contains integers N,QN,Q, the length of the sequence and the number of queries.
  • The next line contains exactly NN integers A1,A2,…,ANA_1,A_2,\ldots,A_N in their original order.
  • Each of the next QQ lines contains an integer kk, the 11-based rank in the sorted list of sequences.

Output

Print exactly QQ lines. For query ii, print two space-separated integers l,rl,r such that 1≤l<r≤N1\le l<r\le N and the array after swapping these positions equals the requested BkB_k. Multiple valid answers are accepted.

Subtasks

  • Subtask 1 (20 points): 2≤N≤72\le N\le 7; 1≤Q≤301\le Q\le 30; 1≤Ai≤N1\le A_i\le N for every ii; 1≤k≤N(N−1)/21\le k\le N(N-1)/2 for each query.
  • Subtask 2 (30 points): 2≤N≤25002\le N\le 2500; 1≤Q≤50001\le Q\le 5000; 1≤Ai≤N1\le A_i\le N for every ii; 1≤k≤N(N−1)/21\le k\le N(N-1)/2 for each query.
  • Subtask 3 (50 points): 2≤N≤2⋅1052\le N\le 2\cdot 10^5; 1≤Q≤2⋅1051\le Q\le 2\cdot 10^5; 1≤Ai≤N1\le A_i\le N for every ii; 1≤k≤N(N−1)/21\le k\le N(N-1)/2 for each query.

Examples

Input

4 3
1 2 1 2
1
3
5

Output

2 3
2 4
1 2

Explanation

The six swap pairs produce f(1,2)=(2,1,1,2)f(1,2)=(2,1,1,2), f(1,3)=(1,2,1,2)f(1,3)=(1,2,1,2), f(1,4)=(2,2,1,1)f(1,4)=(2,2,1,1), f(2,3)=(1,1,2,2)f(2,3)=(1,1,2,2), f(2,4)=(1,2,1,2)f(2,4)=(1,2,1,2), and f(3,4)=(1,2,2,1)f(3,4)=(1,2,2,1).

In sorted order, these are (1,1,2,2)(1,1,2,2), (1,2,1,2)(1,2,1,2), (1,2,1,2)(1,2,1,2), (1,2,2,1)(1,2,2,1), (2,1,1,2)(2,1,1,2), and (2,2,1,1)(2,2,1,1). Thus, for ranks 1,3,51,3,5, the reported pairs (2,3)(2,3), (2,4)(2,4), and (1,2)(1,2) are all correct. Rank 33 also accepts (1,3)(1,3), illustrating why a special checker is necessary.