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

Bài tập thuộc lộ trình

1 lộ trình

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 NN half-open intervals [li,ri)[l_i,r_i) on the number line, find the total length covered by at least one interval. Count overlap once.

Input

First line: NN. Each of the next NN lines contains li,ril_i,r_i.

Output

Print one integer: the union length.

Subtasks

  • Subtask 1 (20 points): 1≤N≤301\le N\le 30; 0≤li<ri≤300\le l_i<r_i\le 30.
  • Subtask 2 (30 points): 1≤N≤2000001\le N\le 200000; 0≤li<ri≤10000000000\le l_i<r_i\le 1000000000; li=0l_i=0.
  • Subtask 3 (50 points): 1≤N≤2000001\le N\le 200000; 0≤li<ri≤10000000000\le l_i<r_i\le 1000000000.

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).