矩阵谜题AI (CI翻译)
Camila and Daniel are the greatest puzzle rivals in Mexico. For years, they have challenged each other with the hardest number games in the country, and now they have turned their attention to you. You want to prove to them that you can compete at their level, and so they present you with the following problem.
Camila 和 Daniel 是墨西哥最强的谜题对手。多年来,他们一直用这个国家最难的数字游戏互相挑战,而现在他们把目光转向了你。你想向他们证明自己也能达到他们的水平,于是他们给你出了下面这道题。AI (CI翻译)
You are given two square matrices $a$ and $b$ of size $n \times n$. The submatrix $(r_1, c_1, r_2, c_2)$ of a matrix consists of the elements in rows $r_1$ to $r_2$ and columns $c_1$ to $c_2$. The rows are numbered from $1$ to $n$ from top to bottom and the columns are numbered from $1$ to $n$ from left to right.
给定两个大小为 $n \times n$ 的方阵 $a$ 和 $b$。矩阵的子矩阵 $(r_1, c_1, r_2, c_2)$ 由第 $r_1$ 行到第 $r_2$ 行、第 $c_1$ 列到第 $c_2$ 列中的元素组成。行从上到下编号为 $1$ 到 $n$,列从左到右编号为 $1$ 到 $n$。AI (CI翻译)
In one move on a submatrix of $a$, you pick one of its rows or columns and add any constant (possibly negative) to every element of that row or column inside the submatrix. The submatrix $(r_1, c_1, r_2, c_2)$ of $a$ is matchable if some sequence of moves makes it equal to the same submatrix of $b$. In other words, it is matchable if and only if you can choose a value $h_i$ for each row $i$ and a value $w_j$ for each column $j$ such that $a_{i,j} + h_i + w_j = b_{i,j}$ for all $r_1 \le i \le r_2$ and $c_1 \le j \le c_2$.
对 $a$ 的一个子矩阵进行一次操作时,你可以选择其中一行或一列,并给该行或该列在子矩阵内的每个元素加上任意常数(可以为负数)。如果经过若干次操作后,$a$ 的子矩阵 $(r_1, c_1, r_2, c_2)$ 能变成 $b$ 中对应的子矩阵,则称它是可匹配的。换句话说,当且仅当可以为每一行 $i$ 选择一个值 $h_i$,并为每一列 $j$ 选择一个值 $w_j$,使得对所有满足 $r_1 \le i \le r_2$ 且 $c_1 \le j \le c_2$ 的位置都有 $a_{i,j} + h_i + w_j = b_{i,j}$ 时,该子矩阵可匹配。AI (CI翻译)
You will be asked to perform $q$ operations.
你需要执行 $q$ 次操作。AI (CI翻译)
The operations are of two types:
操作分为两种:AI (CI翻译)
U$\;r_1\;c_1\;r_2\;c_2\;x$: Add a constant$x$to every$a_{i,j}$in the submatrix$(r_1, c_1, r_2, c_2)$.Q$\;r_1\;c_1\;r_2\;c_2$: Determine whether the submatrix$(r_1, c_1, r_2, c_2)$of$a$is matchable.
U$\;r_1\;c_1\;r_2\;c_2\;x$:给子矩阵$(r_1, c_1, r_2, c_2)$中的每个$a_{i,j}$加上常数$x$。Q$\;r_1\;c_1\;r_2\;c_2$:判断$a$的子矩阵$(r_1, c_1, r_2, c_2)$是否可匹配。
Queries do not modify either matrix: the moves are hypothetical and are chosen independently for each query. Only update operations change $a$, and $b$ never changes.
查询不会修改任何一个矩阵:每次查询中的操作都是假设进行的,并且各次查询相互独立。只有更新操作会改变 $a$,而 $b$ 始终不变。AI (CI翻译)
Input
输入格式
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
每个输入包含多组测试用例。第一行包含测试用例数 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)
The first line of each test case contains two integers $n$ and $q$ ($2 \le n \le 1000$, $1 \le q \le 2 \cdot 10^4$) — the dimension of the matrices and the number of operations.
每个测试用例的第一行包含两个整数 $n$ 和 $q$($2 \le n \le 1000$,$1 \le q \le 2 \cdot 10^4$)——矩阵的维度和操作次数。AI (CI翻译)
The next $n$ lines of each test case contain $n$ integers each ($-10^4 \le a_{i,j} \le 10^4$) — the initial state of matrix $a$.
接下来的 $n$ 行每行包含 $n$ 个整数($-10^4 \le a_{i,j} \le 10^4$)——矩阵 $a$ 的初始状态。AI (CI翻译)
The next $n$ lines of each test case contain $n$ integers each ($-10^4 \le b_{i,j} \le 10^4$) — the matrix $b$.
再接下来的 $n$ 行每行包含 $n$ 个整数($-10^4 \le b_{i,j} \le 10^4$)——矩阵 $b$。AI (CI翻译)
Then $q$ lines follow for that test case. Each line starts with a character U or Q:
然后是该测试用例的 $q$ 行操作。每行以字符 U 或 Q 开头:AI (CI翻译)
U: then five integers$r_1$,$c_1$,$r_2$,$c_2$,$x$($1 \le r_1 \le r_2 \le n$,$1 \le c_1 \le c_2 \le n$,$-10^4 \le x \le 10^4$) — update the submatrix$(r_1, c_1, r_2, c_2)$by adding$x$to each of its elements.Q: then four integers$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 n$) — query the submatrix$(r_1, c_1, r_2, c_2)$.
U:接下来是五个整数$r_1$、$c_1$、$r_2$、$c_2$、$x$($1 \le r_1 \le r_2 \le n$,$1 \le c_1 \le c_2 \le n$,$-10^4 \le x \le 10^4$)——将$x$加到子矩阵$(r_1, c_1, r_2, c_2)$中的每个元素上。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 n$)——查询子矩阵$(r_1, c_1, r_2, c_2)$。
It is guaranteed that the sum of $n^2$ over all test cases does not exceed $10^6$.
保证所有测试用例的 $n^2$ 之和不超过 $10^6$。AI (CI翻译)
It is guaranteed that the sum of $q$ over all test cases does not exceed $2 \cdot 10^4$.
保证所有测试用例的 $q$ 之和不超过 $2 \cdot 10^4$。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each query, output "YES" (without quotes) if the submatrix is matchable, and "NO" (without quotes) otherwise.
对于每个查询,如果该子矩阵可匹配,输出 "YES"(不含引号);否则输出 "NO"(不含引号)。AI (CI翻译)
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
答案可以使用任意大小写。例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 都会被识别为肯定回答。AI (CI翻译)
Note
说明
AI (CI翻译)In the first test case, after the update, $a$ becomes $$\begin{bmatrix}1 & 0 \\ 0 & -1\end{bmatrix}.$$ For the query, choosing $h_1=0$, $h_2=3$, $w_1=0$, and $w_2=2$ makes the submatrices match, so the answer is YES.
在第一个测试用例中,更新后,$a$ 变为 $$\begin{bmatrix}1 & 0 \\ 0 & -1\end{bmatrix}.$$ 对于该查询,取 $h_1=0$、$h_2=3$、$w_1=0$ 和 $w_2=2$,即可使两个子矩阵匹配,因此答案为 YES。AI (CI翻译)
In the second test case, the first query has answer YES: choose $h_1=1$, $h_2=4$, $w_1=0$, $w_2=1$, and $w_3=2$. After the first update, the submatrices of $a$ and $b$ in the second query are $$\begin{bmatrix}6 & 1 & 1 \\ 2 & 2 & 2\end{bmatrix}, \quad \begin{bmatrix}2 & 3 & 4 \\ 6 & 7 & 8\end{bmatrix},$$ and it can be proved that the answer is NO. After the second update, in the third query they are $$\begin{bmatrix}6 & 6 & 6 \\ 2 & 2 & 2\end{bmatrix}, \quad \begin{bmatrix}2 & 3 & 4 \\ 6 & 7 & 8\end{bmatrix},$$ so choosing $h_1=-4$, $h_2=4$, $w_1=0$, $w_2=1$, and $w_3=2$ makes them match, and the answer is YES.
在第二个测试用例中,第一个查询的答案为 YES:取 $h_1=1$、$h_2=4$、$w_1=0$、$w_2=1$ 和 $w_3=2$ 即可。第一次更新后,第二个查询中 $a$ 和 $b$ 的子矩阵分别为 $$\begin{bmatrix}6 & 1 & 1 \\ 2 & 2 & 2\end{bmatrix}, \quad \begin{bmatrix}2 & 3 & 4 \\ 6 & 7 & 8\end{bmatrix},$$ 可以证明答案为 NO。第二次更新后,第三个查询中的两个子矩阵分别为 $$\begin{bmatrix}6 & 6 & 6 \\ 2 & 2 & 2\end{bmatrix}, \quad \begin{bmatrix}2 & 3 & 4 \\ 6 & 7 & 8\end{bmatrix},$$ 因此取 $h_1=-4$、$h_2=4$、$w_1=0$、$w_2=1$ 和 $w_3=2$,即可使它们匹配,答案为 YES。AI (CI翻译)
In the third test case, after both updates, the submatrices of $a$ and $b$ in the first query are $$\begin{bmatrix}1 & 0 & 0 & 0 & 0 \\ 2 & 7 & 7 & -5 & -5 \\ 3 & 7 & 7 & -5 & -5 \\ 4 & 0 & 0 & -5 & -5\end{bmatrix}, \quad \begin{bmatrix}2 & 2 & 3 & 4 & 5 \\ 4 & 3 & 4 & 5 & 6 \\ 6 & 4 & 5 & 6 & 7 \\ 8 & 5 & 6 & 7 & 8\end{bmatrix},$$ and it can be proved that the answer is NO. In the second query, they are $$\begin{bmatrix}-5 & -5 \\ -5 & -5 \\ -5 & -5\end{bmatrix}, \quad \begin{bmatrix}5 & 6 \\ 6 & 7 \\ 7 & 8\end{bmatrix},$$ so choosing $h_2=10$, $h_3=11$, $h_4=12$, $w_4=0$, and $w_5=1$ makes them match, and the answer is YES. In the third query, choosing $h_2=0$, $h_3=1$, $w_1=2$, $w_2=-4$, $w_3=-3$, $w_4=10$, and $w_5=11$ makes the submatrices match, so the answer is YES. It can be proved that the answer to the fourth query is NO.
在第三个测试用例中,进行两次更新后,第一个查询中 $a$ 和 $b$ 的子矩阵分别为 $$\begin{bmatrix}1 & 0 & 0 & 0 & 0 \\ 2 & 7 & 7 & -5 & -5 \\ 3 & 7 & 7 & -5 & -5 \\ 4 & 0 & 0 & -5 & -5\end{bmatrix}, \quad \begin{bmatrix}2 & 2 & 3 & 4 & 5 \\ 4 & 3 & 4 & 5 & 6 \\ 6 & 4 & 5 & 6 & 7 \\ 8 & 5 & 6 & 7 & 8\end{bmatrix},$$ 可以证明答案为 NO。对于第二个查询,两个子矩阵分别为 $$\begin{bmatrix}-5 & -5 \\ -5 & -5 \\ -5 & -5\end{bmatrix}, \quad \begin{bmatrix}5 & 6 \\ 6 & 7 \\ 7 & 8\end{bmatrix},$$ 因此取 $h_2=10$、$h_3=11$、$h_4=12$、$w_4=0$ 和 $w_5=1$,即可使它们匹配,答案为 YES。对于第三个查询,取 $h_2=0$、$h_3=1$、$w_1=2$、$w_2=-4$、$w_3=-3$、$w_4=10$ 和 $w_5=11$,即可使两个子矩阵匹配,因此答案为 YES。可以证明,第四个查询的答案为 NO。AI (CI翻译)
样例
3
2 2
0 0
0 -1
1 2
3 4
U 1 1 1 1 1
Q 1 1 2 2
3 5
1 1 1
2 2 2
3 3 3
2 3 4
6 7 8
10 11 12
Q 1 1 2 3
U 1 1 1 1 5
Q 1 1 2 3
U 1 2 1 3 5
Q 1 1 2 3
5 6
1 0 0 0 0
2 0 0 0 0
3 0 0 0 0
4 0 0 0 0
5 0 0 0 0
2 2 3 4 5
4 3 4 5 6
6 4 5 6 7
8 5 6 7 8
10 6 7 8 9
U 2 2 3 3 7
U 2 4 4 5 -5
Q 1 1 4 5
Q 2 4 4 5
Q 2 1 3 5
Q 1 1 2 3
Yes
Yes
No
Yes
No
Yes
Yes
No
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。