P24803 [AtCoder] [ABC367E] Permute K times
普及+
题目描述

Problem Description

Given a sequence \( X \) of length \( N \) where each element is between \( 1 \) and \( N \), and a sequence \( A \) of length \( N \), output the result of performing the following operation \( K \) times on \( A \).

  • Let \( B_i = A_{X_i} \), then \( B \) becomes the new \( A \).

Input Format

The input is given from the standard input in the following format:

\( N \) \( K \) \( X_1 \) \( X_2 \) \( \dots \) \( X_N \) \( A_1 \) \( A_2 \) \( \dots \) \( A_N \)

Output Format

Let \( A' \) be the sequence \( A \) after the operations. Output \( A' \) in the following format:

\( A'_1 \) \( A'_2 \) \( \dots \) \( A'_N \)

Sample Input and Output

Sample Input #1

7 3
5 2 6 3 1 4 6
1 2 3 5 7 9 11

Sample Output #1

7 2 3 5 1 9 3

Sample Input #2

4 0
3 4 1 2
4 3 2 1

Sample Output #2

4 3 2 1

Sample Input #3

9 1000000000000000000
3 7 8 5 9 3 7 4 2
9 9 8 2 4 4 3 5 3

Sample Output #3

3 3 3 3 3 3 3 3 3

Notes/Hints

Constraints

  • All inputs are integers.
  • \( 1 \le N \le 2 \times 10^5 \)
  • \( 0 \le K \le 10^{18} \)
  • \( 1 \le X_i \le N \)
  • \( 1 \le A_i \le 2 \times 10^5 \)

Sample Explanation 1

In this input, \( X = (5, 2, 6, 3, 1, 4, 6) \) and the initial sequence \( A = (1, 2, 3, 5, 7, 9, 11) \).
- After 1 operation, the sequence becomes \( (7, 2, 9, 3, 1, 5, 9) \).
- After 2 operations, the sequence becomes \( (1, 2, 5, 9, 7, 3, 5) \).
- After 3 operations, the sequence becomes \( (7, 2, 3, 5, 1, 9, 3) \).

Sample Explanation 2

There might be cases where no operations are performed.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
7 3 5 2 6 3 1 4 6 1 2 3 5 7 9 11
样例输出
7 2 3 5 1 9 3