Madoka and the Sixth-graders
Contest: Codeforces Round 777 (Div. 2)
Difficulty rating: 2500
Tags: data structures, dfs and similar, greedy

After the most stunning success with the fifth-graders, Madoka has been trusted with teaching the sixth-graders.

There's $$$n$$$ single-place desks in her classroom. At the very beginning Madoka decided that the student number $$$b_i$$$ ($$$1 \le b_i \le n$$$) will sit at the desk number $$$i$$$. Also there's an infinite line of students with numbers $$$n + 1, n + 2, n + 3, \ldots$$$ waiting at the door with the hope of being able to learn something from the Madoka herself. Pay attention that each student has his unique number.

After each lesson, the following happens in sequence.

1. The student sitting at the desk $$$i$$$ moves to the desk $$$p_i$$$. All students move simultaneously.
2. If there is more than one student at a desk, the student with the lowest number keeps the place, and the others are removed from the class forever.
3. For all empty desks in ascending order, the student from the lowest number from the outside line occupies the desk.

Note that in the end there is exactly one student at each desk again. It is guaranteed that the numbers $$$p$$$ are such that at least one student is removed after each lesson. Check out the explanation to the first example for a better understanding.

After several (possibly, zero) lessons the desk $$$i$$$ is occupied by student $$$a_i$$$. Given the values $$$a_1, a_2, \ldots, a_n$$$ and $$$p_1, p_2, \ldots, p_n$$$, find the lexicographically smallest suitable initial seating permutation $$$b_1, b_2, \ldots, b_n$$$.

The permutation is an array of $$$n$$$ different integers from $$$1$$$ up to $$$n$$$ in any order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not ($$$2$$$ occurs twice). $$$[1,3,4]$$$ is not a permutation either ($$$n=3$$$ but there's $$$4$$$ in the array).

For two different permutations $$$a$$$ and $$$b$$$ of the same length, $$$a$$$ is lexicographically less than $$$b$$$ if in the first position where $$$a$$$ and $$$b$$$ differ, the permutation $$$a$$$ has a smaller element than the corresponding element in $$$b$$$.

Input format:
The first line of input data contains an integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — a number of desks in the classroom.

The second line contains $$$n$$$ integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \leq p_i \leq n$$$) — desks where the students move. It is guaranteed that $$$p$$$ has at least two equal elements.

The third line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the final seating of the students. It is guaranteed that there is an initial permutation from which the seating $$$a$$$ can be obtained.

Output format:
In the only line print $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$1 \le b_i \le n$$$) — lexicographically minimum permutation describing the initial seating of the sixth-graders that can lead to the final seating $$$a$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
The description of the first test is below:

The first picture shows the starting permutation, which is the answer. Then the students sitting at desks $$$1, 2$$$ are transferred to a $$$5$$$ desk. Also, a $$$1$$$ student moved from a $$$5$$$ desk, and a student from a $$$4$$$ disk is transferred to a $$$3$$$ desk.

Thus, after all these transfers permutation shown in the second image is obtained. Then, at the desk with the number $$$5$$$, the student with the number $$$3$$$ is expelled, and at the desk with the number $$$3$$$, the student with the number $$$5$$$ is expelled. (Since their numbers are not the smallest) Then new students with numbers $$$6, 7$$$ sit at desks numbered $$$2, 4$$$. And this permutation (after the end of the first lesson) is shown in the third image.

The $$$4$$$ image shows the seating arrangement, after the second lesson before all the extra ones were kicked out. And the fifth shows the final seating after $$$2$$$ lesson.

Example input:
5
5 5 3 3 1
1 8 2 9 4

Example output:
1 3 2 5 4

Example input:
5
1 3 2 5 2
3 2 5 4 1

Example output:
3 2 5 4 1

Editorial:
It is not difficult to understand how many transplants there were in total, since the number of the maximum student increases by the number of desks that no one sits at. Then let the number of such transplants  — be $$$k$$$.
Then let $$$to_i$$$ — be the desk to which the student will move after $$$k$$$ transplantation, who originally sat at $$$i$$$ desk, if students with numbers less than or equal to $$$n$$$ were not kicked out. It is not difficult to calculate be using binary liffting, or you can find all cycles and make an unpleasant dfs on this.
Let's understand that if someone is transferred to the $$$i$$$ desk after $$$k$$$ operations, then the value in it is  — $$$min(b_v)$$$, for all $$$v$$$ that have $$$to_v = i$$$. And from this follows an understandable greedy starting seating of schoolchildren:
Let $$$s$$$ — be a set of schoolchildren for whom we have not yet chosen the desk at which they are sitting. We will iterate over $$$i$$$ from $$$1$$$ in ascending order. Then you need to understand who should sit at the $$$i$$$ desk. If we know that there is a desk for which $$$min(b_v) = i$$$, let the set $$$V$$$ contain all the numbers of desks $$$i$$$, where $$$to_i = v$$$, then we must put one person from this set at this desk (since the minimum is strictly equal to $$$i$$$), and put the rest at desks with large numbers (otherwise the minimum would be less than $$$i$$$). So we should take the student with the smallest number from the set $$$V$$$ and put him at the $$$i$$$ desk, and add the rest to $$$s$$$. Otherwise, we just need to take a person from $$$s$$$ with the minimum number, put him in a place under the number $$$i$$$, and then simply remove him from the set $$$V$$$.