Not in Range
Practice
3.9 (58 votes)
Arrays
Data structures
Easy
One Dimensional
Partial sum
Prefix sum
Segment trees
Problem
84% Success 11652 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given \(10^6\) boxes that are numbered from \(1\) to \(10^6\). 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 \(10^6\) .

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
\(0 ≤ N ≤ 10^5 \)
\(1 ≤  L ≤ R ≤ 10^6 \)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
44 votes
Tags:
1-D ArrayArraysData StructuresEasyeasy
Points:10
8 votes
Tags:
MathematicsVery-EasyMathematicsMathamatics
Points:50
16 votes
Tags:
HardFlowGreedy algorithm