#143Medium
Painting a line
Time: 1 s · Memory: 256 MB
- Sorting
Statement
segments of a number line were painted; segment is . Segments may overlap or lie inside each other. Find the total length of the painted part of the line.
Input format
The first line contains (). The next lines contain and ().
Output format
A single number — the length of the painted part.
Examples
Input
2 1 3 2 4
Output
3
Input
1 10 10
Output
0
Note
Sort the segments by their left end and walk through them, keeping the current union . If the next segment has , it joins the union: . Otherwise the length is added to the answer and a new union starts.
The point segment has length . The answer reaches , which does not fit into a 32-bit int.
Source: acmp.ru, problem 377.
