PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000274. Độ dài hợp các nửa khoảng (Union Length on a Signed Axis)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Độ dài hợp các nửa khoảng (Union Length on a Signed Axis)
Union Length on a Signed Axis
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Given half-open intervals on the real number line with possibly negative integer endpoints, compute the length of their union. Touching intervals leave no gap; the length of is .
Input
First line: . Next lines: .
Output
Print the union length.
Subtasks
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; ; intervals are pairwise disjoint (touching allowed).
- Subtask 3 (50 points): ; .
Example 1
Input
3
-3 1
0 4
6 8
Output
9
Explanation
The union is and , length .
Example 2
Input
3
-5 -2
-2 0
-4 -3
Output
5
Explanation
The first two touch and form ; the third is contained in their union.