Pagini recente » Cod sursa (job #3252226) | Cod sursa (job #2622122) | Cod sursa (job #1788518) | Cod sursa (job #1412736) | Cod sursa (job #2775822)
#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[21][N], lg[N];
int main()
{
usain_bolt();
int n, q;
fin >> n >> q;
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(; 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;
}