l, n = map(int, input().split())
tree = 0
district = []
for i in range(n):
start, end = map(int, input().strip().split())
district.append((start, end))
district.sort(key = lambda n: n[0])
def isconverge(dis):# 判断最末两区间是否交叉
t1 = dis[-1]
t2 = dis[-2]
if t1[0] > t2[1]:
return False
elif t1[0] <= t2[1]:
return True
while len(district) > 1:
if not isconverge(district): # 未交叉,弹出最后一个区间
temp = district.pop()
tree += temp[1] - temp[0] + 1
elif isconverge(district): # 交叉则将两区间合并,再加入列表
temp1 = district.pop()
temp2 = district.pop()
district.append((min(temp1[0], temp2[0]), max(temp1[1], temp2[1])))
tree += (district[0][1] - district[0][0]) + 1 # 最后一个区间
print(l + 1 - tree)
只有前两个过了,根据题干,应该是只有未交叉的情况过了,但是我测试了题目样例(存在交叉),自己又试了一些其他的有交叉数据,都是能过的,且输出数字正确,请问我的代码存在什么问题。
(所有未通过测试点报错都为Wrong Answer.wrong answer On line 1 column 1, read -, expected 4)