CF1602B.Divine Array

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Black is gifted with a Divine array aa consisting of nn (1≤n≤20001 \le n \le 2000) integers. Each position in aa has an initial value. After shouting a curse over the array, it becomes angry and starts an unstoppable transformation.

The transformation consists of infinite steps. Array aa changes at the ii-th step in the following way: for every position jj, aja_j becomes equal to the number of occurrences of aja_j in aa before starting this step.

Here is an example to help you understand the process better:

Initial array:

22 11 11 44 33 11 22

After the 11-st step:

22 33 33 11 11 33 22

After the 22-nd step:

22 33 33 22 22 33 22

After the 33-rd step:

44 33 33 44 44 33 44

...

...

In the initial array, we had two 22-s, three 11-s, only one 44 and only one 33, so after the first step, each element became equal to the number of its occurrences in the initial array: all twos changed to 22, all ones changed to 33, four changed to 11 and three changed to 11.

The transformation steps continue forever.

You have to process qq queries: in each query, Black is curious to know the value of axa_x after the kk-th step of transformation.

Black 拥有一个神圣数组 aa,该数组由 nn(1≤n≤20001 \le n \le 2000)个整数组成。数组 aa 的每个位置都有一个初始值。在对数组施放一道咒语后,它变得愤怒,并开始一场不可阻挡的变换。

该变换由无限多步组成。在第 ii 步中,数组 aa 按如下方式变化:对每个位置 jj,aja_j 的新值等于在本步开始前整个数组 aa 中值为 aja_j 的元素出现次数。

以下示例有助于你更好地理解该过程:

初始数组:

22 11 11 44 33 11 22

第 11 步后:

22 33 33 11 11 33 22

第 22 步后:

22 33 33 22 22 33 22

第 33 步后:

44 33 33 44 44 33 44

...

...

在初始数组中,数字 22 出现了两次,数字 11 出现了三次,数字 44 和 33 各出现一次。因此,在第 11 步中,每个元素均被替换为其在初始数组中的出现频次:所有 22 变为 22,所有 11 变为 33,44 变为 11,33 也变为 11。

该变换将永远持续下去。

你需要处理 qq 个查询:对每个查询,Black 希望知道经过变换的第 kk 步后,位置 xx 上的元素 axa_x 的值。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10001 \le t \le 1000). Description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤20001 \le n \le 2000) — the size of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤n1 \le a_i \le n) — the initial values of array aa.

The third line of each test case contains a single integer qq (1≤q≤100 0001 \le q \le 100\,000) — the number of queries.

Next qq lines contain the information about queries — one query per line. The ii-th line contains two integers xix_i and kik_i (1≤xi≤n1 \le x_i \le n; 0≤ki≤1090 \le k_i \le 10^9), meaning that Black is asking for the value of axia_{x_i} after the kik_i-th step of transformation. ki=0k_i = 0 means that Black is interested in values of the initial array.

It is guaranteed that the sum of nn over all test cases doesn't exceed 20002000 and the sum of qq over all test cases doesn't exceed 100 000100\,000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤10001 \le t \le 1000)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤20001 \le n \le 2000)—— 数组 aa 的大小。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤n1 \le a_i \le n)—— 数组 aa 的初始值。

每个测试用例的第三行包含一个整数 qq(1≤q≤100 0001 \le q \le 100\,000)—— 查询的数量。

接下来的 qq 行描述查询信息,每行一个查询。第 ii 行包含两个整数 xix_i 和 kik_i(1≤xi≤n1 \le x_i \le n;0≤ki≤1090 \le k_i \le 10^9),表示 Black 想知道经过 kik_i 次变换后 axia_{x_i} 的值。其中 ki=0k_i = 0 表示 Black 关心的是数组的初始值。

保证所有测试用例的 nn 之和不超过 20002000,且所有测试用例的 qq 之和不超过 100 000100\,000。

输出格式

For each test case, print qq answers. The ii-th of them should be the value of axia_{x_i} after the kik_i-th step of transformation. It can be shown that the answer to each query is unique.

对于每个测试用例,输出 qq 个答案。其中第 ii 个答案应为经过第 kik_i 步变换后 axia_{x_i} 的值。可以证明,每个查询的答案是唯一的。

输入输出样例

  • 输入#1

    2
    7
    2 1 1 4 3 1 2
    4
    3 0
    1 1
    2 2
    6 1
    2
    1 1
    2
    1 0
    2 1000000000

    输出#1

    1
    2
    3
    3
    1
    2

说明/提示

The first test case was described ih the statement. It can be seen that:

  1. k1=0k_1 = 0 (initial array): a3=1a_3 = 1;
  2. k2=1k_2 = 1 (after the 11-st step): a1=2a_1 = 2;
  3. k3=2k_3 = 2 (after the 22-nd step): a2=3a_2 = 3;
  4. k4=1k_4 = 1 (after the 11-st step): a6=3a_6 = 3.

For the second test case,

Initial array:

11 11

After the 11-st step:

22 22

After the 22-nd step:

22 22

...

...

It can be seen that:

  1. k1=0k_1 = 0 (initial array): a1=1a_1 = 1;
  2. k2=1000000000k_2 = 1000000000: a2=2a_2 = 2;

第一个测试用例已在题目描述中给出。可以观察到:

  1. k1=0k_1 = 0(初始数组):a3=1a_3 = 1;
  2. k2=1k_2 = 1(第 11 步之后):a1=2a_1 = 2;
  3. k3=2k_3 = 2(第 22 步之后):a2=3a_2 = 3;
  4. k4=1k_4 = 1(第 11 步之后):a6=3a_6 = 3。

对于第二个测试用例,

初始数组:

11 11

第 11 步之后:

22 22

第 22 步之后:

22 22

...

...

可以观察到:

  1. k1=0k_1 = 0(初始数组):a1=1a_1 = 1;
  2. k2=1000000000k_2 = 1000000000:a2=2a_2 = 2;

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

首页