#C3896. Magic Matrix Submatrices
Magic Matrix Submatrices
Magic Matrix Submatrices
You are given a matrix and multiple queries. Each query specifies four integers (r_1, c_1, r_2, c_2) representing the top-left and bottom-right coordinates of a submatrix (using 1-indexing). Your task is to check if the submatrix is a magic matrix. A submatrix is magic if the sum of every row is equal and the sum of every column is equal. Formally, if we denote (S) as the common sum, then for every row (i) in (r_1 \le i \le r_2) we must have: [ \sum_{j=c_1}^{c_2} a_{i,j} = S, ] and for every column (j) in (c_1 \le j \le c_2): [ \sum_{i=r_1}^{r_2} a_{i,j} = S. ] Output "YES" if the submatrix is magic, and "NO" otherwise.
inputFormat
The input is read from standard input. The first line contains two integers (r) and (c) representing the number of rows and columns of the matrix. The following (r) lines each contain (c) space-separated integers representing the matrix rows. Next, a line with an integer (q) is given which denotes the number of queries. Each of the next (q) lines contains four integers (r_1, c_1, r_2, c_2) that specify a submatrix using 1-indexed coordinates.
outputFormat
For each query, output a single line to standard output containing either "YES" if the submatrix is magic, or "NO" otherwise.## sample
4 4
4 4 4 4
4 4 4 4
4 4 4 4
4 4 4 4
1
1 1 2 2
YES