#P16019. [ICPC 2021 NAC] Permutation CFG

[ICPC 2021 NAC] Permutation CFG

题目描述

Consider a permutation of the integers 11 to nn. Now, consider each number 11 through nn to be a non-terminal in a Context-Free Grammar (CFG). Each number kk expands a list of the integers from 11 to kk in the order of the permutation. For example, if n=4n = 4 and the permutation is 1 4 3 21\ 4\ 3\ 2:

$$\begin{aligned} 1 & \Longrightarrow 1 \\ 2 & \Longrightarrow 1\ 2 \\ 3 & \Longrightarrow 1\ 3\ 2 \\ 4 & \Longrightarrow 1\ 4\ 3\ 2 \end{aligned}$$

Now, consider a process of starting with nn, and at each step, applying these rules to create a new list of integers. In the above example, at the first step:

1 4 3 24\overbrace{1\ 4\ 3\ 2}^{4}

At the second step:

$$\overbrace{1}^{1}\ \overbrace{1\ 4\ 3\ 2}^{4}\ \overbrace{1\ 3\ 2}^{3}\ \overbrace{1\ 2}^{2}$$

At the third step:

$$\overbrace{1}^{1}\ \overbrace{1}^{1}\ \overbrace{1\ 4\ 3\ 2}^{4}\ \overbrace{1\ 3\ 2}^{3}\ \overbrace{1\ 2}^{2}\ \overbrace{1}^{1}\ \overbrace{1\ 3\ 2}^{3}\ \overbrace{1\ 2}^{2}\ \overbrace{1}^{1}\ \overbrace{1\ 2}^{2}$$

Given a permutation, a number of steps, and a list of queries asking for the number of occurrences of a particular integer in a prefix of the list created by the process, answer all of the queries.

输入格式

The first line of input contains three integers, nn (2n1052 \le n \le 10^5), ss (1s51 \le s \le 5) and qq (1q21051 \le q \le 2 \cdot 10^5), where nn is the size of the permutation, ss is the number of steps to apply the process, and qq is the number of queries.

Each of the next nn lines contains a single integer pp (1pn1 \le p \le n). This is the permutation, in order. All of the values of pp will be distinct.

Each of the next qq lines contains two integers kk (1kn1 \le k \le n) and aa (1a1091 \le a \le 10^9, aa will not exceed the length of the final list). This is a query for the number of occurrences of the integer kk in the first aa elements of the list created by the process.

输出格式

Output qq lines, each with a single integer, which are the answers to the queries in the order that they appear in the input.

4 3 6
1
4
3
2
1 6
2 20
4 1
3 5
2 9
1 1
3
6
0
1
2
8