SimJin @ 2024-03-07 15:47:23
l, m = map(int, input().split())
areas = []
for i in range(m):
u, v = map(int, input().split())
j = 0
while j < len(areas):
# [150, 300] [100 ,200] 左交集
if areas[j][0] >= u and areas[j][0] <= v and areas[j][1] >= v:
areas[j][0] = u
break
# [100, 200] [150, 300] 右交集
elif areas[j][0] <= u and areas[j][1] >= u and areas[j][1] <= v:
areas[j][1] = v
break
# [100, 300] [150, 160] 包含
elif areas[j][0] <= u and areas[j][1] >= v:
break
# [150, 160] [100, 300] 被包含
elif areas[j][0] >= u and areas[j][1] <= v :
del areas[j]
j -= 1
j += 1
if(len(areas) == 0): # 首次加入
areas.append([u, v])
elif j >= len(areas): # 非首次, 遍历完后无交集, 加入
areas.append([u, v])
remove_area = 0
for area in areas:
remove_area += (area[1] - area[0] + 1)
print(l - remove_area + 1)
by 12gas890 @ 2024-03-07 17:43:45
你知道了吗哥们
by SimJin @ 2024-03-12 14:37:49
@12gas890 当输入[474, 8369]时,需要合并[98, 981],[1577,9342]=>[98,9342] 但是这上面没有考虑这种情况