CF1621I.Two Sequences

NOI/NOI+/CTSC

通过率:0%

时间限制:8.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Consider an array of integers C=[c1,c2,…,cn]C = [c_1, c_2, \ldots, c_n] of length nn. Let's build the sequence of arrays D0,D1,D2,…,DnD_0, D_1, D_2, \ldots, D_{n} of length n+1n+1 in the following way:

  • The first element of this sequence will be equals CC: D0=CD_0 = C.
  • For each 1≤i≤n1 \leq i \leq n array DiD_i will be constructed from Di−1D_{i-1} in the following way:
    • Let's find the lexicographically smallest subarray of Di−1D_{i-1} of length ii. Then, the first n−in-i elements of DiD_i will be equals to the corresponding n−in-i elements of array Di−1D_{i-1} and the last ii elements of DiD_i will be equals to the corresponding elements of the found subarray of length ii.

Array xx is subarray of array yy, if xx can be obtained by deletion of several (possibly, zero or all) elements from the beginning of yy and several (possibly, zero or all) elements from the end of yy.

For array CC let's denote array DnD_n as op(C)op(C).

Alice has an array of integers A=[a1,a2,…,an]A = [a_1, a_2, \ldots, a_n] of length nn. She will build the sequence of arrays B0,B1,…,BnB_0, B_1, \ldots, B_n of length n+1n+1 in the following way:

  • The first element of this sequence will be equals AA: B0=AB_0 = A.
  • For each 1≤i≤n1 \leq i \leq n array BiB_i will be equals op(Bi−1)op(B_{i-1}), where opop is the transformation described above.

She will ask you qq queries about elements of sequence of arrays B0,B1,…,BnB_0, B_1, \ldots, B_n. Each query consists of two integers ii and jj, and the answer to this query is the value of the jj-th element of array BiB_i.

考虑一个长度为 nn 的整数数组 C=[c1,c2,…,cn]C = [c_1, c_2, \ldots, c_n]。我们按如下方式构造长度为 n+1n+1 的数组序列 D0,D1,D2,…,DnD_0, D_1, D_2, \ldots, D_{n}:

  • 该序列的第一个数组即为 CC:D0=CD_0 = C。
  • 对每个 1≤i≤n1 \leq i \leq n,数组 DiD_i 由 Di−1D_{i-1} 按如下方式构造:
    • 找出 Di−1D_{i-1} 中字典序最小的长度为 ii 的子数组;然后,DiD_i 的前 n−in-i 个元素等于 Di−1D_{i-1} 中对应的前 n−in-i 个元素,而 DiD_i 的后 ii 个元素等于所找到的长度为 ii 的子数组中对应的元素。

若数组 xx 可通过从数组 yy 的开头删除若干(可能为零个或全部)元素、并从其末尾删除若干(可能为零个或全部)元素而得到,则称 xx 是 yy 的子数组。

对数组 CC,记 DnD_n 为 op(C)op(C)。

Alice 有一个长度为 nn 的整数数组 A=[a1,a2,…,an]A = [a_1, a_2, \ldots, a_n]。她将按如下方式构造长度为 n+1n+1 的数组序列 B0,B1,…,BnB_0, B_1, \ldots, B_n:

  • 该序列的第一个数组即为 AA:B0=AB_0 = A。
  • 对每个 1≤i≤n1 \leq i \leq n,数组 BiB_i 等于 op(Bi−1)op(B_{i-1}),其中 opop 即上述定义的变换。

她将向你提出 qq 个查询,每个查询针对数组序列 B0,B1,…,BnB_0, B_1, \ldots, B_n 中的某个元素。每个查询包含两个整数 ii 和 jj,其答案为数组 BiB_i 的第 jj 个元素的值。

输入格式

The first line contains the single integer nn (1≤n≤1051 \leq n \leq 10^5) — the length of array AA.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \leq a_i \leq n) — the array AA.

The third line contains the single integer qq (1≤q≤1061 \leq q \leq 10^6) — the number of queries.

Each of the next qq lines contains two integers ii, jj (1≤i,j≤n1 \leq i, j \leq n) — parameters of queries.

第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5)—— 数组 AA 的长度。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \leq a_i \leq n)—— 数组 AA。

第三行包含一个整数 qq(1≤q≤1061 \leq q \leq 10^6)—— 查询的数量。

接下来的 qq 行中,每行包含两个整数 ii、jj(1≤i,j≤n1 \leq i, j \leq n)—— 查询的参数。

输出格式

Output qq integers: values of Bi,jB_{i, j} for required ii, jj.

输出 qq 个整数:所需 ii、jj 对应的 Bi,jB_{i, j} 的值。

输入输出样例

  • 输入#1

    4
    2 1 3 1
    4
    1 1
    1 2
    1 3
    1 4

    输出#1

    2
    1
    1
    3

说明/提示

In the first test case B0=A=[2,1,3,1]B_0 = A = [2, 1, 3, 1].

B1B_1 is constructed in the following way:

  • Initially, D0=[2,1,3,1]D_0 = [2, 1, 3, 1].
  • For i=1i=1 the lexicographically smallest subarray of D0D_0 of length 11 is [1][1], so D1D_1 will be [2,1,3,1][2, 1, 3, 1].
  • For i=2i=2 the lexicographically smallest subarray of D1D_1 of length 22 is [1,3][1, 3], so D2D_2 will be [2,1,1,3][2, 1, 1, 3].
  • For i=3i=3 the lexicographically smallest subarray of D2D_2 of length 33 is [1,1,3][1, 1, 3], so D3D_3 will be [2,1,1,3][2, 1, 1, 3].
  • For i=4i=4 the lexicographically smallest subarray of D3D_3 of length 44 is [2,1,1,3][2, 1, 1, 3], so D4D_4 will be [2,1,1,3][2, 1, 1, 3].
  • So, B1=op(B0)=op([2,1,3,1])=[2,1,1,3]B_1 = op(B_0) = op([2, 1, 3, 1]) = [2, 1, 1, 3].

在第一个测试用例中,B0=A=[2,1,3,1]B_0 = A = [2, 1, 3, 1]。

B1B_1 按如下方式构造:

  • 初始时,D0=[2,1,3,1]D_0 = [2, 1, 3, 1]。
  • 当 i=1i=1 时,D0D_0 中长度为 11 的字典序最小的子数组是 [1][1],因此 D1=[2,1,3,1]D_1 = [2, 1, 3, 1]。
  • 当 i=2i=2 时,D1D_1 中长度为 22 的字典序最小的子数组是 [1,3][1, 3],因此 D2=[2,1,1,3]D_2 = [2, 1, 1, 3]。
  • 当 i=3i=3 时,D2D_2 中长度为 33 的字典序最小的子数组是 [1,1,3][1, 1, 3],因此 D3=[2,1,1,3]D_3 = [2, 1, 1, 3]。
  • 当 i=4i=4 时,D3D_3 中长度为 44 的字典序最小的子数组是 [2,1,1,3][2, 1, 1, 3],因此 D4=[2,1,1,3]D_4 = [2, 1, 1, 3]。
  • 因此,B1=op(B0)=op([2,1,3,1])=[2,1,1,3]B_1 = op(B_0) = op([2, 1, 3, 1]) = [2, 1, 1, 3]。

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

首页