Cod sursa(job #2906995)

Utilizator madalin1902Florea Madalin Alexandru madalin1902 Data 28 mai 2022 13:24:17
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <fstream>

using namespace std;

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

int rmq[200002][19], lg[200002], N, M;
int index1, index2, a, b, aux;

int main() {
    fin >> N >> M;
    
    lg[1] = 0;
    for (index1 = 2; index1 <= N; ++index1)
        lg[index1] = 1 + lg[index1 / 2];
    
    for (index1 = 1; index1 <= N; ++index1) {
        fin >> rmq[index1][0];
        for (index2 = 1; (1 << index2) <= index1; ++index2)
            rmq[index1][index2] = min(rmq[index1 - (1 << (index2 - 1))][index2 - 1], rmq[index1][index2 - 1]);
    }
    
    for (index1 = 1; index1 <= M; index1 += 1) {
        fin >> a >> b;
        aux = lg[b - a + 1];
        fout << min(rmq[a + (1 << aux) - 1][aux], rmq[b][aux]) << "\n";
    }
    
    fin.close();
    fout.close();

    return 0;
}