#P6886. Safe Fly Swatter Placement

    ID: 20093 Type: Default 1000ms 256MiB

Safe Fly Swatter Placement

Safe Fly Swatter Placement

Norman has a fly swatter in the shape of a $K$-gon. The fly swatter has fixed orientation (i.e. it cannot be rotated or reflected) and its vertex coordinates are defined in a template with integer coordinates. Norman wants to count the number of ways to translate the fly swatter so that:

  • All its vertices lie on the integer grid points inside the axis‐aligned rectangle with bottom‐left corner $(0,0)$ and top‐right corner $(X_p,Y_p)$.
  • None of the $N$ flies, each given as a point $(X,Y)$, lies inside or on the boundary of the translated fly swatter. In other words, a fly is harmed if and only if it lies in the interior or on the boundary of the fly swatter.

The input provides:

  1. The first line contains three integers: $K$, $X_p$, and $Y_p$.
  2. The next $K$ lines each contain two integers describing the template coordinates of the fly swatter's vertices.
  3. The following line contains an integer $N$, the number of flies.
  4. Then $N$ lines follow, each with two integers representing the coordinates of a fly.

Let the template polygon's vertices be $(x_i, y_i)$, $1 \le i \le K$. A translation by $(dx,dy)$ moves each vertex to $(x_i+dx, y_i+dy)$. The translation is valid only if:

  • All translated vertices satisfy $0\le x_i+dx\le X_p$ and $0\le y_i+dy\le Y_p$.
  • No fly $(X,Y)$ satisfies that it lies inside or on the boundary of the translated polygon.

Your task is to compute the total number of valid translations.

Note: A point is a grid point if both its coordinates are integers. The formula for determining if a point is inside a polygon can be implemented using the ray casting algorithm. Also, to check if a point lies on a line segment between $(x_1,y_1)$ and $(x_2,y_2)$, you may check if the cross product is zero and the point lies between the endpoints.

inputFormat

The input is given from standard input and consists of the following lines:

  1. The first line contains three space-separated integers: K (the number of polygon vertices), X_p, and Y_p (the rectangle's top‐right corner).
  2. The next K lines each contain two integers, representing the coordinates of the fly swatter's vertices in the template.
  3. The next line contains an integer N representing the number of flies.
  4. Each of the following N lines contains two integers, the coordinates of a fly.

All coordinates are integers.

outputFormat

Output a single integer: the number of valid placements (translations) of the fly swatter such that all its vertices lie inside the rectangle and none of the flies is harmed.

sample

4 4 4
0 0
1 0
1 1
0 1
1
2 2
12