101775: [AtCoder]ABC177 F - I hate Shortest Path Problem

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $600$ points

Problem Statement

There is a grid of squares with $H+1$ horizontal rows and $W$ vertical columns.

You will start at one of the squares in the top row and repeat moving one square right or down. However, for each integer $i$ from $1$ through $H$, you cannot move down from the $A_i$-th, $(A_i + 1)$-th, $\ldots$, $B_i$-th squares from the left in the $i$-th row from the top.

For each integer $k$ from $1$ through $H$, find the minimum number of moves needed to reach one of the squares in the $(k+1)$-th row from the top. (The starting square can be chosen individually for each case.) If, starting from any square in the top row, none of the squares in the $(k+1)$-th row can be reached, print -1 instead.

Constraints

  • $1 \leq H,W \leq 2\times 10^5$
  • $1 \leq A_i \leq B_i \leq W$

Input

Input is given from Standard Input in the following format:

$H$ $W$
$A_1$ $B_1$
$A_2$ $B_2$
$:$
$A_H$ $B_H$

Output

Print $H$ lines. The $i$-th line should contain the answer for the case $k=i$.


Sample Input 1

4 4
2 4
1 1
2 3
2 4

Sample Output 1

1
3
6
-1

Let $(i,j)$ denote the square at the $i$-th row from the top and $j$-th column from the left.

For $k=1$, we need one move such as $(1,1)$ → $(2,1)$.

For $k=2$, we need three moves such as $(1,1)$ → $(2,1)$ → $(2,2)$ → $(3,2)$.

For $k=3$, we need six moves such as $(1,1)$ → $(2,1)$ → $(2,2)$ → $(3,2)$ → $(3,3)$ → $(3,4)$ → $(4,4)$.

For $k=4$, it is impossible to reach any square in the fifth row from the top.

Input

题意翻译

### 题目大意 有一个 $(H+1)$ 行 $W$ 列的矩阵,你每步可以在矩阵中向右或向下移动一个格子。其中,在第 $i\,(1 \le i \le H)$ 行中,你无法从左至右第 $A_i$ 至 $B_i$ 个格子向下走。对于每一个 $k\,(1 \le k \le H)$,求出你从第 $1$ 行的任意一个格子出发移动到第 $(k+1)$ 行的最少步数,若无法移动到则输出 `-1`。 数据范围:$1 \le H,W \le 2\times 10^5$,$1 \le A_i \le B_i \le W$。 ### 输入格式 第一行两个整数 $H,W$,接下来 $H$ 行每行两个整数 $A_i,B_i$。 ### 输出格式 共 $H$ 行,每行一个整数,第 $i$ 行的数字表示从第 $1$ 行移动到第 $(i+1)$ 行需要的最少步数,若无法移动到则为 `-1`。 ### 样例解释 $k=1$ 时,其中一种答案最小的移动顺序为 $(1,1)\rightarrow (2,1)$; $k=2$ 时,一种移动顺序为 $(1,1)\rightarrow (2,1)\rightarrow (2,2)\rightarrow (3,2)$; $k=3$ 时,一种移动顺序为 $(1,1)\rightarrow (2,1)\rightarrow (2,2)\rightarrow (3,2)\rightarrow (3,3)\rightarrow (3,4)\rightarrow (4,4)$; $k=4$ 时,无法从第 $1$ 行移动到第 $5$ 行。 (翻译 by @CarroT1212)

加入题单

算法标签: