Problem 686
Seat Reservation Manager
Medium · 20 pts · Design, Heap
Problem
A venue has N seats numbered from 1 to N, all initially free. Process Q operations. 'reserve' takes the smallest-numbered free seat, marks it reserved and prints its number. 'unreserve k' frees the reserved seat k. A reserve is only issued when a free seat exists, and an unreserve only names a reserved seat. Keep the freed seats in a min-heap together with a pointer to the next never-used seat, so each operation is O(log N).
Input
The first line contains N and Q. Each of the next Q lines contains one operation. There is at least one reserve.
Output
Print the seat number for each reserve, one per line.
Constraints
1 <= N, Q <= 100000; operations are always valid
Example input
5 8 reserve reserve unreserve 2 reserve reserve reserve reserve unreserve 5
Example output
1 2 2 3 4 5
Explanation
With 5 seats: the first two reserves get seats 1 and 2. After unreserve 2, the next reserve gets seat 2 again, then seats 3, 4 and 5 follow. The output is 1 2 2 3 4 5, one per line.