luogu#P16362. [BalticOI 2026] Sort
[BalticOI 2026] Sort
Problem Description
You are given an array of integers. Your task is to answer queries . In one operation, you are allowed to do one of the following:
- sort the first numbers in nondecreasing order, or
- sort the last numbers in nondecreasing order.
What is the minimum number of operations needed to sort the whole array in nondecreasing order? In each query, the array starts from the initial values .
Input Format
The first line contains two integers and : the length of the array and the number of queries.
The second line contains integers : the contents of the array.
The next lines describe the queries. Each line contains two integers and .
Output Format
Print lines with the answers to each query. If it is impossible to sort the array, print "-1".
6 3
3 1 4 1 5 9
4 1
3 3
2 5
1
-1
2
Hint
Explanation
In the first query, the array can be sorted in one operation by sorting the first numbers.
The array cannot be sorted with the available operations in the second query.
In the third query, the array can be sorted in two operations: start by sorting the first numbers and then sort the last numbers.
Constraints
- in all queries
Scoring
| Subtask | Constraints | Points |
|---|---|---|
| 1 | and in all queries | |
| 2 | ||
| 3 | in all queries | |
| 4 | ||
| 5 | and the array is a permutation of the numbers | |
| 6 | ||
| 7 | No additional constraints |