Pagini recente » Cod sursa (job #1091143) | Cod sursa (job #2737845) | Cod sursa (job #990198) | Cod sursa (job #376968) | Cod sursa (job #2646568)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("rmq.in");
ofstream fout ("rmq.out");
const int DIM = 100000 + 5;
int a[DIM], lg[DIM], rmq[20][DIM];
int main()
{
int n, m;
fin >> n >> m;
for(int i = 1; i <= n; ++i) fin >> a[i];
for(int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1;
for(int i = 1; i <= n; ++i) rmq[0][i] = a[i];
for(int i = 1; (1 << i) <= n; ++i) {
for(int j = 1; j + (1 << i) - 1 <= n; ++j) {
rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]);
}
}
for(int i = 1; i <= m; ++i) {
int x, y;
fin >> x >> y;
int dif = y - x + 1;
int base = lg[dif];
fout << min(rmq[base][x], rmq[base][x + dif - (1 << base)]) << "\n";
}
return 0;
}