CF1470E.Strange Permutation

NOI/NOI+/CTSC

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Alice had a permutation p1,p2,…,pnp_1, p_2, \ldots, p_n. Unfortunately, the permutation looked very boring, so she decided to change it and choose some non-overlapping subranges of this permutation and reverse them. The cost of reversing a single subrange [l,r][l, r] (elements from position ll to position rr, inclusive) is equal to r−lr - l, and the cost of the operation is the sum of costs of reversing individual subranges. Alice had an integer cc in mind, so she only considered operations that cost no more than cc.

Then she got really bored, and decided to write down all the permutations that she could possibly obtain by performing exactly one operation on the initial permutation. Of course, Alice is very smart, so she wrote down each obtainable permutation exactly once (no matter in how many ways it can be obtained), and of course the list was sorted lexicographically.

Now Bob would like to ask Alice some questions about her list. Each question is in the following form: what is the ii-th number in the jj-th permutation that Alice wrote down? Since Alice is too bored to answer these questions, she asked you to help her out.

爱丽丝原本有一个排列 p1,p2,…,pnp_1, p_2, \ldots, p_n。不幸的是,这个排列看起来非常单调乏味,因此她决定对其进行修改:选择该排列中若干个互不重叠的子区间,并将每个子区间内的元素反转。反转单个子区间 [l,r][l, r](即从位置 ll 到位置 rr(含端点)的所有元素)的代价为 r−lr - l,而一次操作的总代价等于所有被反转子区间的代价之和。爱丽丝心中设定了一个整数 cc,因此她只考虑总代价不超过 cc 的操作。

随后,她变得极其无聊,于是决定将通过在初始排列上恰好执行一次操作所能得到的所有不同排列全部写下来。当然,爱丽丝非常聪明,因此她对每个可得到的排列仅记录一次(无论其可通过多少种方式获得),并且这些排列按字典序升序排列。

现在鲍勃想向爱丽丝询问一些关于该列表的问题。每个问题的形式如下:爱丽丝所写的第 jj 个排列中,第 ii 个位置上的数是多少?由于爱丽丝实在太无聊而不愿回答这些问题,她请你来帮她解答。

输入格式

The first line contains a single integer tt (1≤t≤301 \leq t \leq 30) — the number of test cases.

The first line of each test case contains three integers nn, cc, qq (1≤n≤3⋅1041 \leq n \leq 3 \cdot 10^4, 1≤c≤41 \leq c \leq 4, 1≤q≤3⋅1051 \leq q \leq 3 \cdot 10^5) — the length of the permutation, the maximum cost of the operation, and the number of queries.

The next line of each test case contains nn integers p1,p2,…,pnp_1, p_2, \dots, p_n (1≤pi≤n1 \leq p_i \leq n, pi≠pjp_i \neq p_j if i≠ji \neq j), describing the initial permutation.

The following qq lines describe the queries. Each of them contains two integers ii and jj (1≤i≤n1 \leq i \leq n, 1≤j≤10181 \leq j \leq 10^{18}), denoting parameters of this query.

It is guaranteed that the sum of values nn over all test cases does not exceed 3⋅1053 \cdot 10^5, and the sum of values qq over all test cases does not exceed 3⋅1053 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤301 \leq t \leq 30)——测试用例的数量。

每个测试用例的第一行包含三个整数 nn、cc、qq(1≤n≤3⋅1041 \leq n \leq 3 \cdot 10^4,1≤c≤41 \leq c \leq 4,1≤q≤3⋅1051 \leq q \leq 3 \cdot 10^5)——分别表示排列的长度、操作的最大代价以及查询的数量。

每个测试用例的下一行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \dots, p_n(1≤pi≤n1 \leq p_i \leq n,当 i≠ji \neq j 时 pi≠pjp_i \neq p_j),描述初始排列。

接下来的 qq 行描述查询。每行包含两个整数 ii 和 jj(1≤i≤n1 \leq i \leq n,1≤j≤10181 \leq j \leq 10^{18}),表示该查询的参数。

保证所有测试用例中 nn 的总和不超过 3⋅1053 \cdot 10^5,且所有测试用例中 qq 的总和不超过 3⋅1053 \cdot 10^5。

输出格式

For each query output the answer for this query, or −1-1 if jj-th permutation does not exist in her list.

对于每个查询,输出该查询的答案;如果她的列表中不存在第 jj 个排列,则输出 −1-1。

输入输出样例

  • 输入#1

    2
    3 1 9
    1 2 3
    1 1
    2 1
    3 1
    1 2
    2 2
    3 2
    1 3
    2 3
    3 3
    6 4 4
    6 5 4 3 1 2
    1 1
    3 14
    1 59
    2 6

    输出#1

    1
    2
    3
    1
    3
    2
    2
    1
    3
    1
    4
    -1
    5
  • 输入#2

    1
    12 4 2
    1 2 3 4 5 6 7 8 9 10 11 12
    2 20
    2 21

    输出#2

    2
    2

说明/提示

In the first test case, Alice wrote down the following permutations: [1,2,3][1, 2, 3], [1,3,2][1, 3, 2], [2,1,3][2, 1, 3].

Note that, for a permutation [3,2,1][3, 2, 1] Alice would have to reverse the whole array, and it would cost her 22, which is greater than the specified value c=1c=1. The other two permutations can not be obtained by performing exactly one operation described in the problem statement.

在第一个测试用例中,Alice 写下了以下排列:[1,2,3][1, 2, 3]、[1,3,2][1, 3, 2]、[2,1,3][2, 1, 3]。

注意,对于排列 [3,2,1][3, 2, 1],Alice 需要将整个数组反转,其代价为 22,大于给定的值 c=1c=1。其余两个排列无法通过恰好执行一次题目描述中的操作得到。

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

首页