#P1900F. Local Deletions

    ID: 269 远端评测题 3000ms 256MiB 尝试: 0 已通过: 0 难度: (无) 上传者: 标签>binary searchdata structuresimplementation*2800

Local Deletions

Description

For an array $b_1, b_2, \ldots, b_m$, for some $i$ ($1 < i < m$), element $b_i$ is said to be a local minimum if $b_i < b_{i-1}$ and $b_i < b_{i+1}$. Element $b_1$ is said to be a local minimum if $b_1 < b_2$. Element $b_m$ is said to be a local minimum if $b_m < b_{m-1}$.

For an array $b_1, b_2, \ldots, b_m$, for some $i$ ($1 < i < m$), element $b_i$ is said to be a local maximum if $b_i > b_{i-1}$ and $b_i > b_{i+1}$. Element $b_1$ is said to be a local maximum if $b_1 > b_2$. Element $b_m$ is said to be a local maximum if $b_m > b_{m-1}$.

Let $x$ be an array of distinct elements. We define two operations on it:

  • $1$ — delete all elements from $x$ that are not local minima.
  • $2$ — delete all elements from $x$ that are not local maxima.

Define $f(x)$ as follows. Repeat operations $1, 2, 1, 2, \ldots$ in that order until you get only one element left in the array. Return that element.

For example, take an array $[1,3,2]$. We will first do type $1$ operation and get $[1, 2]$. Then we will perform type $2$ operation and get $[2]$. Therefore, $f([1,3,2]) = 2$.

You are given a permutation$^\dagger$ $a$ of size $n$ and $q$ queries. Each query consists of two integers $l$ and $r$ such that $1 \le l \le r \le n$. The query asks you to compute $f([a_l, a_{l+1}, \ldots, a_r])$.

$^\dagger$ A permutation of length $n$ is an array 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).

题面翻译

对于一个数组 b1,b2,,bmb_1,b_2,\dots,b_m,对于 1<i<m1<i<m,如果 bi1>bi<bi+1b_{i-1}>b_i<b_{i+1},那么称 bib_i 是一个局部最小值。特别的,如果 b1<b2b_1<b_2,那么称 b1b_1 是一个局部最小值,如果 bm1>bmb_{m-1}>b_m,那么称 bmb_m 是一个局部最小值。 类似的,对于一个数组 b1,b2,,bmb_1,b_2,\dots,b_m,对于 1<i<m1<i<m,如果 bi1<bi>bi+1b_{i-1}<b_i>b_{i+1},那么称 bib_i 是一个局部最大值。特别的,如果 b1>b2b_1>b_2,那么称 b1b_1 是一个局部最大值,如果 bm1<bmb_{m-1}<b_m,那么称 bmb_m 是一个局部最大值。 现在定义一个无重复元素的数组 xx,定义两个操作:

  1. 删除 xx 中所有不是局部最小值的数字。
  2. 删除 xx 中所有不是局部最大值的数字。

定义函数 f(x)f(x) 表示对于数组 xx,循环进行操作 1,21,2 直到这个数组只剩下一个数字,那么 f(x)f(x) 的值就是最后剩下的这个数字。

举个栗子,对于数组 [1,3,2][1,3,2],为了计算 f([1,3,2])f([1,3,2]),我们依次进行以下操作:

  • 进行操作 11,然后数组变成 [1,2][1,2]
  • 进行操作 22,然后数组变成 [2][2],只剩下一个数字,结束。
  • f([1,3,2])=2f([1,3,2])=2

现在给你一个 nn 的排列 aaqq 次询问,每次询问给出 l,rl,r,要你计算 f([al,al+1,,ar])f([a_l,a_{l+1},\dots,a_r])n,q105,1ain,1lirinn,q\le10^5,1\le a_i\le n,1\le l_i\le r_i\le n

Input

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

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

The $i$-th of the next $q$ lines contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) — the description of $i$-th query.

Output

For each query, output a single integer — the answer to that query.

7 5
1 4 3 6 2 7 5
1 1
1 2
2 3
1 4
1 7
10 1
1 2 3 4 5 6 7 8 9 10
1 10
1
1
3
3
3
1

Note

In the first query of the first example, the only number in the subarray is $1$, therefore it is the answer.

In the second query of the first example, our subarray initially looks like $[1, 4]$. After performing type $1$ operation we get $[1]$.

In the third query of the first example, our subarray initially looks like $[4, 3]$. After performing type $1$ operation we get $[3]$.

In the fourth query of the first example, our subarray initially looks like $[1, 4, 3, 6]$. After performing type $1$ operation we get $[1, 3]$. Then we perform type $2$ operation and we get $[3]$.

In the fifth query of the first example, our subarray initially looks like $[1, 4, 3, 6, 2, 7, 5]$. After performing type $1$ operation we get $[1,3,2,5]$. After performing type $2$ operation we get $[3,5]$. Then we perform type $1$ operation and we get $[3]$.

In the first and only query of the second example our subarray initially looks like $[1,2,3,4,5,6,7,8,9,10]$. Here $1$ is the only local minimum, so only it is left after performing type $1$ operation.