#P5987. Maximum Common Coverage in a Toroidal Grid
Maximum Common Coverage in a Toroidal Grid
Maximum Common Coverage in a Toroidal Grid
On a 2D toroidal grid of dimensions \(X\) \(\times\) \(Y\), the left and right boundaries are connected, as well as the top and bottom boundaries. The grid is divided into \(X \times Y\) cells. You are given \(n\) axis-aligned rectangles. Each rectangle is defined by the coordinates of two opposite vertices. Note that due to the toroidal (wrap-around) nature of the grid, for each rectangle in each dimension you may choose the minimal arc between the two coordinates.
For a given rectangle, let \(d_x = |x_2 - x_1|\) and \(d_y = |y_2 - y_1|\). Then the effective width of the rectangle on the torus is \(L_x = \min(d_x, X-d_x)\) and its effective height is \(L_y = \min(d_y, Y-d_y)\).
You are allowed to select the placement (i.e. the interpretation) for each rectangle arbitrarily (as long as the two given vertices are on the boundary of the rectangle) in order to maximize the number of cells that are covered by all \(n\) rectangles simultaneously. In the best case, the maximum number of cells that can be covered by all rectangles is:
[ \text{Answer} = \left( \min_{1 \le i \le n} L_{x_i} \right) \times \left( \min_{1 \le i \le n} L_{y_i} \right). ]
Your task is to compute this maximum number of cells.
inputFormat
The first line contains three integers \(X\), \(Y\), and \(n\) — representing the dimensions of the grid and the number of rectangles.
Each of the next \(n\) lines contains four integers \(x_1\), \(y_1\), \(x_2\), \(y_2\), describing the coordinates of two opposite vertices of a rectangle.
outputFormat
Output a single integer representing the maximum number of grid cells that can be simultaneously covered by all \(n\) rectangles in the best-case scenario.
sample
5 5 1
1 1 3 3
4
</p>