Ctrl K

Halve the Covered Area

Hard
Square tiles with sides parallel to the axes are laid on a plane. Tile tiles[i] = [x, y, l] has its bottom-left corner at (x, y) and side length l. Tiles may overlap; any spot covered by several tiles counts **once**. Find the smallest y coordinate of a horizontal line such that the covered area above the line equals the covered area below it. Answers within 10⁻⁵ of the exact value are accepted.
Example 1
tiles=[[0, 0, 2], [1, 1, 2]] → 1.5

The squares overlap in a 1 × 1 cell, so the covered area is 4 + 4 − 1 = 7 and half of it is 3.5. Below y = 1 a strip of width 2 is covered (area 2); above it the strip is 3 wide. The missing 1.5 takes another 0.5 of height: the answer is 1.5.

Example 2
tiles=[[0, 0, 1], [0, 3, 1]] → 1.0

The area is 2 and 1 must lie below the line. Any line from y = 1 to y = 3 splits it evenly; the lowest is y = 1.

Example 3
tiles=[[4, 4, 2]] → 5.0
Constraints
  • 1 ≤ len(tiles) ≤ 5·10⁴
  • tiles[i] is a triple of integers [x, y, l]
  • 0 ≤ x, y ≤ 10⁹
  • 1 ≤ l ≤ 10⁹
  • The covered area (overlaps counted once) is at most 10¹⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCity Block Outline HardSmaller Values to the Right HardWindow Sums Within a Band HardPairs With a Steep Drop HardArea Covered by Overlapping Rectangles Hard
loading Python…
Input
tiles=[[0, 0, 2], [1, 1, 2]]
Expected
1.5

Run checks the first 3 tests in your browser. Submit checks all 94 tests on the server, hidden ones included.