#P7599. [APIO2021] 雨林跳跃

    ID: 6605 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2021APIO交互题Special JudgeO2优化

[APIO2021] 雨林跳跃

Description

In the tropical rainforest on Sumatra Island, there are NN trees in a row. From left to right, they are numbered from 00 to N1N-1. The height of tree ii is H[i]H[i], and all tree heights are pairwise distinct.

Pak Dengklek is training an orangutan so that she can jump from one tree to another. In one jump, the orangutan can jump left or right from the current tree to the first tree that is taller than the current one. Formally, if the orangutan is currently on tree xx, then she can jump to tree yy if and only if one of the following holds:

  • yy is the largest non-negative integer less than xx among all zz such that H[z]>H[x]H[z]>H[x]; or:
  • yy is the smallest non-negative integer greater than xx among all zz such that H[z]>H[x]H[z]>H[x].

Pak Dengklek has QQ jumping plans. Each plan is described by four integers AA, BB, CC, and DD (AB<CDA \le B<C \le D). For each plan, Pak Dengklek wants to know whether the orangutan can start from some tree ss (AsBA \le s \le B), perform some number of jumps, and reach some tree ee (CeDC \le e \le D). If the plan is feasible, Pak Dengklek also wants to know the minimum number of jumps needed among all feasible ways.

You need to implement the following functions:

void init(int N, int[] H)

  • NN: the number of trees.
  • HH: an array of size NN, where H[i]H[i] is the height of tree ii.
  • This function will be called exactly once before the first call to minimum_jumps.

int minimum_jumps(int A, int B, int C, int D)

  • A,BA,B: the range of tree indices that can be used as the starting point.
  • C,DC,D: the range of tree indices that can be used as the ending point.
  • This function should return the minimum number of jumps among all feasible ways, or return 1-1 if the plan is not feasible.
  • This function will be called exactly QQ times.

Input Format

The sample grader program reads the input in the following format:

  • Line 11: NN QQ.
  • Line 22: H[0]H[0] H[1]H[1] \cdots H[N1]H[N-1].
  • Line 3+i3+i (0iQ10 \le i \le Q-1): AA BB CC DD, representing the parameters of the ii-th call to minimum_jumps.

Output Format

The sample grader program outputs your answers in the following format:

  • Line 1+i1+i (0iQ10 \le i \le Q-1): the return value of the ii-th call to minimum_jumps.
7 3
3 2 1 6 4 5 7
4 4 6 6
1 3 5 6
0 1 2 2

2
1
-1

Hint

Sample Explanation

Consider the following call:

init(7, [3, 2, 1, 6, 4, 5, 7])

After initialization, consider the following call:

minimum_jumps(4, 4, 6, 6)

This plan means the orangutan must start from tree 44 (height 44) and reach tree 66 (height 77).

One feasible way with the minimum number of jumps is: first jump to tree 33 (height 66), then jump to tree 66.

Another way is: first jump to tree 55 (height 55), then jump to tree 66.

Therefore, minimum_jumps should return 22.

Consider another call:

minimum_jumps(1, 3, 5, 6)

This plan means the orangutan must start from one of tree 11 (height 22), tree 22 (height 11), or tree 33 (height 66), and finally reach either tree 55 (height 55) or tree 66 (height 77).

The only feasible way with the minimum number of jumps is: start from tree 33 and jump directly to tree 66.

Therefore, minimum_jumps should return 11.

Consider another call:

minimum jumps(0, 1, 2, 2)

This plan means the orangutan must start from either tree 00 (height 33) or tree 11 (height 22), and finally reach tree 22 (height 11).

Since tree 22 has the smallest height, it is impossible to jump to tree 22 from any other tree.

Therefore, minimum_jumps should return 1-1.

Constraints

  • 2N2×1052 \le N \le 2 \times {10}^5.
  • 1Q1051 \le Q \le {10}^5.
  • 1H[i]N1 \le H[i] \le N (0iN10 \le i \le N - 1).
  • H[i]H[j]H[i]\ne H[j] (0i<jN10 \le i<j \le N - 1).
  • 0AB<CDN10 \le A \le B<C \le D \le N - 1.

Subtasks

  1. (4 points): H[i]=i+1H[i]=i+1 (0iN10 \le i \le N-1).
  2. (8 points): N,Q200N,Q \le 200.
  3. (13 points): N,Q2000N,Q \le 2000.
  4. (12 points): Q5Q \le 5.
  5. (23 points): A=BA=B, C=DC=D.
  6. (21 points): C=DC=D.
  7. (19 points): no additional constraints.

Translated by ChatGPT 5