Pagini recente » Cod sursa (job #314808) | Cod sursa (job #585269) | Cod sursa (job #1961505) | Cod sursa (job #2725644) | Cod sursa (job #3197179)
#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 getLastPow(int d) {
int last = 0;
for(int crt = 1; 1 << crt <= d; crt++)
last = crt;
return last;
}
int query(int l, int r) {
int j = getLastPow(r - l);
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;
}