Separate Squares II Problem
Description
LeetCode Problem 3454.
You are given a 2D integer array squares. Each squares[i] = [x_i, y_i, l_i] represents the coordinates of the bottom-left point and the side length of a square parallel to the x-axis.
Find the minimum y-coordinate value of a horizontal line such that the total area covered by squares above the line equals the total area covered by squares below the line.
Answers within 10^(-5) of the actual answer will be accepted.
Note: Squares may overlap. Overlapping areas should be counted only once in this version.
Example 1:
Input: squares = [[0,0,1],[2,2,1]]
Output: 1.00000
Explanation:

Any horizontal line between y = 1 and y = 2 results in an equal split, with 1 square unit above and 1 square unit below. The minimum y-value is 1.
Example 2:
Input: squares = [[0,0,2],[1,1,1]]
Output: 1.00000
Explanation:

Since the blue square overlaps with the red square, it will not be counted again. Thus, the line y = 1 splits the squares into two equal parts.
Constraints:
1 <= squares.length <= 5 * 10^4squares[i] = [x_i, y_i, l_i]squares[i].length == 30 <= x_i, y_i <= 10^91 <= l_i <= 10^9- The total area of all the squares will not exceed
10^15.
Solution
Approach: Sweep Line with a Segment Tree
Squares may overlap, and overlapping area counts once, so we need the area of a union of rectangles. The classic tool is a sweep line moving upward.
Each square gives two events: at its bottom edge y, add the x-interval [x, x + l), and at its top edge, remove it. Sort the events by y. Compress the x coordinates and keep a segment tree over the elementary x-intervals; each node stores how many intervals fully cover it and the total covered length below it.
First sweep: between two consecutive event heights, the union has width covered (the root's covered length), so the strip adds (y - prev_y) * covered to the total area. After that, the target is half of the total.
Second sweep: repeat the same strips while accumulating area. In the first strip where the accumulated area plus the strip's area reaches the target, the line lies inside that strip, at prev_y + (target - accumulated) / covered.
Complexity: O(n log n) time and O(n) space.
class Node:
__slots__ = ("l", "r", "cnt", "length")
def __init__(self):
self.l = self.r = 0
self.cnt = self.length = 0
class SegmentTree:
def __init__(self, nums):
n = len(nums) - 1
self.nums = nums
self.tr = [Node() for _ in range(n << 2)]
self.build(1, 0, n - 1)
def build(self, u, l, r):
self.tr[u].l, self.tr[u].r = l, r
if l != r:
mid = (l + r) >> 1
self.build(u << 1, l, mid)
self.build(u << 1 | 1, mid + 1, r)
def modify(self, u, l, r, k):
if self.tr[u].l >= l and self.tr[u].r <= r:
self.tr[u].cnt += k
else:
mid = (self.tr[u].l + self.tr[u].r) >> 1
if l <= mid:
self.modify(u << 1, l, r, k)
if r > mid:
self.modify(u << 1 | 1, l, r, k)
self.pushup(u)
def pushup(self, u):
if self.tr[u].cnt:
self.tr[u].length = self.nums[self.tr[u].r + 1] - self.nums[self.tr[u].l]
elif self.tr[u].l == self.tr[u].r:
self.tr[u].length = 0
else:
self.tr[u].length = self.tr[u << 1].length + self.tr[u << 1 | 1].length
@property
def length(self):
return self.tr[1].length
class Solution:
def separateSquares(self, squares: List[List[int]]) -> float:
xs = set()
segs = []
for x1, y1, l in squares:
x2, y2 = x1 + l, y1 + l
xs.update([x1, x2])
segs.append((y1, x1, x2, 1))
segs.append((y2, x1, x2, -1))
segs.sort()
st = sorted(xs)
tree = SegmentTree(st)
d = {x: i for i, x in enumerate(st)}
area = 0
y0 = 0
for y, x1, x2, k in segs:
area += (y - y0) * tree.length
tree.modify(1, d[x1], d[x2] - 1, k)
y0 = y
target = area / 2
area = 0
y0 = 0
for y, x1, x2, k in segs:
t = (y - y0) * tree.length
if area + t >= target:
return y0 + (target - area) / tree.length
area += t
tree.modify(1, d[x1], d[x2] - 1, k)
y0 = y
return 0