#P13540. [IOI 2025] Obstacles for a Llama

[IOI 2025] Obstacles for a Llama

题目描述

A llama wants to travel through the Andean Plateau. It has a map of the plateau in the form of a grid of N×MN \times M square cells. The rows of the map are numbered from 00 to N1N-1 from top to bottom, and the columns are numbered from 00 to M1M-1 from left to right. The cell of the map in row ii and column jj (0i<N0 \leq i < N, 0j<M0 \leq j < M) is denoted by (i,j)(i, j).

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 TT and HH of length NN and MM respectively. Here T[i]T[i] (0i<N0 \leq i < N) indicates the temperature of the cells in row ii, and H[j]H[j] (0j<M0 \leq j < M) indicates the humidity of the cells in column jj.

The llama has also studied the flora of the plateau and noticed that a cell (i,j)(i, j) is free of vegetation if and only if its temperature is greater than its humidity, formally T[i]>H[j]T[i] > H[j].

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 QQ questions. For each question, you are given four integers: LL, RR, SS, and DD. You must determine whether there exists a valid path such that:

  • The path starts at cell (0,S)(0, S) and ends at cell (0,D)(0, D).
  • All cells in the path lie within columns LL to RR, inclusive.

It is guaranteed that both (0,S)(0, S) and (0,D)(0, D) are free of vegetation.

Implementation Details

The first procedure you should implement is:

void initialize(std::vector<int> T, std::vector<int> H)
  • TT: an array of length NN specifying the temperature in each row.
  • HH: an array of length MM 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)
  • L,R,S,DL, R, S, D: integers describing a question.
  • This procedure is called QQ times for each test case.

This procedure should return true if and only if there exists a valid path from cell (0,S)(0, S) to cell (0,D)(0, D), such that all cells in the path lie within columns LL to RR, inclusive.

输入格式

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, L[k]L[k], R[k]R[k], S[k]S[k] and D[k]D[k] (0k<Q0 \leq k < Q) specify the parameters for each call to can_reach.

输出格式

Output format:

A[0]
A[1]
...
A[Q-1]

Here, A[k]A[k] (0k<Q0 \leq k < Q) is 11 if the call can_reach(L[k], R[k], S[k], D[k]) returned true, and 00 otherwise.



提示

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 L=0L = 0 to R=3R = 3, and the black disks indicate the starting and ending cells:

In this case, the llama can reach from cell (0,1)(0, 1) to cell (0,3)(0, 3) through the following valid path:

$$(0, 1), (0, 0), (1, 0), (2, 0), (2, 1), (2, 2), (2, 3), (1, 3), (0, 3) $$

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 (0,1)(0,1) to cell (0,3)(0,3), such that all cells in the path lie within columns 11 to 33, inclusive. Therefore, this call should return false.

Constraints

  • 1N,M,Q2000001 \leq N, M, Q \leq 200000
  • 0T[i]1090 \leq T[i] \leq 10^9 for each ii such that 0i<N0 \leq i < N.
  • 0H[j]1090 \leq H[j] \leq 10^9 for each jj such that 0j<M0 \leq j < M.
  • 0LR<M0 \leq L \leq R < M
  • LSRL \leq S \leq R
  • LDRL \leq D \leq R
  • Both cells (0,S)(0, S) and (0,D)(0, D) are free of vegetation.

Subtasks

Subtask Score Additional Constraints
1 10 L=0,R=M1L = 0, R = M - 1 for each question. N=1N = 1.
2 14 L=0,R=M1L = 0, R = M - 1 for each question. T[i1]T[i]T[i - 1] \leq T[i] for each ii such that 1i<N1 \leq i < N.
3 13 L=0,R=M1L = 0, R = M - 1 for each question. N=3N = 3 and T=[2,1,3]T = [2, 1, 3].
4 21 L=0,R=M1L = 0, R = M - 1 for each question. Q10Q \leq 10.
5 25 L=0,R=M1L = 0, R = M - 1 for each question.
6 17 No additional constraints.