Cod sursa(job #2904771)

Utilizator DariaClemClem Daria DariaClem Data 18 mai 2022 01:22:38
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.81 kb
#include <bits/stdc++.h>

using namespace std;

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

int rmq[200002][19], lg[200002], nrNumere, nrOperatii;

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