Golf Time
Time limit2sMemory limit512 MB
For each start point, find when a diagonal ball bouncing off the course walls first hits the pond and where, or report that it never does.
- Level
Hard8 of 10
- Topics
- Simulation, Math, Number theory
- Solved
- No attempts yet
Problem
Galina is an aspiring golfer who spends most of her free time at a minigolf park. Her favorite course is a rectangle, with one side parallel to the north-south direction and the other parallel to the east-west direction. The ball is very small and can be treated as a point.
Galina wants to practice a shot called "put". In this shot, a ball is placed somewhere inside the course and hit north-east at a speed of inches per second. That means the ball moves 1 inch north and 1 inch east every second.
The park uses an innovative technology that removes all friction. The ball keeps its initial speed no matter how far it travels. Collisions with the course border are perfectly elastic.
A pond sits inside the course. It is a simple polygon whose edges are parallel to the sides of the course. The ball sinks the moment it touches the pond.
You are given the starting positions for Galina's next put practice session. For each starting position, find how long the ball travels before it falls into the pond, and find exactly where it falls in. If the ball moves forever, report that.
Input
The first line contains two integers and , the width and height of the course in inches, where . The coordinate system places the corners at , , , and , and the point is north-east of .
The second line contains , the number of vertices of the pond polygon ().
Each of the next lines contains the coordinates and of a vertex, listed in traversal order, where and . All vertices are distinct, and none of them lies on the polygon's edge. Every edge is vertical (, with ) or horizontal (, with ), and no two edges intersect.
The next line contains (), the number of starting points. Each of the next lines contains the coordinates and of a starting point, where and . No starting point lies inside or on the border of the pond. The starting points are independent, so at any moment exactly one ball is on the course.
Output
For each starting point, in input order, print -1 if the ball never sinks. Otherwise print three integers , , and : the time in seconds before the ball sinks (), and the point where it sinks (, ). The point must lie on the border of the pond.
Hints
In the first example, the ball trajectories look as follows.

In the second example, starting positions 1 and 3 coincide, and the ball from position 2 never stops.
