from more_itertools import pairwise
from typing import List
MOD = int(1e9 + 7)
def rectangleArea(rectangles: List[List[int]]) -> int:
xs, ans = set(), 0
for x0, _, x1, _ in rectangles:
xs.add(x0)
xs.add(x1)
# 纵向x轴扫描线
for a, b in pairwise(sorted(xs)): # xs为所有的x轴扫描线
ys = [(y0, y1) for x0, y0, x1, y1 in rectangles if x0 <= a and b <= x1] # 获得a,b扫描线之间的纵向坐标
s = cur = 0
# 横向y轴扫描线
for c, d in sorted(ys, key=lambda x: (x[0], -x[1])): # 按y0升序,按y1降序
if c > cur:
s += d - c # 总高度
elif d > cur:
s += d - cur
cur = max(cur, d) # 记录当前已计算高度
ans = (ans + s * (b - a)) % MOD
return ans
if __name__ == '__main__':
rectangles = [[0, 0, 2, 2], [1, 0, 2, 3], [1, 0, 3, 1]]
rectangles2 = [[0, 0, 1000000000, 1000000000]]
print(cal_measure(rectangles))
print(cal_measure(rectangles2))
print(rectangleArea(rectangles))
文章目录DiffusionInst: Diffusion Model for Instance Segmentation摘要介绍任务介绍实例分割的几种方法想法来源贡献方法整体结构Mask RepresentationDiffusionInst组成TrainingInference不足之处感悟DiffusionInst: Diffusion Model for Instance Segmentation
代码&#x…