Distance to Polygon#2023

Distance to Polygon

8 seconds
256 megabytes
Medium

On the plane, a convex polygon is given. You are required to process a specified number of queries.

Each query consists of the coordinates of a point on the plane. The result of the query is the distance from that point to the polygon (the distance from the given point to the polygon is the minimum distance from the point to any point inside or on the boundary of the polygon).

Input

The first line contains the integer (3 \leq n \leq 4000) — the number of vertices of the polygon.

The following lines list the coordinates of the vertices in counterclockwise order. No three vertices are collinear. Each vertex is given by two integers , (-15000 \leq x, y \leq 15000).

The next line contains five integers: , , , , (1 \leq q \leq 10^6, 0 \leq a, b, c, d \leq 15000) — the number of queries and the coefficients for calculating the coordinates of subsequent queries using the coordinates and result of the previous query.

The last line contains two integers x_1, y_1 (-15000 \leq x_1, y_1 \leq 15000) — the coordinates of the first query point.

The coordinates of each subsequent query are computed using the coordinates and the result of the previous query as follows. If for the -th query the coordinates are x_i, y_i, and the answer is d_i, then the coordinates for the (i+1)-th query are:

x_{i+1} = ((x_i + 15000) \cdot (\operatorname{round}(d_i) + a) + b) \bmod 30001 - 15000; y_{i+1} = ((y_i + 15000) \cdot (\operatorname{round}(d_i) + c) + d) \bmod 30001 - 15000;

Here \operatorname{round}(d_i) means rounding d_i to the nearest integer.

Output

Print a single number — the answer to the last query, with an absolute or relative error not exceeding 10^{-8}.

Examples

Example 1

Loading
Loading