SnowflakeOA #38
Minimum Segments
Estimated MediumJudge-ready
Topics (AI-labelled)Prefix & RangesTwo Pointers
You are given closed intervals and may add exactly one extra closed interval with length b−a≤k. Find the minimum number of connected blocks after merging. Touching at the same endpoint counts as connected; intervals that differ by 1 are not automatically connected.
Input
The first line contains n k, followed by n lines of a b. 1≤n≤200000, 1≤a≤b≤10⁹, 1≤k≤10⁹. The added interval may have length 0.
Output
Output the single integer required by the problem.
Example 1
Input:
4 2
1 2
2 4
5 8
10 11
Output:
2
Example 2
Input:
2 1
1 1
2 2
Output:
1
Example 3
Input:
1 1
1 100
Output:
1
Example notes
The answers to the three public examples are, in order: 2; 1; 1. The examples were rechecked by independent enumeration, state search, or direct simulation.
Editorial
Loading solution…