Pagini recente » Profil florinhaja | Statistici Pavel Andrei George (andreipavel213) | Cod sursa (job #2024486) | Cod sursa (job #1189623) | Cod sursa (job #2134087)
/*
#include <bits/stdc++.h>
using namespace std;
int main() {
freopen("carici.in", "r", stdin);
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
ios::sync_with_stdio(false);
return 0;
}
*/
#include <bits/stdc++.h>
using namespace std;
#define mypow2(x) (1 << (x))
int n, m, x, y, k;
int lg2[100001];
int D[20][100001];
int main() {
//freopen("carici.in", "r", stdin);
freopen("rmq.in", "r", stdin);
freopen("rmq.out", "w", stdout);
//ios::sync_with_stdio(false);
cin >> n >> m;
lg2[0] = 1;
lg2[1] = 0;
for (int i = 2; i <= n; i++) {
lg2[i] = lg2[i / 2] + 1;
}
for (int i = 1; i <= n; i++) {
scanf("%d", &D[0][i]);
}
for (int i = 1; i <= lg2[n]; i++) {
for (int j = 1; j <= n - mypow2(i - 1); j++) {
D[i][j] = min(D[i - 1][j], D[i - 1][j + mypow2(i - 1)]);
}
}
for (int i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
k = lg2[y - x + 1];
cout << min(D[k][x], D[k][y - mypow2(k) + 1]) << "\n";
}
return 0;
}