CF1900F.Local Deletions
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
For an array b1,b2,…,bm , for some i ( 1<i<m ), element bi is said to be a local minimum if bi<bi−1 and bi<bi+1 . Element b1 is said to be a local minimum if b1<b2 . Element bm is said to be a local minimum if bm<bm−1 .
For an array b1,b2,…,bm , for some i ( 1<i<m ), element bi is said to be a local maximum if bi>bi−1 and bi>bi+1 . Element b1 is said to be a local maximum if b1>b2 . Element bm is said to be a local maximum if bm>bm−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,… 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 † a of size n and q queries. Each query consists of two integers l and r such that 1≤l≤r≤n . The query asks you to compute f([al,al+1,…,ar]) .
† 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).
输入格式
The first line contains two integers n and q ( 1≤n,q≤105 ) — the length of the permutation a and the number of queries.
The second line contains n integers a1,a2,…,an ( 1≤ai≤n ) — the elements of permutation a .
The i -th of the next q lines contains two integers li and ri ( 1≤li≤ri≤n ) — the description of i -th query.
输出格式
For each query, output a single integer — the answer to that query.
输入输出样例
输入#1
7 5 1 4 3 6 2 7 5 1 1 1 2 2 3 1 4 1 7
输出#1
1 1 3 3 3
输入#2
10 1 1 2 3 4 5 6 7 8 9 10 1 10
输出#2
1
说明/提示
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.