Cod sursa(job #942493)
#include <fstream>
#include <cstdlib>
using namespace std;
const int NMAX = 100011;
const int LOGNMAX = 17;
int rmq[LOGNMAX][NMAX];
inline int log2(int N)
{
#ifdef __GNUC__
return (0 == N) ? -1 : 8 * sizeof(N) - __builtin_clz(N) - 1;
#else
int count = -1;
for(; N; N >>= 1, ++count);
return count;
#endif // __GNUC__
}
inline int min(int x, int y) {return x <= y ? x : y;}
int main()
{
int N, M, i, iPow2, till, j, start, end, log2N;
ifstream in("rmq.in");
ofstream out("rmq.out");
in >> N >> M;
for(i = 1; i <= N; ++i)
in >> rmq[0][i];
log2N = log2(N);
for(i = 1, iPow2 = 2; i <= log2N; ++i, iPow2 <<= 1)
{
till = N - iPow2 + 1;
for(j = 1; j <= till; ++j)
rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j + (iPow2 >> 1)]);
}
for(; M; --M)
{
in >> start >> end;
log2N = log2(end - start + 1);
out << min(rmq[log2N][start], rmq[log2N][end - (1 << log2N) + 1]) << '\n';
}
return EXIT_SUCCESS;
}