Reachability in a Matrix

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

요약
서로 다른 값을 가진 n×m 격자와 임계값 k가 주어질 때, 한 칸에서 다른 칸으로 가는 유향 경로가 존재하는지 묻는 질의에 답한다.
난이도

어려움10점 중 8점

유형
그래프, 정렬, 동적 계획법, 유니온 파인드
정답자
아직 제출이 없습니다

문제

You are given a matrix AA of size n×mn \times m consisting of distinct integers from 11 to n⋅mn \cdot m. The rows of the matrix are numbered from 11 to nn, and the columns are numbered from 11 to mm. Also, a positive integer kk is given.

Let us construct a graph consisting of n⋅mn \cdot m vertices, where the vertices will be the cells of the matrix, labeled as (a,b)(a, b) (1≤a≤n1 \le a \le n, 1≤b≤m1 \le b \le m). We will draw a directed edge from cell (a,b)(a, b) to cell (c,d)(c, d) if both of the following conditions are met:

  • The cells are in the same row or column of the matrix. More formally, a=ca = c or b=db = d.
  • A_a,b≥A_c,d+kA\_{a, b} \ge A\_{c, d} + k.

You are given qq queries of the form (a,b,c,d)(a, b, c, d). You need to determine whether there exists a path in this graph along the directed edges, starting at vertex (a,b)(a, b) and ending at vertex (c,d)(c, d).

입력

The first line of the input file contains three integers, nn, mm, and kk (1≤n,m≤2501 \le n, m \le 250, 1≤k≤n⋅m1 \le k \le n \cdot m).

Each of the next nn lines contains mm integers separated by spaces: the values A_i,jA\_{i,j} (1≤A_i,j≤n⋅m1 \le A\_{i,j} \le n \cdot m). It is guaranteed that all numbers in the matrix are distinct.

The next line contains a single integer qq: the number of queries (1≤q≤250,0001 \le q \le 250\\,000).

Each of the next qq lines contains four integers, a_ia\_i, b_ib\_i, c_ic\_i, and d_id\_i: the vertices in the ii-th query (1≤a_i,c_i≤n1 \le a\_i, c\_i \le n, 1≤b_i,d_i≤m1 \le b\_i, d\_i \le m, (a_i,b_i)≠(c_i,d_i)(a\_i, b\_i) \neq (c\_i, d\_i)).

출력

For each of the qq queries, output a line with the word "Ia" if a path exists. Otherwise, output a line with the word "Joq".

힌트

In the third query, there exist paths (3,2)→(3,1)→(1,1)(3, 2) \rightarrow (3, 1) \rightarrow (1, 1) and (3,2)→(1,2)→(1,1)(3, 2) \rightarrow (1, 2) \rightarrow (1, 1).

In the fourth query, there exists a path (1,3)→(2,3)→(2,1)(1, 3) \rightarrow (2, 3) \rightarrow (2, 1).

예제1

  1. 예제 1

    입력
    3 3 2
    2 4 6
    1 8 3
    5 9 7
    6
    3 2 1 3
    3 1 1 1
    3 2 1 1
    1 3 2 1
    3 2 2 3
    2 2 3 3
    
    예상 출력
    Joq
    Ia
    Ia
    Ia
    Ia
    Joq