{"trustable":false,"prependHtml":"\u003cstyle type\u003d\"text/css\"\u003e\n section pre {\n display: block;\n padding: 9.5px;\n margin: 0 0 10px;\n font-size: 13px;\n line-height: 1.42857143;\n word-break: break-all;\n word-wrap: break-word;\n color: #333;\n background: rgba(255, 255, 255, 0.5);\n border: 1px solid #ccc;\n border-radius: 6px;\n }\n\u003c/style\u003e\n\u003cscript\u003e\n window.katexOptions \u003d {\n delimiters: [\n {left: \u0027\\\\(\u0027, right: \u0027\\\\)\u0027, display: false},\n {left: \u0027\\\\[\u0027, right: \u0027\\\\]\u0027, display: true}\n ]\n };\n\u003c/script\u003e\n","sections":[{"title":"Problem Statement","value":{"format":"MD","content":"## 题目描述\n\n有一个城市用一个 $N$ 行 $10^9$ 列的网格图表示,并且城市里有 $N$ 堵墙,编号为 $1 \\dots N$。第 $i$ 堵墙位于第 $i$ 行的第 $L_i$ 到 $R_i$ 列(参见样例 1 的示意图)。\n\nTakahashi 打算拆掉所有的墙。他的一次攻击可以同时拆掉连续 $D$ 列的墙。\n\n* 形式化来说,他可以选择一个 $[1, 10^9 - D + 1]$ 的**整数** $x$,然后拆掉所有在(或者墙的一部分在)第 $x$ 到 $x + D - 1$ 列的墙。\n\n当墙的一部分被拆除时,整个墙都会被波及而被拆除(参见样例 1 的示意图)。\n\nTakahashi 至少需要多少次攻击才能把所有墙全部拆除?\n\n## 数据范围\n\n* $1 \\le N \\le 2 \\times 10^5$\n* $1 \\le D \\le 10^9$\n* $1 \\le L_i \\le R_i \\le 10^9$\n* 所有输入均为整数。\n\n## 输入格式\n\n输入按照以下格式给出:\n\n$N \\ D \\\\\\\\ L_1 \\ R_1 \\\\\\\\ L_2 \\ R_2 \\\\\\\\ \\vdots \\\\\\\\ L_N \\ R_N$\n\n## 输出格式\n\n输出拆除所有墙所需的最少攻击次数。\n\n## 样例解释 1\n\n最少攻击次数为两次,他可以这样攻击:\n\n* 第一次攻击范围为 $[2, 4]$,把第 $1, 2$ 堵墙拆除。\n* 第二次攻击范围为 $[5, 7]$,把第 $3$ 堵墙拆除。\n\n还有另一种攻击方式:\n\n* 第一次攻击范围为 $[7, 9]$,把第 $2, 3$ 堵墙拆除。\n* 第二次攻击范围为 $[1, 3]$,把第 $1$ 堵墙拆除。\n\n\u003csection\u003e\n \u003cp\u003eIn a town divided into a grid with \u003cvar\u003e\\(N\\)\u003c/var\u003e rows and \u003cvar\u003e\\(10^9\\)\u003c/var\u003e columns, there are \u003cvar\u003e\\(N\\)\u003c/var\u003e walls, numbered \u003cvar\u003e\\(1\\)\u003c/var\u003e to \u003cvar\u003e\\(N\\)\u003c/var\u003e.\u003cbr\u003e\n Wall \u003cvar\u003e\\(i\\)\u003c/var\u003e ranges from the \u003cvar\u003e\\(L_i\\)\u003c/var\u003e-th column to the \u003cvar\u003e\\(R_i\\)\u003c/var\u003e-th column from the left in the \u003cvar\u003e\\(i\\)\u003c/var\u003e-th row from the top. (See also the figure at Sample Input and Output \u003cvar\u003e\\(1\\)\u003c/var\u003e.)\u003c/p\u003e\n \u003cp\u003eTakahashi has decided to destroy all \u003cvar\u003e\\(N\\)\u003c/var\u003e walls.\u003cbr\u003e\n With his superhuman strength, his one punch can damage consecutive \u003cvar\u003e\\(D\\)\u003c/var\u003e columns at once.\u003c/p\u003e\n \u003cul\u003e\n \u003cli\u003eMore formally, he can choose an \u003cstrong\u003einteger\u003c/strong\u003e \u003cvar\u003e\\(x\\)\u003c/var\u003e between \u003cvar\u003e\\(1\\)\u003c/var\u003e and \u003cvar\u003e\\(10^9 - D + 1\\)\u003c/var\u003e (inclusive) to damage all walls that exist (or partly exist) in the \u003cvar\u003e\\(x\\)\u003c/var\u003e-th through \u003cvar\u003e\\((x + D - 1)\\)\u003c/var\u003e-th columns and are not yet destroyed.\u003c/li\u003e\n \u003c/ul\u003e\n \u003cp\u003eWhen a part of a wall is damaged, that whole wall will be destroyed by the impact of the punch.\u003cbr\u003e\n (See also the figure at Sample Input and Output \u003cvar\u003e\\(1\\)\u003c/var\u003e.)\u003c/p\u003e\n \u003cp\u003eAt least how many times does Takahashi need to punch to destroy all walls?\u003c/p\u003e\n\u003c/section\u003e"}},{"title":"Constraints","value":{"format":"MD","content":"\u003csection\u003e\n \u003cul\u003e\n \u003cli\u003e\u003cvar\u003e\\(1 \\leq N \\leq 2 \\times 10^5\\)\u003c/var\u003e\u003c/li\u003e\n \u003cli\u003e\u003cvar\u003e\\(1 \\leq D \\leq 10^9\\)\u003c/var\u003e\u003c/li\u003e\n \u003cli\u003e\u003cvar\u003e\\(1 \\leq L_i \\leq R_i \\leq 10^9\\)\u003c/var\u003e\u003c/li\u003e\n \u003cli\u003eAll values in input are integers.\u003c/li\u003e\n \u003c/ul\u003e\n\u003c/section\u003e"}},{"title":"Input","value":{"format":"MD","content":"\u003csection\u003e\n \u003cp\u003eInput is given from Standard Input in the following format:\u003c/p\u003e\n \u003cpre\u003e\u003cvar\u003e\\(N\\)\u003c/var\u003e \u003cvar\u003e\\(D\\)\u003c/var\u003e\n\u003cvar\u003e\\(L_1\\)\u003c/var\u003e \u003cvar\u003e\\(R_1\\)\u003c/var\u003e\n\u003cvar\u003e\\(L_2\\)\u003c/var\u003e \u003cvar\u003e\\(R_2\\)\u003c/var\u003e\n\u003cvar\u003e\\(\\vdots\\)\u003c/var\u003e\n\u003cvar\u003e\\(L_N\\)\u003c/var\u003e \u003cvar\u003e\\(R_N\\)\u003c/var\u003e\n\u003c/pre\u003e\n\u003c/section\u003e"}},{"title":"Output","value":{"format":"MD","content":"\u003csection\u003e\n \u003cp\u003ePrint the minimum number of punches needed to destroy all walls.\u003c/p\u003e\n\u003c/section\u003e"}},{"title":"Sample 1","value":{"format":"MD","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e3 3\n1 2\n4 7\n5 9\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e2\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003csection\u003e\n\u003c/section\u003e\u003csection\u003e\n \u003cp\u003eThe figure below describes the arrangements of walls in Sample Input \u003cvar\u003e\\(1\\)\u003c/var\u003e.\u003c/p\u003e\n \u003cp\u003e\u003cimg alt\u003d\"image\" src\u003d\"CDN_BASE_URL/eca3af62ca98e5f656a8790f8c5d3820?v\u003d1678198516\"\u003e\u003c/p\u003e\n \u003cp\u003eHe can destroy all walls with two punches, such as the following. (Below, \u003cvar\u003e\\(\\lbrack a, b \\rbrack\\)\u003c/var\u003e denotes the range from the \u003cvar\u003e\\(a\\)\u003c/var\u003e-th through \u003cvar\u003e\\(b\\)\u003c/var\u003e-th columns.)\u003c/p\u003e\n \u003cul\u003e\n \u003cli\u003eFirst, punch \u003cvar\u003e\\(\\lbrack 2, 4 \\rbrack\\)\u003c/var\u003e. The walls existing in \u003cvar\u003e\\(\\lbrack 2, 4 \\rbrack\\)\u003c/var\u003e ― Walls \u003cvar\u003e\\(1\\)\u003c/var\u003e and \u003cvar\u003e\\(2\\)\u003c/var\u003e ― are damaged and destroyed.\u003c/li\u003e\n \u003cli\u003eSecond, punch \u003cvar\u003e\\(\\lbrack 5, 7 \\rbrack\\)\u003c/var\u003e. The wall existing in \u003cvar\u003e\\(\\lbrack 5, 7 \\rbrack\\)\u003c/var\u003e ― Wall \u003cvar\u003e\\(3\\)\u003c/var\u003e ― is damaged and destroyed.\u003c/li\u003e\n \u003c/ul\u003e\n \u003cp\u003eIt is also possible to destroy all walls with two punches in this way:\u003c/p\u003e\n \u003cul\u003e\n \u003cli\u003eFirst, punch \u003cvar\u003e\\(\\lbrack 7, 9 \\rbrack\\)\u003c/var\u003e to destroy Walls \u003cvar\u003e\\(2\\)\u003c/var\u003e and \u003cvar\u003e\\(3\\)\u003c/var\u003e.\u003c/li\u003e\n \u003cli\u003eSecond, punch \u003cvar\u003e\\(\\lbrack 1, 3 \\rbrack\\)\u003c/var\u003e to destroy Wall \u003cvar\u003e\\(1\\)\u003c/var\u003e.\u003c/li\u003e\n \u003c/ul\u003e\n\u003c/section\u003e"}},{"title":"Sample 2","value":{"format":"MD","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e3 3\n1 2\n4 7\n4 9\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e1\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003csection\u003e\n\u003c/section\u003e\u003csection\u003e\n \u003cp\u003eThe difference from Sample Input/Output \u003cvar\u003e\\(1\\)\u003c/var\u003e is that Wall \u003cvar\u003e\\(3\\)\u003c/var\u003e now covers \u003cvar\u003e\\(\\lbrack 4, 9 \\rbrack\\)\u003c/var\u003e, not \u003cvar\u003e\\(\\lbrack 5, 9 \\rbrack\\)\u003c/var\u003e.\u003cbr\u003e\n In this case, he can punch \u003cvar\u003e\\(\\lbrack 2, 4 \\rbrack\\)\u003c/var\u003e to destroy all walls with one punch.\u003c/p\u003e\n\u003c/section\u003e"}},{"title":"Sample 3","value":{"format":"MD","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e5 2\n1 100\n1 1000000000\n101 1000\n9982 44353\n1000000000 1000000000\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e3\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n\u003csection\u003e\n\u003c/section\u003e\u003csection\u003e\n\u003c/section\u003e"}}]}