Snakes on a Grid
시간 제한3초메모리 제한256 MB
Q개의 부분 직사각형마다 같은 값을 가진 연결 성분이 모두 뱀 모양인지 판정한다.
문제
On a rectangular grid, a snake of size is defined to be a subset of cells that is formed by taking the first numbered cells in the following figure, where cells are numbered along a path with alternating right and down steps:

Additionally, subsets that are rotations, translations, or reflections of these shapes are also considered snakes. A grid is considered good if every connected component consisting of equal values in the grid is a snake.
Busy Beaver has an grid with rows numbered and columns numbered , where each cell in row , column has a value . Since he's afraid of snakes, he wants you to answer of the following queries: given a contiguous subrectangle of the grid, determine whether or not the subgrid is good.
입력
The first line contains 2 integers, and () --- the dimensions of the grid.
Each of the next rows contains integers, with the -th element of the -th row being () --- the number written in the -th cell of the -th row.
The next line contains a single integer, () --- the number of queries.
The next lines describe the queries, Each line contains 4 integers , , , , representing a query asking whether the rectangular subgrid with top left corner at and bottom right corner at is good.
출력
For each query, output "YES" (without quotes) if the corresponding subgrid consists only of snakes, and "NO" (without quotes) otherwise.
힌트
If we color the sample grid with respect to the value in each cell, we get

In the first query, all components are snakes.

In the second query, the component that is not a snake is colored in black.

In the third query, the component that is not a snake is colored in black.

In the fourth query, all components are snakes.

In the fifth query, the component that is not a snake is colored in black.
