Permutation Representation#1751

Permutation Representation

10 secondi
64 megabyte
Facile

A permutation is a bijection from a set onto itself. If is finite, the elements of are often numbered , , , , . A permutation of a set with five elements is often denoted by

meaning the element is mapped to element , the element to element , and so on. Another way to denote permutations is to use cycle notation. Cycle notation is not necessarily unique. The following cycle

means that the element is mapped to , is mapped to , and is mapped to . This cycle could also be written as

The product of several cycles is evaluated from right to left. The above permutation can be written as

(5\ 3)\ (5\ 1)\ (5\ 4)
(1\ 3\ 5\ 4)\ (1)
(1)\ (1\ 3\ 5\ 4)

A permutation can be written uniquely as the product of cycles

if 0 \leq a_i \leq i - 1 holds for each exponent a_i. The permutation above can be uniquely written as

Your task is to compute the a_i values for a given permutation.

Input

The input consists of several test cases. Each test case consists of three lines. The first line contains (1 \leq n \leq 200\,000). The second line contains the elements from to . The third line contains, for each element in the second line, the image under the permutation.

Output

For each test case, output a single line containing the values a_1 \ldots a_n, separated by spaces.

Esempi

Esempio 1

Loading
Loading