Cod sursa(job #1514601)

Utilizator cristina_borzaCristina Borza cristina_borza Data 31 octombrie 2015 12:51:25
Problema Range minimum query Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>

#define NMAX 1000005

using namespace std;

ifstream f("rmq.in");
ofstream g("rmq.out");

int n , m , x , y , sol;
int r[NMAX][25] , v[NMAX] , lg[25];

int main() {
    f >> n >> m;

    for(int i = 1 ; i <= n ; ++i) {
        f >> v[i];
    }

    for(int i = 1 ; i <= n ; ++i) {
        r[i][0] = v[i];
        for(int j = 1 ; (1 << (j - 1)) <= i ; ++j) {
            r[i][j] = min(r[i - (1 << (j - 1))][j - 1] , r[i][j - 1]);
        }
    }

    for(int i = 1 ; i <= n ; ++i) {
        int aux = 1;
        while((1 << aux) <= i) {
            ++aux;
        }

        lg[i] = aux - 1;
    }

    for(int i = 1 ; i <= m ; ++i) {
        f >> x >> y;

        int aux = lg[y - x + 1];
        sol = min(r[y][aux] , r[x + (1 << aux) - 1][aux]);

        g << sol << '\n';

    }
    return 0;
}