# CFR 777 C. Alyona and Spreadsheet ( Python Output Optimization )

Problem - C - Codeforces

The first line of the input contains two positive integers n and m (1 ≤ n·m ≤ 100 000) — the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.
Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for ai, j (1 ≤ ai, j ≤ 1e9).
The next line of the input contains an integer k (1 ≤ k ≤ 100 000) — the number of task that teacher gave to Alyona.
The i-th of the next k lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n).

O( N * M )

```import sys

N, M = map( int, input().split() )
G = [ list( map( int, input().split() ) ) for i in range( N ) ]

rmaxv = [ 0 for i in range( N ) ]
for i in range( M ):
reach = [ 0 for j in range( N ) ]
for j in range( N - 1, -1, -1 ):
if j + 1 == N or not ( G[ j ][ i ] <= G[ j + 1 ][ i ] ):
reach[ j ] = 1
else:
reach[ j ] = reach[ j + 1 ] + 1
rmaxv[ j ] = max( rmaxv[ j ], reach[ j ] )

Q = int( input() )
ans = []
for qi in range( Q ):
L, R = map( int, input().split() )
if rmaxv[ L - 1 ] >= R - L + 1:
ans.append( "Yes\n" )
else:
ans.append( "No\n" )

sys.stdout.write( ''.join( ans ) )
```