Cod sursa(job #2904038)

Utilizator iulia.talpalariuIulia-Georgiana Talpalariu iulia.talpalariu Data 17 mai 2022 22:01:26
Problema Range minimum query Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.12 kb
#include <fstream>
#include <iostream>
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
                std::cout << matrice[0][i]<<'\n';

    }
    ///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];
            std::cout << matrice[i][j] << " ";
        }
    }
    ///construiesc rasp
    int a,b;
    for(int i = 0; i < m; ++i) {
        fileIn >> a >> b;
        std::cout << a << b << '\n';
        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;
}