using
System;
using
System.Collections.Generic;
class
Program {
static
int
[, ] dp =
new
int
[1005, 1005];
static
int
[] ans =
new
int
[1005];
static
void
precompute(
int
[, ] a,
int
N,
int
M)
{
for
(
int
j = 0; j < M; j++)
dp[j, 0] = 1;
for
(
int
i = 0; i < M; i++) {
for
(
int
j = 1; j < N; j++) {
if
(a[j, i] >= a[j - 1, i])
dp[i, j] = dp[i, j - 1] + 1;
else
dp[i, j] = 1;
}
}
for
(
int
i = 0; i < N; i++) {
for
(
int
j = 0; j < M; j++) {
ans[i] = Math.Max(ans[i], dp[j, i]);
}
}
}
static
List<
string
> query(
int
[, ] a,
int
[, ] arr,
int
Q,
int
N,
int
M)
{
precompute(a, N, M);
List<
string
> res =
new
List<
string
>();
for
(
int
i = 0; i < Q; i++) {
int
l = arr[i, 0];
int
r = arr[i, 1];
r--;
l--;
if
(ans[r] >= r - l + 1)
res.Add(
"YES"
);
else
res.Add(
"NO"
);
}
return
res;
}
public
static
void
Main()
{
int
N = 5, M = 4;
int
[, ] mat = { { 1, 2, 3, 5 },
{ 3, 1, 3, 2 },
{ 4, 5, 2, 3 },
{ 5, 5, 3, 2 },
{ 4, 4, 3, 4 } };
int
Q = 6;
int
[, ] queries = { { 1, 1 }, { 2, 5 }, { 3, 5 },
{ 4, 5 }, { 1, 3 }, { 1, 5 } };
List<
string
> sol = query(mat, queries, Q, N, M);
foreach
(
string
s
in
sol) Console.WriteLine(s);
}
}