QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#17145. Прыжки по вершинам

الإحصائيات

In the computer game Mega Jump, the hero jumps between peaks of a mountain range in order to reach the point with a flag, where the level ends.

The mountain range in the game consists of $n$ consecutive peaks; the $i$-th peak is located at position $i$ and has height $h_i$. For any $i < j$, the hero can jump in a straight line from peak $i$ to peak $j$, provided that during the straight-line flight there are no other peaks on his path. More formally, there must not exist an index $k$ such that $i < k < j$ and the вершина of the $k$-th peak—the point with coordinates $(k, h_k)$—lies strictly above the segment connecting the points $(i, h_i)$ and $(j, h_j)$.

The company Defeat AI is training a neural network to control the hero in this game. To create training data, it is necessary to answer several queries: for a pair of indices $l, r$ ($1 \le l \le r \le n$), determine the minimum number of jumps such that, starting at the peak with index $l$, the hero can reach the peak with index $r$.

Input Format

The first line of the input contains an integer $n$ ($1 \le n \le 10^5$) — the number of peaks.

The second line contains $n$ numbers: $h_1, h_2, \ldots, h_n$ ($0 \le h_i \le 10^{12}$) — the heights of the peaks.

The third line contains an integer $q$ ($1 \le q \le 10^5$) — the number of queries.

Each of the next $q$ lines contains two integers $l_i, r_i$ ($1 \le l_i \le r_i \le n$) — the parameters of the corresponding query.

Output Format

For each query, output on a separate line a non-negative integer — the minimum required number of jumps.

Scoring

Subtask Points Additional Constraints Required Subtasks
1 9 $n, q \le 300$
2 9 $n, q \le 5000$ 1
3 14 $h_i \le 10$
4 21 There exists $k$ such that for all $i$ it holds that $l_i \le k \le r_i$
5 27 $n, q \le 5 \cdot 10^4$ 1, 2
6 20 1–5

Example

standard input

8
5 3 4 3 6 2 1 4
3
1 8
2 7
4 4

standard output

2
2
0

Note

Let us consider the second query from the example. The hero’s path from peak 2 to peak 7 may look as follows:

problem_17145_1.png

He will visit peaks 2, 5, and 7, making two jumps.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.