Reachability in a Matrix
시간 제한3초메모리 제한2048 MB
서로 다른 값을 가진 n×m 격자와 임계값 k가 주어질 때, 한 칸에서 다른 칸으로 가는 유향 경로가 존재하는지 묻는 질의에 답한다.
문제
You are given a matrix of size consisting of distinct integers from to . The rows of the matrix are numbered from to , and the columns are numbered from to . Also, a positive integer is given.
Let us construct a graph consisting of vertices, where the vertices will be the cells of the matrix, labeled as (, ). We will draw a directed edge from cell to cell if both of the following conditions are met:
- The cells are in the same row or column of the matrix. More formally, or .
- .
You are given queries of the form . You need to determine whether there exists a path in this graph along the directed edges, starting at vertex and ending at vertex .
입력
The first line of the input file contains three integers, , , and (, ).
Each of the next lines contains integers separated by spaces: the values (). It is guaranteed that all numbers in the matrix are distinct.
The next line contains a single integer : the number of queries ().
Each of the next lines contains four integers, , , , and : the vertices in the -th query (, , ).
출력
For each of the 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 and .
In the fourth query, there exists a path .