luogu#P5097. [USACO04OPEN] Cave Cows 2

[USACO04OPEN] Cave Cows 2

Problem Description

There is a long passage in a cave. It is made up of NN segments (1N250001 \leq N \leq 25000) connected end to end, numbered 1N1 \ldots N.

Each segment has a threshold value in the range [1,109][1,10^9]. If a cow passes through segments iji \ldots j in order, then the cow’s weight index must not exceed the minimum threshold among segments iji \ldots j. Bessie has QQ questions (1Q250001 \leq Q \leq 25000) and wants to ask you for the minimum threshold value from segment ii to segment jj.

Input Format

The first line contains NN and QQ.

The next NN lines contain the threshold value of each segment. After that, the next QQ lines each contain two integers, corresponding to ii and jj in the questions (i<ji < j).

Output Format

For each query, output the answer.

10 4
75
30
100
38
50
51
52
20
81
5
1 10
3 5
6 9
8 10
5
38
20
5

Hint

Translated by ChatGPT 5