#P16019. [ICPC 2021 NAC] Permutation CFG
[ICPC 2021 NAC] Permutation CFG
题目描述
Consider a permutation of the integers to . Now, consider each number through to be a non-terminal in a Context-Free Grammar (CFG). Each number expands a list of the integers from to in the order of the permutation. For example, if and the permutation is :
$$\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 , and at each step, applying these rules to create a new list of integers. In the above example, at the first step:
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, (), () and (), where is the size of the permutation, is the number of steps to apply the process, and is the number of queries.
Each of the next lines contains a single integer (). This is the permutation, in order. All of the values of will be distinct.
Each of the next lines contains two integers () and (, will not exceed the length of the final list). This is a query for the number of occurrences of the integer in the first elements of the list created by the process.
输出格式
Output 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