Artifact
Time limit1sMemory limit128 MB
Given n row intervals, choose k consecutive columns and pay the cost to extend each row's interval to cover them, minimizing total added tiles.
- Level
Medium7 of 10
- Topics
- Prefix sum, Sliding window, Math
- Solved
- No attempts yet
Problem
While tidying up an old cellar, I found an object that looks like an ancient artifact: a huge chessboard made of thousands of equal, tiny cells.
The board has columns, numbered from to from left to right. The vertical lines are numbered from to , and vertical line is the boundary between column and column . The board has rows.
In each row there is exactly one contiguous strip of gold tiles. This strip lies between vertical line and vertical line ; that is, the cells in columns are all covered with gold tiles, and every other cell of the row is empty.
You may add gold tiles to widen each row's gold strip (after widening, each row's gold tiles must still form a single contiguous strip). Your goal is to choose consecutive columns and add tiles so that, in every row, all of those columns are gold — forming a vertical band of gold, columns wide, that is full from the top row to the bottom row.
Write a program that prints the minimum number of extra gold tiles you must buy.
Input
The first line contains an integer (), the number of tests, followed by test descriptions.
The first line of each test contains two integers () and (). The second line contains pairs of integers and (), the numbers of the two vertical lines between which the gold tiles of row are laid.
Output
For each test, print on its own line the minimum number of gold tiles you must buy.
Hint
The picture below illustrates an example. Black cells are the cells that held gold tiles from the start. Gray cells show the minimal set of cells on which new tiles must be placed so that, after widening the gold strips, consecutive columns become full of gold tiles from top to bottom.
