Submission Code

How many moves are needed in the vertical and horizontal directions respectively.

Vertical Direction

Simply moving downward, so it takes $k$ moves.

Horizontal Direction

Let $a_x$ be the information about where you originally came from when you moved to cell $x$. In other words, you start from $a_x$ in the 1st row and move to $x$ in the $k$-th row. For each $k$, think of starting from one of the cells in the forbidden area and moving to $b_i+1$. The answer is $\underset{x}{min} (x-a_x) + k$. The following figure shows the transition of $a_x$ for sample input 1.