ACM NEERC 2019
Galina is an aspiring golfer and spends all her free time at a minigolf park. Her favorite golf course in this park is a rectangle, with one of its sides parallel to the north-south direction, and the other one parallel to the east-west direction. The ball is really small and can be considered a point.
Galina wants to practice a specific shot called "put". In this shot, a ball is placed somewhere inside the golf course and hit north-east, obtaining the speed of sqrt(2) inches per second. This means that the ball starts moving by exactly 1 inch to the north and 1 inch to the east per second.
The golf park uses an innovative technology that eliminates all friction, and the ball will continue to move at the initial speed regardless of the travelled distance. Collisions with golf course borders will be perfectly elastic. Fancy!
Inside the course there is a pond that is represented as a simple polygon with edges parallel to the sides of the golf course. If the ball touches the pond, it sinks momentarily.
You are given a list of starting coordinates for Galina’s next put training session. For each starting position, assuming a put is shot from this place, calculate how long the ball will travel before falling into the pond and find exactly where it happens, or determine that the ball will move indefinitely.
The first line contains two integers w and h (4 ≤ w, h ≤ 5 *
108), the width and the height of the golf course in inches. A coordinate system is introduced in such a way that the corners of the golf course have coordinates (0, 0), (w, 0), (w, h), (0, h), and the point (1, 1) is to the north-east of the point (0, 0).
The second line contains a single integer n (4 ≤ n ≤ 1000), denoting the number of vertices in the polygon. Each of the following n lines contains two integers
yi (1 ≤
xi ≤ w − 1, 1 ≤
yi ≤ h − 1), denoting the coordinates of the i-th vertex. The vertices are listed in traversal order.
All polygon vertices are distinct and none of them lie at the polygon's edge. All polygon edges are either vertical (
x1) or horizontal (
y1), and none of them intersects another.
The next line contains a single integer t (1 ≤ t ≤ 100), denoting the number of starting points for a shot. Each of the following t lines contains two integers
_yi_ (1 ≤
_xi_ ≤ w − 1, 1 ≤
_yi_ ≤ h − 1), denoting the coordinates of the i-th starting point. No starting point lies inside or on the border of the pond.
Note that all starting points are independent of each other. In particular, you can assume that there is exactly one ball inside the golf course at any moment.
For each starting point in order of input, output either a single integer −1 if the ball will never sink, or three integers
τ xs ys, denoting the time before the ball sinks in seconds, and coordinates of the point where it happens (τ > 0, 1 ≤
xs ≤ w − 1, 1 ≤
ys ≤ h − 1). Point (
ys) must belong to the border of the pond.
10 8 6 1 4 1 5 3 5 3 6 4 6 4 4 4 2 2 1 7 4 7 7 5
2 4 4 3 4 6 13 3 4 15 2 4
6 6 4 2 2 4 2 4 4 2 4 3 5 5 5 2 5 5
3 4 4 -1 3 4 4