#P2082. Total Covered Length of Intervals

    ID: 15364 Type: Default 1000ms 256MiB

Total Covered Length of Intervals

Total Covered Length of Intervals

Given N intervals, each represented as \( [s_i, t_i] \), compute the total length covered by the union of these intervals. In other words, find the measure of the set \( \bigcup_{i=1}^{N} [s_i, t_i] \) where the intervals may overlap.

Note: It is guaranteed that the input intervals are provided in arbitrary order. Use appropriate methods to merge overlapping intervals before calculating the total covered length.

inputFormat

The input consists of multiple lines. The first line contains a single integer \( N \), representing the number of intervals. Each of the following \( N \) lines contains two integers \( s_i \) and \( t_i \) which denote the endpoints of the \( i^{th} \) interval.

Constraints:

  • \( 1 \leq N \leq 10^5 \)
  • \( -10^9 \leq s_i < t_i \leq 10^9 \)

outputFormat

Output a single integer, the total length covered by all the intervals after merging the overlapping ones.

sample

1
1 5
4