Grid Walk
Practice
4.3 (3 votes)
Medium
Algorithms
String
Dp
Problem
30% Success 1023 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

In a \(N\times M\) grid, each grid has a value \(a_{i,j}\). Grid \((i,j)\ (i<N)\) has two extra values \(l_{i,j},r_{i,j}\). The values of each line is not decreasing, that is, for any \(i,j,k(1\le i\le N,1\le j\le k\le M)\), \(a_{i,j}\le a_{i,k}\), and \(l_{i,j}\le l_{i,k},r_{i,j}\le r_{i,k}\) if \(i<N\).

If you are at grid \((i,j)\ (i<N)\), you can go to \((i+1,x)\), where \(x\ge l_{i,j}\) and \(x\le r_{i,j}\). It's guaranteed \(l_{i,j}\le r_{i,j}\) and \(l_{i,j+1}\le r_{i,j}+1\).

Now you walk from some grid \((1,x)\) and end at some grid \((N,y)\), \(x\) and \(y\) are decided by yourself. When you pass a grid \((i,j)\), you add \(a_{i,j}\) to a result array. You need to count hou many different arrays can be obtained. Two arrays are dirrerent if at least one element is different.

Input format

First line contains two integers \(N,M(1\le N,M\le 1000)\).

The next \(N\) lines represent a matrix, the \(j\) th integer on the \(i \) th line is \(a_{i,j}\).

Similarily, the next \(N-1\) lines represent \(l\), and the last \(N-1\) lines represent \(r\).

It's guaranteed \(1\le a_{i,j},l_{i,j},r_{i,j}\le M\).

Output format

One integer represents the answer, modulo \(10^9+7\).

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
290 votes
Tags:
MathematicsOpenApprovedEasyProbability and Statistics
Points:30
36 votes
Tags:
Digit DPDynamic ProgrammingAlgorithmsAdvancedMath
Points:10
330 votes
Tags:
ApprovedEasyImplementationReady