Problem 682
Range Minimum Queries
Medium · 20 pts · Array, Sparse Table
Problem
You are given an array of N integers (positions 1 to N) that never changes, and Q queries. Each query (l, r) asks for the smallest value among A[l], ..., A[r]. Answer every query. Build a sparse table: best[k][i] holds the minimum of the 2^k elements starting at i. Any range is covered by two overlapping blocks of the same power-of-two length, so each query takes O(1) after O(N log N) preprocessing.
Input
The first line contains N and Q. The second line contains the N values. Each of the next Q lines contains l and r.
Output
Print the answer to each query, one per line.
Constraints
1 <= N, Q <= 100000; -10^9 <= A[i] <= 10^9; 1 <= l <= r <= N
Example input
6 2 5 2 8 1 9 3 1 3 4 6
Example output
2 1
Explanation
For A = 5 2 8 1 9 3, the minimum of positions 1 to 3 (5, 2, 8) is 2, and the minimum of positions 4 to 6 (1, 9, 3) is 1.