QOJ.ac

QOJ

时间限制: 5 s 内存限制: 512 MB 总分: 100

#2604. 螺旋矩阵

统计

定义矩阵中的一对相邻单元格为一对单元格 $(r_a, c_a)$ 和 $(r_b, c_b)$,满足: $r_a = r_b$ 且 $|c_a - c_b| = 1$, 或者 $c_a = c_b$ 且 $|r_a - r_b| = 1$。

定义螺旋矩阵为满足以下条件的矩阵: 矩阵仅包含互不相同的正整数。 可以从某个单元格 $(i, j)$ 出发,将所有其他单元格排列成一条路径,使得路径中每两个连续的单元格都是一对相邻单元格,并且沿着从 $(i, j)$ 开始的路径,考虑矩阵中的数值,它们按访问顺序构成一个连续的整数区间 $[l..r]$。

给定一个大小为 $n \times m$ 的矩阵,其中包含互不相同的正整数。同时给定 $q$ 次查询。每次查询定义了一个由角点 $(r_1, c_1)$ 和 $(r_2, c_2)$ 确定的子矩阵。对于每次查询,判断该子矩阵是否为螺旋矩阵。

输入格式

第一行包含三个整数 $n, m$ 和 $q$ ($1 \le n, m \le 2000, 1 \le q \le 10^6$),分别表示矩阵的大小和查询次数。

接下来的 $n$ 行,每行包含 $m$ 个整数。第 $i$ 行的第 $j$ 个整数表示位于矩阵第 $i$ 行第 $j$ 列的元素 $a_{i,j}$ ($1 \le a_{i,j} \le 10^9$)。保证所有元素互不相同。

接下来的 $q$ 行,每行包含四个整数 $r_1, c_1, r_2, c_2$ ($1 \le r_1 \le r_2 \le n, 1 \le c_1 \le c_2 \le m$),表示子矩阵的角点。

输出格式

对于每次查询,在单独的一行中打印答案。如果子矩阵是螺旋矩阵,则打印 “YES”,否则打印 “NO”。

样例

输入 1

5 7 10
10 11 12 13 14 15 16
9 2 3 32 31 30 17
8 1 4 25 26 29 18
7 6 5 24 27 28 19
52 51 50 23 22 21 20
1 1 5 7
1 1 4 1
2 2 5 3
1 4 5 7
1 1 4 3
1 1 5 3
2 2 2 2
2 2 2 3
3 4 5 7
3 3 4 4

输出 1

NO
YES
NO
YES
YES
NO
YES
YES
YES
NO

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.