思路:
首先对所有区间进行排序,遍历区间时当出现新区间时,把上一个合并成功的区间的长度加到$ans$中,最后总的长度减去区间总的长度。
l, m = map(int, input().split())
interval = []
for i in range(m):
interval.append(list(map(int, input().split())))
interval.sort()
ans = 0
st, ed = interval[0]
for item in interval:
a, b = item
if st <= a <= ed and st <= b <= ed:
continue
elif st <= a <= ed and b > ed:
ed = b
else:
ans += ed - st + 1
st, ed = item
ans += ed - st + 1
print(l - ans + 1)