Pagini recente » Cod sursa (job #1009194) | Cod sursa (job #1904488) | Cod sursa (job #1161422) | Cod sursa (job #2985233) | Cod sursa (job #2461136)
#include <fstream>
using namespace std;
ifstream fin( "rmq.in" );
ofstream fout( "rmq.out" );
const int NMAX = 100000;
const int LGMAX = 17;
int rmq[NMAX + 2][LGMAX + 2];
int lg2[NMAX + 2];
int main() {
int n, q, i, p, k, a, b;
fin >> n >> q;
for( i = 1; i <= n; ++i )
fin >> rmq[i][0];
for( i = 2; i <= n; ++i )
lg2[i] = 1 + lg2[i >> 1];
for( k = 1; k <= lg2[n]; ++k )
for( i = 1; i <= n - (1 << lg2[k]) + 1; ++i )
rmq[i][k] = min( rmq[i][k - 1], rmq[i + (1 << (k - 1))][k - 1] );
for( i = 0; i < q; ++i ){
fin >> a >> b;
fout << min( rmq[a][lg2[b - a + 1]], rmq[b - (1 << lg2[b - a + 1]) + 1][lg2[b - a + 1]] ) << "\n";
}
return 0;
}