#P1160. 队列安排
队列安排
Description
In a school, a teacher wants to arrange students in a line. The students are numbered . The teacher proceeds as follows:
-
First, student is placed in the queue. At this moment, the queue contains only that student.
-
Students join the queue one by one. For student numbered , the teacher specifies that student stands to the left or right of some student among (i.e., a student who has already joined the queue).
-
Remove 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 , the number of students.
Lines : line contains two integers , where is a positive integer less than , and is or . If is , insert student to the left of student ; if is , insert student to the right of student .
Line contains an integer , the number of students to remove.
The next lines each contain a positive integer , meaning student is removed from the queue. If student is already not in the queue, ignore this instruction.
Output Format
One line containing at most 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 to the left of student . The queue is now:
2 1
Insert student to the right of student . The queue is now:
2 3 1
Insert student to the left of student . The queue is now:
2 3 4 1
Remove student . The queue is now:
2 4 1
Student is no longer in the queue, so ignore the last instruction.
Final queue:
2 4 1
Constraints
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号