This page is still under construction.

Parts of this page are still being built. What you see may change.

Energy Collection

Time limit1sMemory limit128 MB

Summary
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.

Energy cells and a collector

Input

The input contains several test cases.

Each test case begins with a line holding a single integer NN (1≤N≤10001 \le N \le 1000), the number of energy cells. Each of the next NN lines contains four space-separated integers x1x_1, y1y_1, x2x_2, y2y_2 describing one cell, where (x1,y1)(x_1, y_1) is its lower-left corner and (x2,y2)(x_2, y_2) is its upper-right corner. The coordinates satisfy −106≤x1<x2≤106-10^6 \le x_1 < x_2 \le 10^6, −106≤y1<y2≤106-10^6 \le y_1 < y_2 \le 10^6, and x2−x1=y2−y1x_2 - x_1 = y_2 - y_1 (each cell is a square). The cells within one test case do not overlap.

The input ends with a line containing N=0N = 0, 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.

Examples3

  1. Example 1

    Input
    5
    3 3 4 4
    0 0 3 3
    0 4 3 7
    4 0 7 3
    4 4 7 7
    0
    
    Expected output
    4
    
  2. Example 2

    Input
    1
    0 0 1 1
    0
    
    Expected output
    1
    
  3. Example 3

    Input
    4
    0 0 2 2
    2 0 4 2
    0 2 2 4
    2 2 4 4
    0
    
    Expected output
    4