Ensure that you are logged in and have the required permissions to access the test.

Grid Walk

4.3

3 votes
Medium, Algorithms, String, DP
Problem

In a N×M grid, each grid has a value ai,j. Grid (i,j) (i<N) has two extra values li,j,ri,j. The values of each line is not decreasing, that is, for any i,j,k(1iN,1jkM), ai,jai,k, and li,jli,k,ri,jri,k if i<N.

If you are at grid (i,j) (i<N), you can go to (i+1,x), where xli,j and xri,j. It's guaranteed li,jri,j and li,j+1ri,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 ai,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(1N,M1000).

The next N lines represent a matrix, the j th integer on the i th line is ai,j.

Similarily, the next N1 lines represent l, and the last N1 lines represent r.

It's guaranteed 1ai,j,li,j,ri,jM.

Output format

One integer represents the answer, modulo 109+7.

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

There are 6 different paths, but two of them have the same result array, so the answer is 5.

(1,1)(2,1)(3,1):[1,1,1]

(1,1)(2,1)(3,2):[1,1,3]

(1,2)(2,2)(3,1):[2,1,1]

(1,2)(2,2)(3,2):[2,1,3]

(1,3)(2,3)(3,2):[2,2,3]

(1,3)(2,3)(3,3):[2,2,3]

Editor Image

?