/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
高橋君のクラスには N 人の生徒がおり、生徒には 1 から N までの番号が付けられています。
このクラスでは「伝言リレー」というゲームを行います。ゲームの準備として、各生徒 i (1 \leq i \leq N) には受け渡し先として自分以外のちょうど1人の生徒 P_i が決められています(P_i \neq i)。受け渡し先とは、自分がクイズを解けなかったときにそのクイズを次に渡す相手のことです。
ゲームの開始時に、各生徒 i にクイズが1問ずつ配られます。生徒 i に配られるクイズの難易度は D_i です。また、各生徒 i には学力 S_i が定められており、生徒 i は難易度が S_i 以下のクイズであれば解くことができます。
配られた N 問のクイズは、それぞれ独立に以下のルールに従って処理されます。各クイズの処理ごとに訪問済みの記録は独立に管理され、あるクイズの処理で訪問済みとなった記録が他のクイズの処理に影響することはありません。 そのため、同じ生徒が異なるクイズの処理においてそれぞれ訪問されることがあり、複数のクイズを解くこともあり得ます。
生徒 i に配られたクイズの処理は、以下のように行われます。
はじめに、j = i とし、生徒 i を訪問済みとします。その後、以下の手順を繰り返します:
- クイズの難易度が S_j 以下であれば、生徒 j がそのクイズを解く。このクイズの処理はここで終了する。
- そうでなければ、クイズを受け渡し先の生徒 P_j に回そうとする。
- 生徒 P_j がすでに訪問済みであれば、受け渡しがループするため、そのクイズは誰にも解かれず未回答となる。このクイズの処理はここで終了する。
- 生徒 P_j がまだ訪問済みでなければ、生徒 P_j を訪問済みとし、j を P_j に置き換えて手順 1 から繰り返す。
すべてのクイズについて、そのクイズを最終的に解く生徒の番号を求めてください。未回答となる場合は -1 を出力してください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq P_i \leq N
- P_i \neq i (1 \leq i \leq N)
- 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
- 入力はすべて整数である。
入力
N P_1 P_2 \cdots P_N S_1 S_2 \cdots S_N D_1 D_2 \cdots D_N
- 1 行目には、生徒の人数を表す整数 N が与えられる。
- 2 行目には、各生徒 i (1 \leq i \leq N) のクイズの受け渡し先を表す整数 P_i が、スペース区切りで N 個与えられる。
- 3 行目には、各生徒 i (1 \leq i \leq N) の学力を表す整数 S_i が、スペース区切りで N 個与えられる。
- 4 行目には、各生徒 i (1 \leq i \leq N) に配られるクイズの難易度を表す整数 D_i が、スペース区切りで N 個与えられる。
出力
N 個の整数をスペース区切りで1行に出力せよ。i 番目 (1 \leq i \leq N) の整数は、生徒 i に最初に配られたクイズを最終的に解く生徒の番号とする。誰にも解かれない場合は -1 とする。
入力例 1
4 2 3 4 2 3 5 2 10 2 6 9 1
出力例 1
1 4 4 4
入力例 2
3 2 3 1 1 1 1 5 5 5
出力例 2
-1 -1 -1
入力例 3
10 2 3 4 5 3 7 8 6 10 8 4 8 2 10 6 7 3 9 5 1 1 9 6 11 5 8 4 10 2 7
出力例 3
1 4 4 -1 5 8 8 -1 9 8
入力例 4
25 2 3 4 5 6 3 8 9 10 7 12 13 14 15 16 17 18 19 15 21 22 23 24 25 20 12 5 30 18 7 25 40 10 22 35 6 14 50 9 27 31 3 45 20 100 1 60 15 80 33 4 26 19 8 50 24 36 11 41 2 55 13 49 28 32 44 5 70 21 90 61 16 79 34 101
出力例 4
1 3 3 4 -1 6 7 9 -1 10 -1 12 13 16 18 18 18 -1 15 20 24 22 24 24 -1
入力例 5
2 2 1 1 1000000000 1000000000 1
出力例 5
2 2
Score : 466 pts
Problem Statement
There are N students in Takahashi's class, numbered 1 to N.
In this class, they will play a game called "Message Relay". As preparation for the game, each student i (1 \leq i \leq N) is assigned exactly one other student P_i as their recipient (P_i \neq i). The recipient is the person to whom a student passes a quiz when they cannot solve it.
At the start of the game, each student i is given one quiz. The difficulty of the quiz given to student i is D_i. Additionally, each student i has an ability S_i, and student i can solve a quiz if its difficulty is at most S_i.
The N quizzes are processed independently according to the following rules. The record of visited students is managed independently for each quiz's process; the visited records of one quiz's process do not affect those of other quizzes. Thus, the same student can be visited in different quiz processes and can solve multiple quizzes.
The process for the quiz given to student i is as follows:
Initially, set j = i, and mark student i as visited. Then, repeat the following steps:
- If the difficulty of the quiz is at most S_j, student j solves the quiz. The process for this quiz ends.
- Otherwise, they attempt to pass the quiz to their recipient, student P_j.
- If student P_j is already visited, the passing would loop, so the quiz remains unsolved by anyone. The process for this quiz ends.
- If student P_j is not yet visited, mark student P_j as visited, replace j with P_j, and repeat from Step 1.
For each quiz, find the student number of the student who finally solves it. If a quiz remains unsolved, output -1.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq P_i \leq N
- P_i \neq i (1 \leq i \leq N)
- 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq D_i \leq 10^9 (1 \leq i \leq N)
- All input values are integers.
Input
N P_1 P_2 \cdots P_N S_1 S_2 \cdots S_N D_1 D_2 \cdots D_N
- The first line contains an integer N, representing the number of students.
- The second line contains N space-separated integers representing the recipient P_i for each student i (1 \leq i \leq N).
- The third line contains N space-separated integers representing the ability S_i of each student i (1 \leq i \leq N).
- The fourth line contains N space-separated integers representing the difficulty D_i of the quiz initially given to each student i (1 \leq i \leq N).
Output
Print N integers in a single line, separated by spaces. The i-th integer (1 \leq i \leq N) should be the student number of the student who finally solves the quiz initially given to student i, or -1 if the quiz remains unsolved.
Sample Input 1
4 2 3 4 2 3 5 2 10 2 6 9 1
Sample Output 1
1 4 4 4
Sample Input 2
3 2 3 1 1 1 1 5 5 5
Sample Output 2
-1 -1 -1
Sample Input 3
10 2 3 4 5 3 7 8 6 10 8 4 8 2 10 6 7 3 9 5 1 1 9 6 11 5 8 4 10 2 7
Sample Output 3
1 4 4 -1 5 8 8 -1 9 8
Sample Input 4
25 2 3 4 5 6 3 8 9 10 7 12 13 14 15 16 17 18 19 15 21 22 23 24 25 20 12 5 30 18 7 25 40 10 22 35 6 14 50 9 27 31 3 45 20 100 1 60 15 80 33 4 26 19 8 50 24 36 11 41 2 55 13 49 28 32 44 5 70 21 90 61 16 79 34 101
Sample Output 4
1 3 3 4 -1 6 7 9 -1 10 -1 12 13 16 18 18 18 -1 15 20 24 22 24 24 -1
Sample Input 5
2 2 1 1 1000000000 1000000000 1
Sample Output 5
2 2