Pagini recente » Cod sursa (job #800295) | Cod sursa (job #3257855) | Cod sursa (job #1611001) | Cod sursa (job #1159277) | Cod sursa (job #2906995)
#include <fstream>
using namespace std;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
int rmq[200002][19], lg[200002], N, M;
int index1, index2, a, b, aux;
int main() {
fin >> N >> M;
lg[1] = 0;
for (index1 = 2; index1 <= N; ++index1)
lg[index1] = 1 + lg[index1 / 2];
for (index1 = 1; index1 <= N; ++index1) {
fin >> rmq[index1][0];
for (index2 = 1; (1 << index2) <= index1; ++index2)
rmq[index1][index2] = min(rmq[index1 - (1 << (index2 - 1))][index2 - 1], rmq[index1][index2 - 1]);
}
for (index1 = 1; index1 <= M; index1 += 1) {
fin >> a >> b;
aux = lg[b - a + 1];
fout << min(rmq[a + (1 << aux) - 1][aux], rmq[b][aux]) << "\n";
}
fin.close();
fout.close();
return 0;
}