Ants on a Circle
Time limit1sMemory limit512 MB
Ants move on a circle of N points, reversing on collision; for each query (P, X) find the earliest time point P has been visited at least X times.
- Level
Hard8 of 10
- Topics
- Math, Simulation, Binary search, Prefix sum
- Solved
- No attempts yet
Problem
Taekhee has a circle and keeps ants that crawl around its circumference at a constant speed. Each ant is so small that its size can be ignored.
One day Taekhee marked the points that divide the circumference into () equal parts and numbered them 1, 2, 3, , in clockwise order. Reading the point numbers clockwise gives 1, 2, 3, , , and after point comes point 1. Then he placed the ants so that every ant stands on a different point. Each ant faces either clockwise or counterclockwise, and when Taekhee shouts "start," every ant moves in the direction it faces. The start time is 0, and from then on, exactly every 1 second, the ants arrive at the next point.
All ants move at the same speed, and when two ants meet each other, they immediately reverse direction and go back. The ants move forever, stepping on the points Taekhee marked.
Taekhee thought that, depending on the ants' initial positions and directions, some points would be stepped on relatively more often over the same amount of time. But since the ants never stop, he could not check whether such points actually exist.
At time 0, each ant is standing on its point, so every point where an ant initially stands has been stepped on 1 time at time 0, and every other point has been stepped on 0 times. Also, when two ants step on a point at the same time, that point counts as being stepped on twice at that time.
Taekhee decided he needs a program to test his hypothesis. Specifically, he became curious about the earliest time at which a point gets stepped on at least times, for several queries. For Taekhee, let us write a program that answers these queries quickly.
Input
The first line gives the number of points on the circumference (), the number of ants (), and the number of queries Taekhee is curious about ().
The next lines give each ant's initial position and direction . (, or )
All are distinct.
If , the ant moves clockwise, and if , it moves counterclockwise.
The next lines give the queries that Taekhee is curious about. (, )
This means he wants to know the earliest time at which point gets stepped on at least times.
Output
Over lines, output the answer to each query. The start time is 0.