Spiral Matrix

아직 제출이 없습니다시간 제한4초메모리 제한512 MB

문제

Define a neighbouring pair of cells in a matrix as a pair of cells (r_a,c_a)(r\_a, c\_a) and (r_b,c_b)(r\_b, c\_b) such that:

  • either r_a=r_br\_a = r\_b and c_ac_b=1|c\_a - c\_b| = 1,
  • or c_a=c_bc\_a = c\_b and r_ar_b=1|r\_a - r\_b| = 1.

Define a spiral matrix as a matrix which satisfies the following conditions:

  • The matrix contains only distinct positive integers.
  • One can start from some cell (i,j)(i, j) and arrange all other cells in a path, so that every two consecutive cells in the path are a neighbouring pair, and by following the path  from (i,j)(i, j) and considering the values in the matrix, we form a continuous integer interval \[l..r]\[l..r] in the order of visiting them.

Given is a matrix of size n×mn \times m consisting of distinct positive integers. We are also given qq queries. Each query defines a submatrix with corners (r_1,c_1)(r\_1, c\_1) and (r_2,c_2)(r\_2, c\_2). For each query, determine whether this submatrix is spiral.

입력

The first line contains three integers nn, mm and qq (1n,m20001 \le n, m \le 2000, 1q1061 \le q \le 10^6), denoting the size of the matrix and the number of queries, respectively.

Each of the next nn lines contains mm integers. The jj-th integer on the ii-th of these lines denotes the element a_i,ja\_{i, j} located in the ii-th row and jj-th column of the matrix (1a_i,j1091 \le a\_{i, j} \le 10^9). It is guaranteed that all elements are distinct.

Each of the next qq lines contains four integers r_1r\_1, c_1c\_1, r_2r\_2, c_2c\_2 (1r_1r_2n1 \le r\_1 \le r\_2 \le n, 1c_1c_2m1 \le c\_1 \le c\_2 \le m), denoting the corners of a submatrix.

출력

For each query, print the answer on a separate line. Print "YES" if the submatrix is spiral, or "NO" otherwise.