~/algor $ cat problems/saralash-kesmalar-birlashmasi
Length of a union of segments
Medium
1000 ms
128 MB
64%
$n$ segments are given on the number line: the $i$-th segment is $[l_{i}; r_{i}]$. Segments may overlap. Find the total length of the part covered by at least one segment.
Input
The first line contains a positive integer $n$. Each of the next $n$ lines contains two integers: $l_{i}$ and $r_{i}$.
Output
One integer — the total length of the covered part.
Note
$1 \le n \le 10^{5}$
$0 \le l_{i} \lt r_{i} \le 10^{9}$
Examples
Input #1
3 1 5 3 8 10 12
Output #1
9
Input #2
2 0 4 4 6
Output #2
6