CF1621I.Two Sequences

普及/提高-

通过率:0%

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 1in1 \leq i \leq n array DiD_i will be constructed from Di1D_{i-1} in the following way:
    • Let's find the lexicographically smallest subarray of Di1D_{i-1} of length ii . Then, the first nin-i elements of DiD_i will be equals to the corresponding nin-i elements of array Di1D_{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 1in1 \leq i \leq n array BiB_i will be equals op(Bi1)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 .

输入格式

The first line contains the single integer nn ( 1n1051 \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 ( 1ain1 \leq a_i \leq n ) — the array AA .

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

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

输出格式

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

输入输出样例

  • 输入#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] .
首页