#nt504. Ray Tracing
Ray Tracing
Ray Tracing
Problem Source
Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2) C
Problem Description
There are sensors located in a rectangular room of size metres. The -th sensor is located at point . All sensors are at distinct points strictly inside the rectangle.
Opposite corners of the room are at points and . The walls of the room are parallel to the coordinate axes.
At time , a laser ray is released from point in the direction of point . The ray travels at a speed of metres per second, so it reaches point exactly one second after the start.
When the ray meets a wall it is reflected by the rule that the angle of incidence equals the angle of reflection. If the ray reaches any of the four corners, it stops immediately.
For each sensor, determine the first moment in time when the ray passes through the point where that sensor is located. If the ray never passes through that point, print for that sensor.
Input Format
The first line contains three integers (, ): the lengths of the room's walls and the number of sensors.
Each of the following lines contains two integers (, ): the coordinates of a sensor. It is guaranteed that no two sensors are at the same point.
Output Format
Print integers. The -th of them should be the number of seconds when the ray first passes through the position of the -th sensor, or if this never happens.
Sample Input 1
3 3 4
1 1
1 2
2 1
2 2
Sample Output 1
1
-1
-1
2
Sample Input 2
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
Sample Output 2
1
-1
-1
2
5
-1
Sample Input 3
7 4 5
1 3
2 2
5 1
5 3
4 3
Sample Output 3
13
2
9
5
-1