Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- n, s = gets.strip.split(' ')
- n = n.to_i
- s = s.to_i
- used_start_poins = Hash.new
- used_end_points = Hash.new
- for a0 in (0..s-1)
- l, r = gets.strip.split(' ')
- l = l.to_i
- r = r.to_i
- sum = 0
- if l > 0
- sum += r*(r+1)/2 - (l*(l-1)/2)
- else
- sum += r*(r+1)/2
- end
- used_start_poins[l]=r
- used_end_points[r]=l
- left = l-1
- while left > 0
- i = used_end_points[left]
- if i
- left = used_start_poins[i]-1
- else
- sum += left
- used_start_poins[l]=l
- used_end_points[l]=l
- break
- end
- end
- right = r+1
- while right < n
- i = used_start_poins[right]
- if i
- right = used_end_points[i]+1
- else
- sum += right
- used_start_poins[r]=r
- used_end_poins[r]=r
Add Comment
Please, Sign In to add comment