D - Destroyer Takahashi Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400400

問題文

NN10910^9 列の格子状の区画に区切られた街に NN 枚の壁があり、11 から NN までの番号が割り振られています。
ii は上から ii 行目、左から LiL_i 列目から RiR_i 列目の範囲にあります。(入出力例 11 の図も参考にしてください。)

高橋君は NN 枚の壁をすべて破壊することにしました。
超人的な腕力を持つ高橋君は、11 回のパンチで連続する DD 列にまとめてダメージを与えることができます。

  • より厳密に言い換えると、11 以上 109D+110^9 - D + 1 以下の 整数 xx を選んで、xx 列目から x+D1x + D - 1 列目に (一部でも) 存在するすべての破壊されていない壁にパンチによるダメージを与えることができます。

壁は一部分でもダメージを受けると、パンチの衝撃波により全体が木っ端みじんに破壊されてしまいます。
(入出力例 11 の説明も参考にしてください。)

高橋君がすべての壁を破壊するには、少なくとも何回のパンチを放つ必要がありますか?

制約

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1D1091 \leq D \leq 10^9
  • 1LiRi1091 \leq L_i \leq R_i \leq 10^9
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

NN DD
L1L_1 R1R_1
L2L_2 R2R_2
\vdots
LNL_N RNR_N

出力

すべての壁を破壊するのに必要なパンチの最少回数を出力せよ。


入力例 1Copy

Copy
3 3
1 2
4 7
5 9

出力例 1Copy

Copy
2

入力例 11 に対応する壁の配置を図示したものが下図です。

image

たとえば次のようにパンチを放つことで、 22 回のパンチですべての壁を破壊することができます。(以下の説明では、aa 列目から bb 列目までの範囲を [a,b]\lbrack a, b \rbrack と表記します。)

  • まず、 [2,4]\lbrack 2, 4 \rbrack にパンチを放つ。 [2,4]\lbrack 2, 4 \rbrack に存在する壁である壁 11 と壁 22 はダメージを受け、破壊される。
  • 次に [5,7]\lbrack 5, 7\rbrack にパンチを放つ。[5,7]\lbrack 5, 7\rbrack に存在する壁 33 はダメージを受け、破壊される。

また、次の方法でもすべての壁を 22 回のパンチで破壊することができます。

  • まず、[7,9]\lbrack 7, 9 \rbrack にパンチを放ち、壁 22 と壁 33 を破壊する。
  • 次に、[1,3]\lbrack 1, 3 \rbrack にパンチを放ち、壁 11 を破壊する。

入力例 2Copy

Copy
3 3
1 2
4 7
4 9

出力例 2Copy

Copy
1

入出力例 11 と比べると、壁 33 の範囲が [5,9]\lbrack 5, 9 \rbrack から [4,9]\lbrack 4, 9 \rbrack に変わりました。
この場合は [2,4]\lbrack 2, 4 \rbrack にパンチを放つことで 11 回ですべての壁を破壊できます。


入力例 3Copy

Copy
5 2
1 100
1 1000000000
101 1000
9982 44353
1000000000 1000000000

出力例 3Copy

Copy
3

Score : 400400 points

Problem Statement

In a town divided into a grid with NN rows and 10910^9 columns, there are NN walls, numbered 11 to NN.
Wall ii ranges from the LiL_i-th column to the RiR_i-th column from the left in the ii-th row from the top. (See also the figure at Sample Input and Output 11.)

Takahashi has decided to destroy all NN walls.
With his superhuman strength, his one punch can damage consecutive DD columns at once.

  • More formally, he can choose an integer xx between 11 and 109D+110^9 - D + 1 (inclusive) to damage all walls that exist (or partly exist) in the xx-th through (x+D1)(x + D - 1)-th columns and are not yet destroyed.

When a part of a wall is damaged, that whole wall will be destroyed by the impact of the punch.
(See also the figure at Sample Input and Output 11.)

At least how many times does Takahashi need to punch to destroy all walls?

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1D1091 \leq D \leq 10^9
  • 1LiRi1091 \leq L_i \leq R_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN DD
L1L_1 R1R_1
L2L_2 R2R_2
\vdots
LNL_N RNR_N

Output

Print the minimum number of punches needed to destroy all walls.


Sample Input 1Copy

Copy
3 3
1 2
4 7
5 9

Sample Output 1Copy

Copy
2

The figure below describes the arrangements of walls in Sample Input 11.

image

He can destroy all walls with two punches, such as the following. (Below, [a,b]\lbrack a, b \rbrack denotes the range from the aa-th through bb-th columns.)

  • First, punch [2,4]\lbrack 2, 4 \rbrack. The walls existing in [2,4]\lbrack 2, 4 \rbrack ― Walls 11 and 22 ― are damaged and destroyed.
  • Second, punch [5,7]\lbrack 5, 7 \rbrack. The wall existing in [5,7]\lbrack 5, 7 \rbrack ― Wall 33 ― is damaged and destroyed.

It is also possible to destroy all walls with two punches in this way:

  • First, punch [7,9]\lbrack 7, 9 \rbrack to destroy Walls 22 and 33.
  • Second, punch [1,3]\lbrack 1, 3 \rbrack to destroy Wall 11.

Sample Input 2Copy

Copy
3 3
1 2
4 7
4 9

Sample Output 2Copy

Copy
1

The difference from Sample Input/Output 11 is that Wall 33 now covers [4,9]\lbrack 4, 9 \rbrack, not [5,9]\lbrack 5, 9 \rbrack.
In this case, he can punch [2,4]\lbrack 2, 4 \rbrack to destroy all walls with one punch.


Sample Input 3Copy

Copy
5 2
1 100
1 1000000000
101 1000
9982 44353
1000000000 1000000000

Sample Output 3Copy

Copy
3


2025-04-11 (Fri)
02:18:17 +00:00