Pagini recente » Cod sursa (job #709443) | Cod sursa (job #1740862) | Cod sursa (job #2209765) | Cod sursa (job #699253) | Cod sursa (job #3196128)
#include<bits/stdc++.h>
using namespace std;
const int N = 100009;
const int L = 20;
ifstream in ("rmq.in");
ofstream out("rmq.out");
// auto& in = cin;
// auto& out = cout;
int n, m;
long long sp[L][N];
void init() {
for(int j = 0; (1 << j) < n; j++)
for(int i = 0; i + (1 << j) < n; i++)
sp[j+1][i] = min(sp[j][i], sp[j][i + (1 << j)]);
}
int query(int l, int r) {
int d = r - l;
int j = 0;
while((1 << j) <= d)
j++;
j--;
return min(sp[j][l], sp[j][r - (1 << j) + 1]);
}
int main(){
in>>n>>m;
for(int i = 0; i < n; i++)
in>>sp[0][i];
init();
int a, b;
for(int i = 0; i < m; i++) {
in>>a>>b;
out<<query(a-1, b-1)<<endl;
}
return 0;
}