Rotating Line
Time limit1sMemory limit512 MB
Simulate a line rotating counter-clockwise around its center through up to 3000 integer points, and report the q-th rotation center for each query.
- Level
Hard8 of 10
- Topics
- Geometry, Sorting, Simulation
- Solved
- No attempts yet
Problem
Consider an infinite two-dimensional plane. There are fixed points and one rotating line on the plane. One of the points is , the origin, and the line initially coincides with the axis. The points are distinct, but it is not guaranteed that no three points lie on the same line.
The line rotates counter-clockwise. The centre of rotation is always one of the given points, and it is initially the origin. It may change. While the line contains only one point, the rotation centre is that point. As soon as the line meets another point, consider all given points currently on the line, sorted by their coordinates . If there are points on the line and the current rotation centre is the -th of them (counting from 1), the next rotation centre is the -th of these points. If , the rotation centre does not change. The line starts rotating even if several given points already lie on it at the start.
Your task is to find the -th rotation centre. The initial centre is numbered 0. As long as the centre does not change, the counter does not increase. It is guaranteed that the -th rotation centre exists for the given set of points.
Input
The first line contains an integer , the number of points ().
Each of the following lines contains two integers and , the coordinates of a point (). One of these points is . The points are distinct, but it is not guaranteed that no three points lie on the same line.
The next line contains an integer , the number of queries (). Each of the following lines contains a single integer (). The queries are given in strictly ascending order.
Output
Print lines, one for each query. Each line contains a pair of integers: the coordinates of the -th rotation centre.
Hint
The initial rotation centre (numbered 0) is . The rotation centre number 1 is , because it is the first point the line meets.
Then the line meets two points and at the same time, so there are three points on the line. is the first of them in sorted order, so the centre changes to the third point, . Thus, is the rotation centre number 2.
Then the line meets three points , , and , so there are four points on the line. The centre is the third of them in sorted order, so the centre number 3 is the second of the four points, .
Similarly, the 4th rotation centre is .
After that, the line meets two points and . The centre is the second of three points in sorted order, so it does not change.
After that, the line meets , so the 5th rotation centre is .