A llama wants to travel through the Andean Plateau. It has a map of the plateau in the form of a grid of square cells. The rows of the map are numbered from
to
from top to bottom, and the columns are numbered from
to
from left to right. The cell of the map in row
and column
(
) is denoted by
.
The llama has studied the climate of the plateau and discovered that all cells in each row of the map have the same temperature and all cells in each column of the map have the same humidity. The llama has given you two integer arrays and
of length
and
respectively. Here
(
) indicates the temperature of the cells in row
, and
(
) indicates the humidity of the cells in column
.
The llama has also studied the flora of the plateau and noticed that a cell is free of vegetation if and only if its temperature is greater than its humidity, formally
.
The llama can travel across the plateau only by following valid paths. A valid path is a sequence of distinct cells that satisfy the following conditions:
- Each pair of consecutive cells in the path shares a common side.
- All cells in the path are free of vegetation.
Your task is to answer questions. For each question, you are given four integers:
, and
.
You must determine whether there exists a valid path such that:
- The path starts at cell
and ends at cell
.
- All cells in the path lie within columns
to
, inclusive.
It is guaranteed that both and
are free of vegetation.
Implementation Details
The first procedure you should implement is:
void initialize(std::vector<int> T, std::vector<int> H)
: an array of length
specifying the temperature in each row.
: an array of length
specifying the humidity in each column.
- This procedure is called exactly once for each test case, before any calls to
can_reach
.
The second procedure you should implement is:
bool can_reach(int L, int R, int S, int D)
: integers describing a question.
- This procedure is called
times for each test case.
This procedure should return true
if and only if there exists a valid path from cell to cell
,
such that all cells in the path lie within columns
to
, inclusive.
Constraints
for each
such that
.
for each
such that
.
- Both cells
and
are free of vegetation.
Subtasks
Subtask | Score | Additional Constraints |
---|---|---|
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 | No additional constraints. |
Example
Consider the following call:
initialize([2, 1, 3], [0, 1, 2, 0])
This corresponds to the map in the following image, where white cells are free of vegetation:

As the first question, consider the following call:
can_reach(0, 3, 1, 3)
This corresponds to the scenario in the following image, where the thick vertical lines indicate the range of columns from to
, and the black disks indicate the starting and ending cells:

In this case, the llama can reach from cell to cell
through the following valid path:
.
Therefore, this call should return
true
.
As the second question, consider the following call:
can_reach(1, 3, 1, 3)
This corresponds to the scenario in the following image:

In this case, there is no valid path from cell to cell
, such that all cells in the path lie within columns
to
, inclusive.
Therefore, this call should return
false
.
Sample Grader
Input format:
N M
T[0] T[1] ... T[N-1]
H[0] H[1] ... H[M-1]
Q
L[0] R[0] S[0] D[0]
L[1] R[1] S[1] D[1]
...
L[Q-1] R[Q-1] S[Q-1] D[Q-1]
Here, and
(
) specify the parameters for each call to
can_reach
.
Output format:
A[0]
A[1]
...
A[Q-1]
Here, (
) is
if the call
can_reach(L[k], R[k], S[k], D[k])
returned true
, and otherwise.
Attachment Package
The sample grader along with sample test cases are available here.
Comments