#P4030. Clever Matrix

    ID: 17278 Type: Default 1000ms 256MiB

Clever Matrix

Clever Matrix

We call a square matrix of order \(n\) clever if for any selection of \(n\) elements from the matrix, with no two elements in the same row or column (i.e. for any permutation of columns), the sum of the selected elements is the same. Note that every \(1 \times 1\) matrix is clever.

For example, consider the matrix below:

1 2 3
4 5 6
7 8 9

It is clever because \[ 1+5+9 = 1+6+8 = 2+4+9 = 2+6+7 = 3+5+7 = 3+4+8 = 15 \]

However, the matrix

1 2
2 1

is not clever since \(1+1 \neq 2+2\).

You are given a matrix \(M\) with dimensions \(n \times m\) and \(T\) queries. Each query specifies a square submatrix of \(M\) (via its top-left corner and size) and asks whether that submatrix is clever.

inputFormat

The input is given in the following format:

  • The first line contains two integers \(n\) and \(m\) representing the number of rows and columns of the matrix \(M\).
  • The next \(n\) lines each contain \(m\) integers, representing the elements of the matrix.
  • The next line contains an integer \(T\), the number of queries.
  • Each of the following \(T\) lines contains three integers \(r\), \(c\), and \(k\). Here, \(r\) and \(c\) (1-indexed) denote the top-left corner of a submatrix, and \(k\) is the size of the square submatrix. It is guaranteed that the submatrix \(k \times k\) lies within \(M\).

outputFormat

For each query, output a single line containing "Yes" if the specified submatrix is clever, and "No" otherwise.

sample

3 3
1 2 3
4 5 6
7 8 9
3
1 1 3
1 2 2
2 2 2
Yes

Yes Yes

</p>