#P1160. 队列安排

队列安排

Description

In a school, a teacher wants to arrange NN students in a line. The students are numbered 1N1 \sim N. The teacher proceeds as follows:

  1. First, student 11 is placed in the queue. At this moment, the queue contains only that student.

  2. Students 2N2 \sim N join the queue one by one. For student numbered ii, the teacher specifies that student ii stands to the left or right of some student among 1(i1)1 \sim (i - 1) (i.e., a student who has already joined the queue).

  3. Remove MM students from the queue; the relative order of the remaining students does not change.

After all students have been arranged according to the above rules, the teacher wants to know the IDs of all students from left to right.

Input Format

The first line contains an integer NN, the number of students.

Lines 2N2 \sim N: line ii contains two integers k,pk, p, where kk is a positive integer less than ii, and pp is 00 or 11. If pp is 00, insert student ii to the left of student kk; if pp is 11, insert student ii to the right of student kk.

Line N+1N + 1 contains an integer MM, the number of students to remove.

The next MM lines each contain a positive integer xx, meaning student xx is removed from the queue. If student xx is already not in the queue, ignore this instruction.

Output Format

One line containing at most NN space-separated integers, the IDs of the students from left to right.

4
1 0
2 1
1 0
2
3
3
2 4 1

Hint

[Sample Explanation]

Insert student 22 to the left of student 11. The queue is now:

2 1

Insert student 33 to the right of student 22. The queue is now:

2 3 1

Insert student 44 to the left of student 11. The queue is now:

2 3 4 1

Remove student 33. The queue is now:

2 4 1

Student 33 is no longer in the queue, so ignore the last instruction.

Final queue:

2 4 1

Constraints

  • For 20%20\% of the testdata, 1N101 \leq N \leq 10.
  • For 40%40\% of the testdata, 1N10001 \leq N \leq 1000.
  • For 100%100\% of the testdata, 1<MN1051 < M \leq N \leq 10^5.

Translated by ChatGPT 5