Pagini recente » Profil yoanaa_kiss | Rating Sfiriac Bianca (steluta) | Cod sursa (job #24986) | Profil Cudrici_Carina | Cod sursa (job #3294900)
#include <iostream>
using namespace std;
const int NMAX = 2 * 1e5;
const int LOG = 20;
const char nl = '\n';
int sptbl[NMAX + 5][LOG];
int a[NMAX + 5];
int log_bin[NMAX + 5];
int query(int l, int r) {
int len = r - l + 1;
int k = log_bin[len];
return min(sptbl[l][k], sptbl[r - (1 << k) + 1][k]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
freopen("rmq.in", "r", stdin);
freopen("rmq.out", "w", stdout);
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
sptbl[i][0] = a[i];
}
log_bin[1] = 0;
for (int i = 2; i <= n; ++i) {
log_bin[i] = log_bin[i / 2] + 1;
}
for (int j = 1; j < LOG; ++j) {
for (int i = 1; i + (1 << j) - 1 <= n; ++i) {
sptbl[i][j] = min(sptbl[i][j - 1], sptbl[i + (1 << (j - 1))][j - 1]);
}
}
while (q--) {
int x, y;
cin >> x >> y;
cout << query(x, y) << nl;
}
return 0;
}