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

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

1 lộ trình

Độ 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 qq half-open intervals [li,ri)[l_i,r_i) on the real number line with possibly negative integer endpoints, compute the length of their union. Touching intervals leave no gap; the length of [a,b)[a,b) is b−ab-a.

Input

First line: qq. Next qq lines: li,ril_i,r_i.

Output

Print the union length.

Subtasks

  • Subtask 1 (20 points): 1≤q≤1001\le q\le 100; −1012≤li<ri≤1012-10^{12}\le l_i<r_i\le10^{12}.
  • Subtask 2 (30 points): 1≤q≤2000001\le q\le 200000; −1012≤li<ri≤1012-10^{12}\le l_i<r_i\le10^{12}; intervals are pairwise disjoint (touching allowed).
  • Subtask 3 (50 points): 1≤q≤2000001\le q\le 200000; −1012≤li<ri≤1012-10^{12}\le l_i<r_i\le10^{12}.

Example 1

Input

3
-3 1
0 4
6 8

Output

9

Explanation

The union is [−3,4)[-3,4) and [6,8)[6,8), length 7+2=97+2=9.

Example 2

Input

3
-5 -2
-2 0
-4 -3

Output

5

Explanation

The first two touch and form [−5,0)[-5,0); the third is contained in their union.