Orchard Division
Time limit2sMemory limit512 MB
Find the smallest axis-aligned rectangle anchored at one orchard corner that contains exactly half of N given tree coordinates.
- Level
Medium7 of 10
- Topics
- Geometry, Prefix sum, Binary search
- Solved
- No attempts yet
Problem
Uncle Oliver is going to sell a large part of his well known dwarf plum tree orchard. He will divide the orchard into two parts, sell one of them and keep the other.
The trees were originally planted in regular rows and columns, forming a square grid with the same number of rows and columns. Over the years Oliver removed many trees that were weak or eaten by bugs, so today there are also many free squares with no tree on them.
Oliver has decided to keep exactly half of all the trees in the orchard. He also set a few more demands that, in his opinion, will make his part easy to maintain later.
- The part Oliver keeps must be a rectangle.
- At least one corner of the rectangle must coincide with a corner of the orchard.
- The area of the rectangle must be as small as possible.
Each tree stands in the center of a square whose area is exactly one square meter, so the position of a tree is given by the coordinates of the square it stands on. The fence between the two parts runs along the borders of the squares.

Input
The input holds several test cases. Each test case starts with a line containing two integers () and () separated by a space. is the side length of the orchard in meters and is the number of trees in the orchard. The next lines each contain the and coordinates of one tree, separated by a space. Coordinates are zero based, so the corner squares of the orchard have coordinates , , , . Within one test case all coordinate pairs are distinct. The input continues until the end of the file.
Output
For each test case, print one line with the whole number , the smallest possible area in square meters of uncle Oliver's part of the orchard. If the orchard cannot be divided so that every demand holds, print -1 instead. The printed value might not fit into a 32-bit integer type.