Problems > Merged Interval Count

Merged Interval Count

Difficulty: 1400 3000 ms 128 MB

intervals sorting

Merge all overlapping or touching intervals [a, b] and print how many disjoint blocks remain.

Input: the first line has N; the next N lines each have two integers a b (a ≤ b).

Output: the number of merged blocks.

Constraints:

  • 1 ≤ N ≤ 105
  • 0 ≤ a ≤ b ≤ 106

Sample Tests

Input 1
3
1 3
2 5
7 8
Output 1
2
Input 2
2
1 2
2 3
Output 2
1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.