Territories
Time limit9sMemory limit1024 MB
Each territory can be placed in four ways on the torus. Find the largest area common to all territories after choosing one placement for each.
- Level
Hard9 of 10
- Topics
- Segment tree, Intervals, Sorting
- Solved
- No attempts yet
Statement
Bajtazar is a biologist studying the fauna of a newly discovered planet. He has observed that the planet is home to distinct species of unique animals. Geologists have also found huge mineral deposits there, and plans call for large mines that could threaten the planet's ecological balance.
Every species on the planet is territorial: each one has a fixed rectangle in which it can move. To reassure the biologists, the Interplanetary Parliament issued a decree that any area lying inside the territories of all species will become a nature reserve, so no mines will be built there.
For each species, Bajtazar recorded the coordinates and of two opposite corners of its territory. He has now returned to Earth and is analyzing the data to determine the size of the reserve.
The planet is a torus, and its map is a grid of size with a coordinate system. Points are written as with and . All territories are rectangles with sides parallel to the coordinate axes.
Because the planet is a torus, two opposite corners do not determine a unique rectangle. Each species has four possible territories. Parliament wants to know as soon as possible how many mines can definitely be built, so Bajtazar must find the largest possible area of the reserve from the given data.
Input
The first line contains three integers , , and (, ), the number of species and the map size. Each of the next lines contains four integers (, , , ), the coordinates of two opposite corners of a species' territory.
Output
Print one integer: the maximum possible area of the intersection of all territories.
Hint
The figure shows three of the 16 possible placements of two territories, given corners , and , on a map. The intersections have areas 0, 8, and 15. The last figure shows the largest possible reserve. The reserve does not need to be connected.
