#!/usr/bin/ruby def checkio(data) result=0 se=[] data.each{|l,r| right_idx=(0...se.size).bsearch{|i|([l,0]<=>se[i])<=0}||se.size #l <= se[right][0] if right_idx!=0 left_idx=right_idx-1 if l<=se[left_idx][1] # overlap with left l=se[left_idx][0] r=[r,se[left_idx][1]].max result-=se[left_idx][1]-se[left_idx][0]+1 se.delete_at(left_idx) right_idx-=1 end end while right_idx