Not in Range

3.9

58 votes
Arrays, Data Structures, Easy, One-dimensional, Partial Sum, Prefix sum, Segment Trees
Problem

You are given 106 boxes that are numbered from 1 to 106. The value of each box is equal to its number. 
There are N ranges and every range consists of two integers L  and R  denoting that the value of box in the range [L,R] will turn out to be zero. 
Find the sum of values of all boxes from 1 to 106 .

Note: The ranges may overlap.

Input format

  • First line: Integer N
  • Next N lines: Two space-separated integers L and R  

Output format
Print a single integer denoting the answer.

Constraints
0N105
1LR106

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Boxes having number 1, 22, 2001, 999999,1000000 does not belong to any range.
So the sum of these numbers is 2002023 which is our answer.

Editor Image

?