#P4030. [Code+#2] 可做题1

[Code+#2] 可做题1

Description

The problem is as follows:

For any nn-by-nn matrix, if for any selection of nn positions with no two in the same row or column, the sum of the values on those positions is always the same, then we call this matrix “clever.” Note that any matrix with n=1n = 1 is clever. For example, the matrix:

1 2 3
4 5 6
7 8 9

is clever because 1+5+9=1+6+8=2+4+9=2+6+7=3+5+7=3+4+8=151+5+9=1+6+8=2+4+9=2+6+7=3+5+7=3+4+8=15.

The matrix:

1 2
2 1

is not clever because 1+12+21+1 \neq 2+2.

Now you are given an n×mn \times m matrix MM and TT queries. Each query asks whether a subsquare is clever.

Input Format

Read from standard input.

The first line contains three positive integers n,m,Tn, m, T.

The next nn lines each contain mm space-separated non-negative integers, representing the matrix MM.

The next TT lines each contain three positive integers x,y,kx, y, k, indicating the kk-by-kk square whose top-left corner is at row xx, column yy. It is guaranteed that this square lies entirely within MM.

Output Format

Output to standard output.

Print TT lines, each containing a single character Y or N. Y means the queried square is clever, and N means it is not.

3 3 4
1 1 1
1 1 1
1 1 2
1 1 2
1 1 3
2 2 2
2 1 2
Y
N
N
Y

Hint

Constraints: For all the testdata, 0Mij1090 \leq M_{ij} \leq 10^9, 1xn1 \leq x \leq n, 1ym1 \leq y \leq m.

From CodePlus 2017 December Contest, proudly presented by the Student Association for Algorithms and Programming Contest, Department of Computer Science and Technology, Tsinghua University.

Credit: idea/卢政荣 命题/卢政荣 验题/吕时清,王聿中.

Git Repo: https://git.thusaac.org/publish/CodePlus201712

Thanks to Tencent for supporting this contest.

Translated by ChatGPT 5