Skip to content

Instantly share code, notes, and snippets.

@racterub
Created Feb 16, 2019
Embed
What would you like to do?
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Date : 2019-02-16 08:59:40
# @Author : Racter (vivi.450@hotmail.com)
# @Profile : https://racterub.me
# OJ: https://zerojudge.tw/ShowProblem?problemid=b966
while 1:
try:
count = int(input().strip())
data = [list(map(int, input().strip().split(' '))) for i in range(count)]
data = sorted(data, key=lambda v:v[1]) # sorted with start value to prevent data needs to jump to different block back and forth
s = data[0][0]
e = data[0][1]
length = e-s
for i in data[1:]:
if i[0] < e and i[1] > e:
e = i[1]
elif i[0] > e:
length += (e-s)
s = i[0]
e = i[1]
print(length)
except EOFError:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment