luogu#P5097. [USACO04OPEN] Cave Cows 2

[USACO04OPEN] Cave Cows 2

Problem Description

In one cave Bessie is planning to explore, a long corridor is made up of NN segments (1N25,0001 \le N \le 25,000 and numbered 1..N1..N) joined end-to-end.
Each of these segments has a particular width which can only be traversed by a cow whose "fatness" index is no larger than that width.
A cow can travel along a sequence i..ji..j of these segments only if its fatness index is no larger than the minimum width of all of those corridors. Corridor widths are integers in the range 1..1,000,000,0001..1,000,000,000.

In order to plan her caving expedition, Bessie needs to answer a collection of QQ (1Q25,0001 \le Q \le 25,000) queries of the form "what is the maximum fatness of a cow that can pass through the sequence i..ji..j of corridors?". Please help Bessie with her dilemma.

Input Format

  • Line 11: Two space-separated integers, NN and QQ.

  • Lines 2..N+12..N+1: Each line gives the integer width of a corridor. Line 22 describes corridor 11; line 33 describes corridor 22; and so on.

  • Lines N+2..N+Q+1N+2..N+Q+1: Each line corresponds to a query and contains two space-separated integers ii and jj (where i<ji < j), giving the indices of the corridors at both ends of the query interval.

Output Format

  • Lines 1..Q1..Q: Each line contains the integer answer to a query.
10 4
75
30
100
38
50
51
52
20
81
5
1 10
3 5
6 9
8 10
5
38
20
5