<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 Solution:
    def minimumVisitedCells(self, grid: List[List[int]]) -> int:
        # approach 2: DP & Diff Array
        from sortedcontainers import SortedList
        MAX = 10**6
        R, C = len(grid), len(grid[0])
        dp = [[MAX]*C for _ in range(R)]
        row_diff = [[] for _ in range(R)]
        col_diff = [[] for _ in range(C)]
        row_set = [SortedList() for _ in range(R)]
        col_set = [SortedList() for _ in range(C)]

        for i in range(R):
            for j in range(C):
                while (row_diff[i] and row_diff[i][0][0] == j):
                    cost_mark = row_diff[i][0][1]
                    heapq.heappop(row_diff[i])
                    if cost_mark > 0:
                        row_set[i].add(cost_mark)
                    if cost_mark < 0:
                        row_set[i].pop(row_set[i].index(-cost_mark))
                while (col_diff[j] and col_diff[j][0][0] == i):
                    cost_mark = col_diff[j][0][1]
                    heapq.heappop(col_diff[j])
                    if cost_mark > 0:
                        col_set[j].add(cost_mark)
                    if cost_mark < 0:
                        col_set[j].pop(col_set[j].index(-cost_mark))
                
                min_cost = MAX
                if row_set[i]: min_cost = min(min_cost, row_set[i][0])
                if col_set[j]: min_cost = min(min_cost, col_set[j][0])
                dp[i][j] = 1 if (i == 0 and j == 0) else min_cost

                step = grid[i][j]
                if step == 0: continue
                cost = dp[i][j]
                heapq.heappush(row_diff[i], (j+1, cost+1))
                heapq.heappush(row_diff[i], (j+1+step, -(cost+1)))
                heapq.heappush(col_diff[j], (i+1, cost+1))
                heapq.heappush(col_diff[j], (i+1+step, -(cost+1)))
        
        if dp[-1][-1] == MAX: return -1
        return dp[-1][-1]

        # # approach 1: BFS with SortedList - O(RC * log(max(R, C))), O(RC)
        # from sortedcontainers import SortedList

        # R, C = len(grid), len(grid[0])
        # visit0 = [SortedList(range(C)) for _ in range(R)] # unvisited cols in rows
        # visit1 = [SortedList(range(R)) for _ in range(C)] # unvisited rows in cols
        # q = deque([(0, 0, 1)]) # i, j, number of cells

        # while q:
        #     i, j, d = q.popleft()
        #     if (i, j) == (R-1, C-1):
        #         return d
        #     for k in list(visit0[i].irange(j+1, min(j+1+grid[i][j], C) - 1)): # irange is inclusive
        #         q.append((i, k, d+1))
        #         visit0[i].remove(k) # more and more space available thanks to remove
        #         visit1[k].remove(i)
        #     for k in list(visit1[j].irange(i+1, min(i+1+grid[i][j], R) - 1)):
        #         q.append((k, j, d+1))
        #         visit1[j].remove(k)
        #         visit0[k].remove(j)
        # return -1
5670