Skip to content

Instantly share code, notes, and snippets.

@racterub
Created February 16, 2019 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save racterub/a9c5526f7993ceaf2caa89df5e529e96 to your computer and use it in GitHub Desktop.
Save racterub/a9c5526f7993ceaf2caa89df5e529e96 to your computer and use it in GitHub Desktop.
#!/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