neq-array

Statement

Given a constraint array A of length N, we generate a new integer array B of length N satisfying the following conditions:

  • All elements of B are positive, i.e. Bi > 0.
  • B is non-decreasing, i.e. B1 ≤ B2 ≤ B3 ≤ .... ≤ BN.
  • Bi ≠ Ai for each 1 ≤ i ≤ N.
  • BN is the minimum possible.

Let f(A) denote the minimum possible value of BN for a valid array B.

You will be given queries L and R, and you need to answer f(A[L, R]) where A[L, R] denotes the subarray [AL, AL+1, ..., AR] . That is, you need to answer the minimum value of BN possible if only the L-th to the R-th indices of A were present.

There are multiple test cases in each file, and thus you will be given an input parameter T, denoting that you have to solve T test cases.

Input Format

The first line of input contains T - the number of test cases.

The first line of each test case contains N and Q - the size of the array A and the number of queries respectively.

The second line of each test case contains N integers - A1, A2, ..., AN.

The j-th of the next Q lines contains Lj and Rj - the parameters of the j-th query.

Output Format

For each test case, output Q integers on a single line, f(A[Lj, Rj]) for each query.

Constraints

It is guaranteed that:

  • 1 ≤ T ≤ 104
  • 1 ≤ N, Q ≤ 2 × 105
  • 1 ≤ Ai ≤ N
  • 1 ≤ Lj ≤ Rj ≤ N
  • The sum of N and the sum of Q over all test cases does not exceed 2 × 105.

Subtasks

The special constraint whole array means that Q = 1, L₁ = 1, R₁ = N, i.e. there is only one query which covers the whole array.

  • Subtask 1 (4 points) : N = 2 and whole array.
  • Subtask 2 (15 points) : N ≤ 1000, whole array, and the sum of N over all test cases does not exceed 1000.
  • Subtask 3 (8 points) : whole array.
  • Subtask 4 (18 points) : Ai ≤ 100.
  • Subtask 5 (12 points) : |Ai - Ai - 1| ≤ 1.
  • Subtask 6 (20 points) : Rj = N for all queries.
  • Subtask 7 (23 points) : No additional constraints.

Sample 1

Input

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

Output

2
5 1
3 3 2 2 1

Explanation

Test Case 1 : This is a query on the whole array. A = [2, 1] and we choose B = [1, 2] which satisfies all conditions, while minimizing B_N = 2. It can be proven it is impossible to achieve a smaller value.

Test Case 2 : In the first query, the query is again of the whole array, A = [1, 2, 3, 4] and we choose B = [5, 5, 5, 5] which satisfies all conditions and minimizes B_N = 5.

In the second query, we work with the subarray A = [2, 3, 4] instead, and we can choose B = [1, 1, 1] here.

Time Limit
2000 ms
Memory Limit
1048576 KiB
Please login to submit.
Ln: 1, Col: 1
Key Map: default
Login to submit.
Time: 28 Apr 2025, Mon 05:31:18
© 2025 UNIQUE BIT TECHNOLOGIES PVT. LTD. All Rights Reserved.