Codeforces Round 1048 (Div. 1)
B
Antiamuny Wants to Learn Swap
Codeforces
1900
原题
← AC1 →
翻译中…

For an array $b$ of length $m$, you may perform the following two operations:

翻译中…
  1. Select an index $1\le i\le m - 1$. Then, swap the values of $b_i$ and $b_{i + 1}$.
  2. Select an index $1\le i\le m - 2$. Then, swap the values of $b_i$ and $b_{i + 2}$.
翻译中…

However, you can only perform operation $2$

翻译中…

at most once

翻译中…

.

翻译中…

We define $f(b)$ as the minimum number of operations (using both operation 1 and operation 2) required to sort array $b$ in non-decreasing order, and $g(b)$ as the minimum number of operations required to sort array $b$ in non-decreasing order using only operation 1.

翻译中…

The array $b$ is perfect if $f(b) = g(b)$. In other words, the ability to use operation 2 does not reduce the number of operations required to sort array $b$ compared to using only adjacent swaps.

翻译中…

You are given a permutation $a$ of length $n$$^{\text{∗}}$, and must answer $q$ queries. Each query consists of two integers $l$ and $r$ ($1\le l\le r\le n$), representing the subarray $a[l\ldots r]$$^{\text{†}}$. For each query, determine whether the subarray $a[l\ldots r]$ is perfect.

翻译中…

$^{\text{∗}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array). $^{\text{†}}$The subarray $a[l\ldots r]$ includes all elements from index $l$ to $r$, i.e., $[a_l, a_{l + 1}, a_{l + 2}, \ldots, a_r]$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5 \cdot 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$, $q$ ($1 \le n, q \le 5 \cdot 10^5$) — the length of array $a$ and the number of queries.

翻译中…

The second line of each test case contains $n$ integers $a_1, a_2, \ldots ,a_n$ ($1 \le a_i \le n$) — the elements in permutation $a$.

翻译中…

Each of the next $q$ lines contains two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) — the left and right endpoints of the queried subarray.

翻译中…

It is guaranteed that both the sum of $n$ and the sum of $q$ over all test cases do not exceed $5\cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output "YES" if queried subarray $a[l\ldots r]$ is perfect, and "NO" otherwise.

翻译中…

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

翻译中…

Note

翻译中…

In the first test case:

翻译中…
  • Query 1: $a[1\ldots 2] = [1, 5]$ is already sorted in increasing order. Thus, $f(a[1\ldots 2]) = g(a[1\ldots 2]) = 0$ and the subarray $a[1\ldots 2]$ is perfect.
  • Query 2: $a[1\ldots 5] = [1, 5, 4, 3, 2]$. $f(a[1\ldots 5]) = 4$ as we can sort the array using the following sequence of operations:$$[1,\textbf{5},4,\textbf{3},2] \xrightarrow{\text{op} 2} [1,3,4,\textbf{5},\textbf{2}] \xrightarrow{\text{op} 1} [1,3,\textbf{4},\textbf{2},5]\xrightarrow{\text{op} 1} [1,\textbf{3},\textbf{2},4,5] \xrightarrow{\text{op} 1}[1,2,3,4,5]$$ On the other hand, $g(a[1\ldots 5]) = 6$ as it requires at least $6$ adjacent swaps to sort $a[1\ldots 5]$. Since $f(a[1\ldots 5]) \neq g(a[1\ldots 5])$, the subarray $a[1\ldots 5]$ is not perfect.
  • Query 3: $a[3\ldots 5] = [4, 3, 2]$. $f(a[3\ldots 5]) = 1$ as we can sort the array using the following sequence of operations:$$[\textbf{4}, 3,\textbf{2}] \xrightarrow{\text{op} 2} [2, 3, 4]$$ On the other hand, $g(a[3\ldots 5]) = 3$ as it requires at least $3$ adjacent swaps to sort $a[3\ldots 5]$. Since $f(a[3\ldots 5]) \neq g(a[3\ldots 5])$, the subarray $a[3\ldots 5]$ is not perfect.
翻译中…

样例

Input #1
2
5 5
1 5 4 3 2
1 2
1 5
3 5
1 4
2 5
5 5
3 2 1 4 5
1 1
4 5
1 4
2 5
3 4
Output #1
YES
NO
NO
NO
NO
YES
YES
NO
YES
YES
InputCopy OutputCopy
题目信息
题号
2138B
时间限制
2 s
内存限制
512 MB
标签
数据结构
贪心
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。