Student Canteen
Time limit1sMemory limit512 MB
Students numbered by arrival either join the back of a queue or cut in right ahead of an earlier student; report each cutter's 1-indexed position at that moment.
- Level
Medium6 of 10
- Topics
- Implementation, Linked list, Binary search
- Solved
- No attempts yet
Problem
Even before the student canteen opens, a queue forms in front of it. Students arrive at the queue one by one, and they are numbered from 1 to in the order they arrive. Watching the queue, you notice that some students do not go to the back of the queue the way they should. Instead, they rudely cut in next to their friends, who (just as rudely) let them pass ahead.
Write a program that reads the arrivals of students, which can be:
- student arrived at the back of the queue, or
- student cut in ahead of student .
For every cut-in (type B), print the position of student at that moment, counting from the front of the queue.
Input
The first line contains a positive integer (), the number of students.
Each of the following lines describes one student's arrival at the queue in front of the canteen. If the -th line contains the number 0, this is an arrival of type A. Otherwise the -th line contains the number (), and this is an arrival of type B. At least one arrival is of type B.
Output
For each arrival of type B, print the requested position of the student who has just cut into the queue, one per line.
Hint
The state of the queue in this example:
- (1),
- (1, 2),
- (1, 3, 2), we print position 2,
- (1, 4, 3, 2), we print position 2,
- (1, 4, 3, 5, 2), we print position 4.