Cod sursa(job #3161207)

Utilizator vladorovOroviceanu Vlad vladorov Data 26 octombrie 2023 08:48:17
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

int main(){
    ifstream fin("rmq.in");
    ofstream fout("rmq.out");

    int lim; fin>>lim;
    int nrq; fin>>nrq;

    int rmq[(int)log2(lim)+1][lim];

    for(int i=0; i<lim; i++){
        fin>>rmq[0][i];
    }

    for(int p=2; (1<<p-1)<=lim; p++){
        for(int i=0; i<lim; i++){
            rmq[p-1][i]=rmq[p-2][i];

            if(i+(1<<p-2)<lim) rmq[p-1][i]=min(rmq[p-2][i], rmq[p-2][i+(1<<p-2)]);

        }
    }

    for(int i=0; i<nrq; i++){
        int p1, p2; fin>>p1>>p2; p1--; p2--;

        int exp=(int)log2(p2-p1+1);

        fout<<min(rmq[exp][p1], rmq[exp][p2-(1<<exp)+1])<<endl;
    }

    return 0;
}