<p>You are given a <strong>0-indexed</strong> <code>m x n</code> integer matrix <code>grid</code>. Your initial position is at the <strong>top-left</strong> cell <code>(0, 0)</code>.</p>

<p>Starting from the cell <code>(i, j)</code>, you can move to one of the following cells:</p>

<ul>
	<li>Cells <code>(i, k)</code> with <code>j &lt; k &lt;= grid[i][j] + j</code> (rightward movement), or</li>
	<li>Cells <code>(k, j)</code> with <code>i &lt; k &lt;= grid[i][j] + i</code> (downward movement).</li>
</ul>

<p>Return <em>the minimum number of cells you need to visit to reach the <strong>bottom-right</strong> cell</em> <code>(m - 1, n - 1)</code>. If there is no valid path, return <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex1.png" style="width: 271px; height: 171px;" />
<pre>
<strong>Input:</strong> grid = [[3,4,2,1],[4,2,3,1],[2,1,0,0],[2,4,0,0]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The image above shows one of the paths that visits exactly 4 cells.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/25/ex2.png" style="width: 271px; height: 171px;" />
<pre>
<strong>Input:</strong> grid = [[3,4,2,1],[4,2,1,1],[2,1,1,0],[3,4,1,0]]
<strong>Output:</strong> 3
<strong>Explanation: </strong>The image above shows one of the paths that visits exactly 3 cells.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/26/ex3.png" style="width: 181px; height: 81px;" />
<pre>
<strong>Input:</strong> grid = [[2,1,0],[1,0,0]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> It can be proven that no path exists.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= grid[i][j] &lt; m * n</code></li>
	<li><code>grid[m - 1][n - 1] == 0</code></li>
</ul>

class SegmentTree:
    def __init__(self, n, k_inf):
        self.n = n
        self.k_inf = k_inf
        self.tree = [k_inf] * (4 * n)

    def update(self, i, val):
        self._update(0, 0, self.n - 1, i, val)

    def query(self, i, j):
        return self._query(0, 0, self.n - 1, i, j)

    def _update(self, tree_index, lo, hi, i, val):
        if lo == i and hi == i:
            self.tree[tree_index] = val
            return
        mid = (lo + hi) // 2
        left_tree_index = self._left(tree_index)
        right_tree_index = self._right(tree_index)
        if i <= mid:
            self._update(left_tree_index, lo, mid, i, val)
        else:
            self._update(right_tree_index, mid + 1, hi, i, val)
        self.tree[tree_index] = self._merge(self.tree[left_tree_index], self.tree[right_tree_index])

    def _query(self, tree_index, lo, hi, i, j):
        if i <= lo and hi <= j:
            return self.tree[tree_index]
        if j < lo or hi < i:
            return self.k_inf
        mid = (lo + hi) // 2
        return self._merge(self._query(self._left(tree_index), lo, mid, i, j),
                           self._query(self._right(tree_index), mid + 1, hi, i, j))

    def _merge(self, a, b):
        return min(a, b)

    def _left(self, tree_index):
        return 2 * tree_index + 1

    def _right(self, tree_index):
        return 2 * tree_index + 2


class Solution:
    def minimumVisitedCells(self, grid):
        m = len(grid)
        n = len(grid[0])
        k_inf = (m + n) * 2 - 1
        rows = [SegmentTree(n, k_inf) for _ in range(m)]
        cols = [SegmentTree(m, k_inf) for _ in range(n)]

        # The min # cells to visit (m - 1, n - 1) from (m - 1, n - 1) is 1.
        rows[m - 1].update(n - 1, 1)
        cols[n - 1].update(m - 1, 1)

        for i in range(m - 1, -1, -1):
            for j in range(n - 1, -1, -1):
                if grid[i][j] == 0:
                    continue
                move_right = rows[i].query(j + 1, min(n - 1, j + grid[i][j]))
                move_down = cols[j].query(i + 1, min(m - 1, i + grid[i][j]))
                min_move = min(k_inf, min(move_right, move_down) + 1)
                rows[i].update(j, min_move)
                cols[j].update(i, min_move)

        res = rows[0].query(0, 0)
        return -1 if res == k_inf else res

59800