#P9660. [ICPC2021 Macao R] Pass the Ball!

    ID: 9022 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2021多项式O2优化快速傅里叶变换 FFT快速数论变换 NTTICPC澳门

[ICPC2021 Macao R] Pass the Ball!

题目描述

There are nn children playing with nn balls. Both children and balls are numbered from 11 to nn.

Before the game, nn integers p1,p2,,pnp_1, p_2, \cdots, p_n are given. In each round of the game, child ii will pass the ball he possesses to child pip_i. It is guaranteed that no child will pass his ball to himself, which means piip_i \neq i. Moreover, we also know that after each round, each child will hold exactly one ball.

Let bib_i be the ball possessed by child ii. At the beginning of the game, child ii (1in1 \le i \le n) will be carrying ball ii, which means bi=ib_i=i initially. You're asked to process qq queries. For each query you're given an integer kk and you need to compute the value of i=1ni×bi\sum\limits_{i=1}^{n} i \times b_i after kk rounds.

输入格式

There is only one test case for each test file.

The first line of the input contains two integers nn (2n1052 \le n \le 10^5) and qq (1q1051 \le q \le 10^5), indicating the number of children and the number of queries.

The second line contains nn integers p1,p2,,pnp_1, p_2, \cdots, p_n (1pin1 \le p_i \le n) indicating how the children pass the balls around.

For the following qq lines, the ii-th line contains one integer kik_i (1ki1091 \le k_i \le 10^9) indicating a query asking for the result after kik_i rounds.

输出格式

For each query output one line containing one integer indicating the answer.

4 4
2 4 1 3
1
2
3
4
25
20
25
30

提示

The sample test case is explained below.

$$\begin{array}{|c|c|c|c|c|c|} \hline \textbf{Round} & \textbf{b1} & \textbf{b2} & \textbf{b3} & \textbf{b4} & \textbf{Answer} \\\hline 1 & 3 & 1 & 4 & 2 & 25 \\\hline 2 & 4 & 3 & 2 & 1 & 20 \\\hline 3 & 2 & 4 & 1 & 3 & 25 \\\hline 4 & 1 & 2 & 3 & 4 & 30 \\\hline \end{array} $$