Way Out

All submissions for this problem are available.
Read problems statements in Mandarin Chinese and Russian.
Oh, no! Chef’s in trouble. He’s got himself stuck in a cave (we don’t know how) and is looking for a way out. The bigger problem is that he needs to get his tractor out of the cave (don't ask why Chef owns a tractor!). He currently faces a large block of height N cells and length N cells, and needs to get his tractor across this block. The block is made up of vertical columns of soil, each of which is one cell long. Each column has a continuous vertical gap, with the i^{th} column having its gap from the l_{i}^{th} cell to the h_{i}^{th} cell (starting from the bottom, 0indexing). That is, in the i^{th} column, there is no soil from the l_{i}^{th} cell to the h_{i}^{th} cell (both inclusive). Chef can build additional gaps by clearing some cells of soil. His tractor has height H, and therefore, he needs to build a horizontal corridor of height H passing through all the columns. That is, some consecutive H rows must have no soil. Please see the figures in the example and explanation sections for more details.
Chef is able to clear one cell of soil by spending one unit of energy. Chef is smart, and will figure out a way to build the horizontal corridor while spending the minimum possible amount of energy. To estimate how many of his tasty dishes he will still be able to cook for you tonight, find out what is the minimum possible energy he needs to spend.
Input
First line of input contains one integer T  number of test cases. T test cases follow.
Each test case starts with two integers N and H – size of the cave and height of the tractor, respectively. In each of the next N lines are two integers l_{i} and h_{i}, respectively indicating lowest and highest number of cell for the gap in the i^{th} column.
Output
One integer – minimum energy required.
Constraints
 1 ≤ T ≤ 10^{3}
 1 ≤ N ≤ 10^{6}
 1 ≤ sum of N over all test cases ≤ 10^{6}
 1 ≤ H ≤ N
 0 ≤ l_{i} ≤ h_{i} < N
Subtasks
Subtask 1 (25 points): T ≤ 100, N ≤ 100
Subtask 2 (75 points): No additional constraints
Example
Input: 2 4 3 1 2 1 2 1 2 1 2 5 2 2 3 1 2 2 3 1 2 2 3 Output: 4 2
Explanation
In the second case, the figure describes the initial map, where white cells denote empty cells and brown cells denote soil cells.
When we removed soil in two cells as the following figure, then we can make a corridor of height 2, adn this is the optimal way to make a corridor.
Author:  witalij_hq 
Tester:  laycurse 
Editorial  http://discuss.codechef.com/problems/WOUT 
Tags  aug15, dynamicprogramming, easy, prefixsum, subarray, witalij_hq 
Date Added:  22022015 
Time Limit:  2 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions