Pyramid Base
Time limit5sMemory limit128 MB
Given up to 1000 weighted rectangles on a grid up to 10^6 by 10^6, find the largest axis-aligned square whose total cost of intersected rectangles is at most B.
- Level
Hard8 of 10
- Topics
- Binary search, Geometry, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
You want to find the largest affordable site for building a new pyramid. To help you decide, you are given a survey of the available land, conveniently divided into an grid of square cells. The base of the pyramid must be a square whose sides are parallel to those of the grid.
The survey identified a set of possibly overlapping obstacles, each described as a rectangle in the grid with sides parallel to those of the grid. To build the pyramid, every cell covered by its base must be cleared of obstacles. Removing the -th obstacle costs . An obstacle must always be removed completely; you cannot remove only part of one. Also, removing an obstacle does not affect any other obstacle that overlaps it.
Given the survey dimensions and , the descriptions of the obstacles, the removal cost of each obstacle, and your budget , write a program that finds the maximum possible side length of the pyramid's base such that the total cost of removing obstacles does not exceed .
Input
Your program must read the following data from standard input.
- Line 1: two integers and , separated by a single space. ()
- Line 2: the integer , the maximum cost you can afford (your budget). ()
- Line 3: the integer , the number of obstacles found in the survey. ()
- Each of the next lines describes one obstacle. The -th such line describes the -th obstacle and consists of five integers , , , , and , separated by single spaces. They give, respectively, the coordinates of the bottommost-leftmost cell of the obstacle, the coordinates of the topmost-rightmost cell of the obstacle, and the removal cost. The bottommost-leftmost cell of the grid has coordinates and the topmost-rightmost cell has coordinates . (, , )
Output
Your program must write a single line to standard output containing one integer: the maximum possible side length of the pyramid's base that can be prepared. If no pyramid can be built, output .
Hint

The figure shows the only possible location for a pyramid base with a side of length .