Difficulty: Hard Tags: Sweep Line, Segment Tree, Geometry, Binary Search Link: https://leetcode.com/problems/separate-squares-ii/
You are given multiple axis-aligned squares on a 2D plane.
Each square is described as:
[xi, yi, li]
(xi, yi)→ bottom-left cornerli→ side length
Your task is to find the minimum y-coordinate of a horizontal line such that:
The total covered area above the line equals the total covered area below the line
| Version | Overlapping Area |
|---|---|
| 3453 – Separate Squares I | counted multiple times |
| 3454 – Separate Squares II | counted only once |
This single change completely breaks the simple binary-search-with-sum approach.
In 3453, area below y = h was:
sum of each square's contribution
In 3454, area below y = h is:
area of UNION of rectangles clipped by y ≤ h
That means:
- Overlaps must be merged
- You must compute union area, not sum
- Area function is piecewise linear, not directly additive
Each square defines:
x ∈ [xi, xi + li]
y ∈ [yi, yi + li]
We care about area distribution along y.
Classic computational geometry trick:
- Treat square bottoms and tops as events
- Between consecutive y-events, the set of active x-intervals is constant
- For each horizontal slice:
area = (covered x-length) × (height)
Find y where accumulated union-area reaches total_area / 2
For each square:
(y_start = yi, + interval [xi, xi+li])
(y_end = yi+li, - interval [xi, xi+li])
Sort all events by y.
Maintain a data structure that supports:
- Add interval
[l, r) - Remove interval
[l, r) - Query total union length on x-axis
This requires a Segment Tree + coordinate compression.
While sweeping:
area += (current_x_coverage) × (delta_y)
Store prefix area for each y-segment.
Once total area is known:
target = total_area / 2
Scan segments again (or binary search) to find the lowest y where:
area_below(y) ≥ target
Interpolate linearly within the segment.
- Sweep line handles overlaps correctly
- Segment tree guarantees exact union length
- Area accumulation is deterministic and precise
- Linear interpolation ensures floating-point accuracy
y ↑
│ ┌─────┐
│ │ │ overlapping squares
│ └──┐ │
│ └──┘
│────────── y = h ← find this line
│██████████ area below
Only the outer boundary counts.
Let n = number of squares.
| Aspect | Complexity |
|---|---|
| Events | 2n |
| Coordinate compression | O(n log n) |
| Sweep + segment tree | O(n log n) |
| Space | O(n) |
Efficient enough for n = 5 × 10⁴
- Requires recognizing union-area geometry
- Needs sweep line + segment tree
- Binary search alone is insufficient
- Floating-point + geometry + data structures combined
This is contest-level Hard, not “tricky Medium”.
| Aspect | 3453 | 3454 |
|---|---|---|
| Overlap handling | counted repeatedly | merged |
| Area function | simple sum | union |
| Technique | binary search | sweep line |
| Data structure | none | segment tree |
| Difficulty | Medium | Hard |
“Use a sweep line on the y-axis with a segment tree to track x-interval union length, accumulate union area, and find the lowest y where cumulative area reaches half.”
- If overlaps count once → think union area
- Union area in 2D → sweep line
- Balance problems → prefix area + interpolation
- This is a template problem for advanced geometry