<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:
        def query(qlow,qhigh,low,high,index,segTree):
            if low <= qlow and qhigh <= high:
                return segTree[index]
            if qhigh < low or high < qlow:
                return float('inf')
            mid = qlow+(qhigh-qlow)//2
            a = query(qlow,mid,low,high,2*index+1, segTree)
            b = query(mid+1,qhigh,low,high,2*index+2, segTree)
            return min(a,b)
        def update(qlow, qhigh, low, high, index,val,segTree):
            # print (qlow,qhigh,low,high,index)
            if low <= qlow and qhigh <= high:
                segTree[index] = min(segTree[index], val)
                return
            if qhigh < low or high < qlow:
                return 
            # print (qlow,qhigh,low,high,index,val)
            segTree[index] = min(segTree[index], val)
            mid = qlow+(qhigh-qlow)//2
            a = update(qlow,mid,low,high,2*index+1,val, segTree)
            b = update(mid+1,qhigh,low,high,2*index+2,val, segTree)
            return 
        rdict = {}
        cdict = {}
        rs = 1
        rsize = 1
        while rs < len(grid):
            rs*=2
            rsize+=1
        rrs = 2**rsize-1
        csize = 1
        cs = 1
        while cs < len(grid[0]):
            cs*=2
            csize+=1
        ccs = 2**csize - 1
        for i in range (len(grid)):
            rdict[i] = [float('inf')]*ccs
        for j in range (len(grid[0])):
            cdict[j] = [float('inf')]*rrs
        dp = [[float('inf')]*len(grid[0]) for _ in range (len(grid))]
        dp[-1][-1] = 1
        n = len(grid)
        m = len(grid[0])
        update(0,m-1,m-1,m-1,0,1,rdict[n-1])
        update(0,n-1,n-1,n-1,0,1,cdict[m-1])
        for i in range (len(grid)-2,-1,-1):
            cur = query(0,n-1,i,min(n-1,grid[i][-1]+i),0,cdict[m-1])
            dp[i][-1] = 1+cur
            update(0,n-1,i,i,0,dp[i][-1],cdict[m-1])
        for j in range (len(grid[0])-2,-1,-1):
            cur = query(0,m-1,j,min(m-1,grid[-1][j]+j),0,rdict[n-1])
            dp[-1][j] = 1+cur
            update(0,m-1,j,j,0,dp[-1][j],rdict[n-1])
        for i in range (len(grid)-2,-1,-1):
            update(0,m-1,m-1,m-1,0,dp[i][-1],rdict[i])
        for j in range (len(grid[0])-2,-1,-1):
            update(0,n-1,n-1,n-1,0,dp[-1][j], cdict[j])
        for j in range (len(grid[0])-2,-1,-1):
            for i in range (len(grid)-2,-1,-1):
                op1 = query(0,n-1,i,min(n-1,grid[i][j]+i),0,cdict[j])
                op2 = query(0,m-1,j,min(m-1,grid[i][j]+j),0,rdict[i])
                dp[i][j] = 1+min(op1,op2)
                update(0,n-1,i,i,0,dp[i][j],cdict[j])
                update(0,m-1,j,j,0,dp[i][j],rdict[i])
        return dp[0][0] if dp[0][0] != float('inf') else -1
                
                
68600