CF1900F.Local Deletions

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

For an array b1,b2,…,bmb_1, b_2, \ldots, b_m, for some ii (1<i<m1 \lt i \lt m), element bib_i is said to be a local minimum if bi<bi−1b_i \lt b_{i-1} and bi<bi+1b_i \lt b_{i+1}. Element b1b_1 is said to be a local minimum if b1<b2b_1 \lt b_2. Element bmb_m is said to be a local minimum if bm<bm−1b_m \lt b_{m-1}.

For an array b1,b2,…,bmb_1, b_2, \ldots, b_m, for some ii (1<i<m1 \lt i \lt m), element bib_i is said to be a local maximum if bi>bi−1b_i \gt b_{i-1} and bi>bi+1b_i \gt b_{i+1}. Element b1b_1 is said to be a local maximum if b1>b2b_1 \gt b_2. Element bmb_m is said to be a local maximum if bm>bm−1b_m \gt b_{m-1}.

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

  • 11 — delete all elements from xx that are not local minima.
  • 22 — delete all elements from xx that are not local maxima.

Define f(x)f(x) as follows. Repeat operations 1,2,1,2,…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][1,3,2]. We will first do type 11 operation and get [1,2][1, 2]. Then we will perform type 22 operation and get [2][2]. Therefore, f([1,3,2])=2f([1,3,2]) = 2.

You are given a permutation†^\dagger aa of size nn and qq queries. Each query consists of two integers ll and rr such that 1≤l≤r≤n1 \le l \le r \le n. The query asks you to compute f([al,al+1,…,ar])f([a_l, a_{l+1}, \ldots, a_r]).

†^\dagger A permutation of length nn is an array of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3, but there is 44 in the array).

对于数组 b1,b2,…,bmb_1, b_2, \ldots, b_m,对某个下标 ii(其中 1<i<m1 \lt i \lt m),若满足 bi<bi−1b_i \lt b_{i-1} 且 bi<bi+1b_i \lt b_{i+1},则称元素 bib_i 为局部最小值;若 b1<b2b_1 \lt b_2,则称 b1b_1 为局部最小值;若 bm<bm−1b_m \lt b_{m-1},则称 bmb_m 为局部最小值。

对于数组 b1,b2,…,bmb_1, b_2, \ldots, b_m,对某个下标 ii(其中 1<i<m1 \lt i \lt m),若满足 bi>bi−1b_i \gt b_{i-1} 且 bi>bi+1b_i \gt b_{i+1},则称元素 bib_i 为局部最大值;若 b1>b2b_1 \gt b_2,则称 b1b_1 为局部最大值;若 bm>bm−1b_m \gt b_{m-1},则称 bmb_m 为局部最大值。

设 xx 是一个由互异元素构成的数组。我们在其上定义如下两种操作:

  • 操作 11:删除 xx 中所有非局部最小值的元素;
  • 操作 22:删除 xx 中所有非局部最大值的元素。

定义函数 f(x)f(x) 如下:按顺序重复执行操作 1,2,1,2,…1, 2, 1, 2, \ldots,直至数组中仅剩一个元素为止,返回该元素。

例如,考虑数组 [1,3,2][1,3,2]。我们首先执行操作 11,得到 [1,2][1, 2];接着执行操作 22,得到 [2][2]。因此,f([1,3,2])=2f([1,3,2]) = 2。

现给定一个长度为 nn 的排列†^\dagger aa 以及 qq 个查询。每个查询包含两个整数 ll 和 rr,满足 1≤l≤r≤n1 \le l \le r \le n。该查询要求你计算 f([al,al+1,…,ar])f([a_l, a_{l+1}, \ldots, a_r])。

†^\dagger 长度为 nn 的排列是指由 11 到 nn 这 nn 个互异整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数组中 22 出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

输入格式

The first line contains two integers nn and qq (1≤n,q≤1051 \le n, q \le 10^5) — the length of the permutation aa and the number of queries.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n) — the elements of permutation aa.

The ii-th of the next qq lines contains two integers lil_i and rir_i (1≤li≤ri≤n1 \le l_i \le r_i \le n) — the description of ii-th query.

第一行包含两个整数 nn 和 qq(1≤n,q≤1051 \le n, q \le 10^5)—— 分别表示排列 aa 的长度和查询次数。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \le a_i \le n)—— 排列 aa 的元素。

接下来的 qq 行中,第 ii 行包含两个整数 lil_i 和 rir_i(1≤li≤ri≤n1 \le l_i \le r_i \le n)—— 描述第 ii 个查询。

输出格式

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 11, therefore it is the answer.

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

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

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

In the fifth query of the first example, our subarray initially looks like [1,4,3,6,2,7,5][1, 4, 3, 6, 2, 7, 5]. After performing type 11 operation we get [1,3,2,5][1,3,2,5]. After performing type 22 operation we get [3,5][3,5]. Then we perform type 11 operation and we get [3][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][1,2,3,4,5,6,7,8,9,10]. Here 11 is the only local minimum, so only it is left after performing type 11 operation.

在第一个样例的第一个查询中,子数组中唯一的数字是 11,因此答案就是 11。

在第一个样例的第二个查询中,我们的子数组初始为 [1,4][1, 4]。执行一次类型 11 操作后,得到 [1][1]。

在第一个样例的第三个查询中,我们的子数组初始为 [4,3][4, 3]。执行一次类型 11 操作后,得到 [3][3]。

在第一个样例的第四个查询中,我们的子数组初始为 [1,4,3,6][1, 4, 3, 6]。执行一次类型 11 操作后,得到 [1,3][1, 3];再执行一次类型 22 操作,得到 [3][3]。

在第一个样例的第五个查询中,我们的子数组初始为 [1,4,3,6,2,7,5][1, 4, 3, 6, 2, 7, 5]。执行一次类型 11 操作后,得到 [1,3,2,5][1,3,2,5];再执行一次类型 22 操作,得到 [3,5][3,5];然后执行一次类型 11 操作,得到 [3][3]。

在第二个样例的唯一查询中,我们的子数组初始为 [1,2,3,4,5,6,7,8,9,10][1,2,3,4,5,6,7,8,9,10]。其中 11 是唯一的局部最小值,因此执行类型 11 操作后仅剩 11。

输入解题思路,AI测评打分。不知道怎么写?

首页