A relay race has n legs, numbered 1 through n. As each runner finishes, a spotter phones the finish time for that runner's leg in to the broadcast booth — but spotters call in whenever their runner finishes, so leg times can arrive in any order, and each leg is called in exactly once over the course of the race.
The booth can only announce leg times in leg order: leg 1's time must be announced before leg 2's, leg 2's before leg 3's, and so on. Keep a marker at the next leg still awaiting announcement (it starts at leg 1). Every time a new leg time is phoned in, announce every leg time that this call-in newly makes announceable: starting at the marker, announce leg times for as long as consecutive legs already have a recorded time, then advance the marker past everything just announced. If the leg at the marker still has no recorded time, announce nothing for this call.
Line 1: an integer n — the total number of legs. Line 2: an integer q — the number of call-ins. Next q lines: each contains two integers id and time — leg id's finish time is time. Each id (1 <= id <= n) appears in exactly one call-in across the whole input.
q lines. The i-th line corresponds to the i-th call-in (in input order): print the leg times newly announced by that call-in, space-separated in leg order, or print an empty line if nothing becomes announceable yet.
Example 1
Input
5 5 3 300 1 100 2 200 5 500 4 400
Expected
100 200 300 400 500
Explanation
Leg 3's time (300) arrives first, but the marker is still at leg 1 (unrecorded), so nothing is announced (empty line). Leg 1's time (100) arrives next: the marker is at leg 1, so 100 is announced and the marker moves to leg 2. Leg 2's time (200) arrives: the marker is at leg 2, so 200 is announced, the marker moves to leg 3, which is already recorded (300), so 300 is announced too, and the marker moves to leg 4 (unrecorded) — output '200 300'. Leg 5's time (500) arrives, but the marker is at leg 4 (unrecorded), so nothing is announced. Finally leg 4's time (400) arrives: the marker is at leg 4, so 400 is announced, the marker moves to leg 5, which is already recorded (500), so 500 is announced too — output '400 500'.
Example 2
Input
3 3 1 10 2 20 3 30
Expected
10 20 30
Explanation
Every leg arrives exactly in order, so each call-in immediately advances the marker by one and announces just that leg's own time: '10', then '20', then '30'.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →