PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000253. Tổng chiều dài hợp các khoảng (Union Length of Intervals)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Tổng chiều dài hợp các khoảng (Union Length of Intervals)
Union Length of Intervals
Source: Phước Hưng OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Given half-open intervals on the number line, find the total length covered by at least one interval. Count overlap once.
Input
First line: . Each of the next lines contains .
Output
Print one integer: the union length.
Subtasks
- Subtask 1 (20 points): ; .
- Subtask 2 (30 points): ; ; .
- Subtask 3 (50 points): ; .
Example 1
Input
4
1 4
3 7
7 9
12 14
Output
10
Explanation
First three intervals form [1,9), length 8; last has length 2.
Example 2
Input
3
0 5
1 3
0 5
Output
5
Explanation
All intervals lie within [0,5).