Cod sursa(job #2864837)

Utilizator George_CristianGeorge Dan-Cristian George_Cristian Data 8 martie 2022 11:45:21
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>
#include <cmath>

using namespace std;

ifstream f("rmq.in");
ofstream g("rmq.out");

int n, m, v[100005], dp[100005][(int) log2(100005) + 1];

void citire() {
    f >> n >> m;
    for (int i = 1; i <= n; ++i) {
        f >> v[i];
        dp[i][0] = i;
    }
}

void dinamica() {
    int linmax, colmax = log2(n), put = 1;
    for (int j = 1; j <= colmax; ++j) {
        linmax = n - (put << 1) + 1;
        for (int i = 1; i <= linmax; ++i)
            dp[i][j] = v[dp[i][j - 1]] < v[dp[i + put][j - 1]] ? dp[i][j - 1] : dp[i + put][j - 1];
        put <<= 1;
    }
}

void afisare() {
    int st, dr;
    for (int i = 1; i <= m; ++i) {
        f >> st >> dr;
        int l = log2(dr - st + 1);
        g << min(v[dp[st][l]], v[dp[dr - (1 << l) + 1][l]]) << '\n';
    }
}

int main() {
    citire();
    dinamica();
    afisare();
    return 0;
}