Pagini recente » Cod sursa (job #2372777) | Cod sursa (job #2430096) | Cod sursa (job #1701676) | Cod sursa (job #2145548) | Cod sursa (job #2723425)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("rmq.in");
ofstream fout ("rmq.out");
void usain_bolt()
{
ios::sync_with_stdio(false);
fin.tie(0);
}
const int N = 1e5 + 5;
int a[N], rmq[20][N], lg[N];
int main()
{
usain_bolt();
int n, q;
fin >> n >> q;
for(int i = 1; i <= n; ++i) {
fin >> a[i];
}
lg[2] = 1;
for(int i = 3; i <= n; ++i) {
lg[i] = lg[i / 2] + 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 <= n - (1 << i) + 1; ++j) {
rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j + (1 << (i - 1))]);
}
}
for(; q; --q) {
int x, y;
fin >> x >> y;
int dif = y - x + 1;
int base = lg[dif];
fout << min(rmq[base][x], rmq[base][y - (1 << base) + 1]) << "\n";
}
return 0;
}