Cod sursa(job #2461136)

Utilizator Asgari_ArminArmin Asgari Asgari_Armin Data 24 septembrie 2019 21:59:11
Problema Range minimum query Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.72 kb
#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;
}