Snakes on a Grid

시간 제한3초메모리 제한256 MB

요약
Q개의 부분 직사각형마다 같은 값을 가진 연결 성분이 모두 뱀 모양인지 판정한다.
난이도

어려움10점 중 9점

유형
그래프, BFS, 누적 합, 구현
정답자
아직 제출이 없습니다

문제

On a rectangular grid, a snake of size kk is defined to be a subset of cells that is formed by taking the first kk 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 N×MN \times M grid with rows numbered 0,…,N−10, \dots, N-1 and columns numbered 0,…,M−10, \dots, M-1, where each cell (i,j)(i, j) in row ii, column jj has a value a_ija\_{ij}. Since he's afraid of snakes, he wants you to answer QQ 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, NN and MM (1≤N,M≤10001 \leq N, M \leq 1000) --- the dimensions of the grid.

Each of the next NN rows contains MM integers, with the jj-th element of the ii-th row being a_i,ja\_{i,j} (1≤a_i,j≤1061 \leq a\_{i, j} \leq 10^6) --- the number written in the jj-th cell of the ii-th row.

The next line contains a single integer, QQ (1≤Q≤5⋅1051 \leq Q \leq 5 \cdot 10^5) --- the number of queries.

The next QQ lines describe the queries, Each line contains 4 integers x_1x\_1, y_1y\_1, x_2x\_2, y_2y\_2 (0≤x_1≤x_2<N,(0 \leq x\_1 \leq x\_2 < N, 0≤y_1≤y_2<M) 0 \leq y\_1 \leq y\_2 < M), representing a query asking whether the rectangular subgrid with top left corner at (x_1,y_1)(x\_1, y\_1) and bottom right corner at (x_2,y_2)(x\_2, y\_2) 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.

예제1

  1. 예제 1

    입력
    10 9
    1 1 2 2 3 3 2 2 1
    4 1 1 3 3 2 2 4 4
    4 4 1 1 2 2 3 4 4
    4 2 2 1 1 3 3 4 4
    2 2 3 4 4 2 2 3 3
    1 1 3 3 4 4 2 2 3
    2 1 1 3 3 4 4 2 4
    2 2 1 2 4 4 3 3 4
    1 3 3 4 4 2 2 3 4
    3 3 1 1 1 1 4 4 4
    5
    0 1 6 5
    5 4 7 8
    0 6 2 8
    6 0 9 3
    8 5 9 8
    
    예상 출력
    YES
    NO
    NO
    YES
    NO