Energy Collection
Time limit1sMemory limit128 MB
Given non-overlapping axis-aligned squares, find an axis-aligned collector square that overlaps strictly and is no larger than the cells it collects, maximizing the count.
- Level
Medium7 of 10
- Topics
- Geometry, Binary search, Sorting, Prefix sum
- Solved
- No attempts yet
Problem
You are given a square grid that contains several non-overlapping energy cells. Every energy cell is itself a square. Cells may have different sizes, but each one produces the same amount of energy.
You want to collect as much energy as possible using a single square collector. The collector is placed on the grid, stays grid-aligned, and you may set its side length to any positive integer.
The collector gathers all of the energy of an energy cell exactly when both of these hold:
- the collector and the cell overlap with positive area (merely touching along an edge or at a single corner does not count), and
- the energy cell is at least as large as the collector; a cell smaller than the collector can never be collected, no matter where it lies.
Choose the collector's position and side length to maximize the total energy collected.

Input
The input contains several test cases.
Each test case begins with a line holding a single integer (), the number of energy cells. Each of the next lines contains four space-separated integers , , , describing one cell, where is its lower-left corner and is its upper-right corner. The coordinates satisfy , , and (each cell is a square). The cells within one test case do not overlap.
The input ends with a line containing , which must not be processed.
Output
Because every energy cell yields the same amount of energy, measure energy in units of one cell.
For each test case, print a single line with one integer: the maximum number of energy cells that a single collector can collect. This maximum energy value is unique.