This page is still under construction.

Parts of this page are still being built. What you see may change.

Ants on a Circle

Time limit1sMemory limit512 MB

Summary
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 MM 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 NN (≥M\ge M) equal parts and numbered them 1, 2, 3, …\ldots, NN in clockwise order. Reading the point numbers clockwise gives 1, 2, 3, …\ldots, NN, and after point NN 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 NN 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 PP gets stepped on at least XX 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 NN (2≤N≤1052 \le N \le 10^5), the number of ants MM (1≤M≤N1 \le M \le N), and the number of queries Taekhee is curious about QQ (1≤Q≤1051 \le Q \le 10^5).

The next MM lines give each ant's initial position PiP_i and direction did_i. (1≤Pi≤N1 \le P_i \le N, di=0d_i = 0 or 11)

All PiP_i are distinct.

If di=0d_i = 0, the ant moves clockwise, and if di=1d_i = 1, it moves counterclockwise.

The next QQ lines give the queries PP XX that Taekhee is curious about. (1≤P≤N1 \le P \le N, 1≤X≤1091 \le X \le 10^9)

This means he wants to know the earliest time at which point PP gets stepped on at least XX times.

Output

Over QQ lines, output the answer to each query. The start time is 0.

Examples1

  1. Example 1

    Input
    4 2 3
    2 0
    4 1
    3 4
    1 2
    2 1
    
    Expected output
    5
    3
    0