Cod sursa(job #2904049)

Utilizator iulia.talpalariuIulia-Georgiana Talpalariu iulia.talpalariu Data 17 mai 2022 22:03:47
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <fstream>
int matrice[17][100001];
int main() {
    std::fstream fileIn("rmq.in");
    std::ofstream fileOut("rmq.out");
    int n, m;
    fileIn >> n >> m;
    for(int i = 1; i <= n ; ++i) {
        fileIn >> matrice[0][i]; // pe prima linie pun vectorul nostru

    }
    //preprocesez matricea cu minime pe intervale de puteri a lui 2
    for(int i = 1 ; (1 << i) <=n; ++i) {
        for(int j = 1; j <=  n- (1 << i) + 1; ++j){
            matrice[i][j] = (matrice[i-1][j] < matrice[i-1][(1<<(i-1)) + j]) ? matrice[i-1][j] :  matrice[i-1][(1<<(i-1))+j];
        }
    }
    //construiesc rasp
    int a,b;
    for(int i = 0; i < m; ++i) {
        fileIn >> a >> b;
        int lungime= b - a + 1;
        int k = 0;
        while((1<<(k + 1)) <= lungime) k++;//calculez log afland poz MSB
        fileOut << ((matrice[k][a] < matrice[k][b-(1<<k) + 1]) ? matrice[k][a] : matrice[k][b - (1 << k) + 1] ) << '\n';
    }

    return 0;
}