#P9514. JOI Kingdom's Garden
JOI Kingdom's Garden
JOI Kingdom's Garden
The JOI Kingdom is vast and mysterious. King JOI plans to partition a part of his territory to build a beautiful garden. The kingdom’s territory is modeled as an infinite grid of square cells. One cell is designated as the origin. A cell with coordinates \((x,y)\) is reached by moving \(x\) cells to the right and \(y\) cells upward from the origin (with left and downward moves interpreted as negative moves).
There are two classes of art pieces placed on the grid:
- A-type art pieces: There are \(N\) types. For the \(i\)th type \((1 \le i \le N)\), an art piece is present in every cell of the form \[ (P_i+kD,\; Q_i+lD),\quad \text{for all integers } k,l. \]
- B-type art pieces: There are \(M\) types. For the \(j\)th type \((1 \le j \le M)\), an art piece is present in every cell of either of the forms \[ (R_j+kD,\; y) \quad \text{or} \quad (x,\; S_j+lD), \quad \text{for all integers } k,l \text{ and for all integers } x,y. \]
King JOI wants to select a rectangular region from the grid to build his garden. He will choose four integers \(a,b,c,d\) and the garden will consist of all cells \((x,y)\) satisfying \(a \le x \le b\) and \(c \le y \le d\). In order for the king to enjoy a diversity of art pieces, his garden must contain at least one art piece from each of the total \(N+M\) types. However, if the garden is too large the residents become angry. Hence, King JOI wishes to minimize the number of cells in the garden.
Given the information about the art pieces, compute the minimum number of cells that the garden must contain.
inputFormat
The input is given from standard input. The first line contains three integers: \(D, N, M\).
The next \(N\) lines each contain two integers \(P_i\) and \(Q_i\) for \(i=1,\ldots,N\).
The following \(M\) lines each contain two integers \(R_j\) and \(S_j\) for \(j=1,\ldots,M\).
outputFormat
Output a single integer: the minimum number of cells in a rectangle that contains at least one art piece of each of the \(N+M\) types.
sample
5 1 1
1 2
3 4
3