0134. Segments

Input file name: segnemts.in
Output file name: segments.out
Time limit: 500 ms
Memory limit: 64 megabytes

You are to find the total length of union of n segments located on a horizontal line.

Input file

The first number in the input file is an integer n (1 ≤ n ≤ 5000) representing the number of segments. The rest of file is filled by 2n integers (x1 y1 x2 y2 …) meaning coordinates of two points confining i segment. All coordinates do not exceed 109 by an absolute value.

Output file

Print only one number – the total length of union of given segments.

Examples:

segnemts.insegments.out
3 0 5 3 6 7 87


Source: Petrozavodsk Summer 2003. Blitz Kontest, Monday, August 25
Author: Andrew Lopatin, Nick Durov

Discuss       Submit a solution