Pagini recente » Cod sursa (job #668859) | Cod sursa (job #377054) | Monitorul de evaluare | Cod sursa (job #182818) | Cod sursa (job #2284602)
#include <bits/stdc++.h>
#define MAXN 100005
#define LOGMAX 18
using namespace std;
inline int log2(int x) {
if (x == 1) return 0;
return 1 + log2(x/2);
}
int N, M;
int V[MAXN];
int RMQ[LOGMAX][MAXN];
void ComputeRMQ() {
for (int i=1; i<=N; ++i)
RMQ[0][i] = V[i];
for (int i=1, j, p=2; p<=N; ++i, p*=2)
for (j=1; j+p-1<=N; ++j)
RMQ[i][j] = min(RMQ[i-1][j], RMQ[i-1][j+p/2]);
}
int RMQQuery(int X, int Y) {
int log = log2(Y-X+1);
return min(RMQ[log][X], RMQ[log][Y-(1<<log)+1]);
}
ifstream fin("rmq.in");
ofstream fout("rmq.out");
void Citire() {
fin >> N >> M;
for (int i=1; i<=N; ++i)
fin >> V[i];
}
void Rezolvare() {
ComputeRMQ();
int X, Y;
while (M--) {
fin >> X >> Y;
fout << RMQQuery(X, Y) << '\n';
}
}
int main()
{
Citire();
Rezolvare();
return 0;
}