Permute K times

AtCoder
IDabc367_e
Time2000ms
Memory256MB
Difficulty
You are given a sequence $X$ of length $N$ where each element is between $1$ and $N$, inclusive, and a sequence $A$ of length $N$. Print the result of performing the following operation $K$ times on $A$. * Replace $A$ with $B$ such that $B_i = A_{X_i}$. ## Constraints * All input values 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$ ## Input The input is given from Standard Input in the following format: $N$ $K$ $X_1$ $X_2$ $\dots$ $X_N$ $A_1$ $A_2$ $\dots$ $A_N$ [samples]
Samples
Input #1
7 3
5 2 6 3 1 4 6
1 2 3 5 7 9 11
Output #1
7 2 3 5 1 9 3

In this input, $X=(5,2,6,3,1,4,6)$ and the initial sequence is $A=(1,2,3,5,7,9,11)$.

*   After one operation, the sequence is $(7,2,9,3,1,5,9)$.
*   After two operations, the sequence is $(1,2,5,9,7,3,5)$.
*   After three operations, the sequence is $(7,2,3,5,1,9,3)$.
Input #2
4 0
3 4 1 2
4 3 2 1
Output #2
4 3 2 1

There may be cases where no operations are performed.
Input #3
9 1000000000000000000
3 7 8 5 9 3 7 4 2
9 9 8 2 4 4 3 5 3
Output #3
3 3 3 3 3 3 3 3 3
API Response (JSON)
{
  "problem": {
    "name": "Permute K times",
    "description": {
      "content": "You are given a sequence $X$ of length $N$ where each element is between $1$ and $N$, inclusive, and a sequence $A$ of length $N$.   Print the result of performing the following operation $K$ times on",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc367_e"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a sequence $X$ of length $N$ where each element is between $1$ and $N$, inclusive, and a sequence $A$ of length $N$.  \nPrint the result of performing the following operation $K$ times on...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments