Cod sursa(job #2878842)

Utilizator daristyleBejan Darius-Ramon daristyle Data 27 martie 2022 23:05:46
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;

const int nmaxim = 1e5+1, logaritmi = 20;
int n, v[nmaxim], rmq[nmaxim][logaritmi], lg[nmaxim], q, x, y;

int main() {
    ifstream cin("rmq.in");
    ofstream cout("rmq.out"); 
    cin >> n >> q;
    for (int i = 1; i <= n; ++i) {
        cin >> v[i];
        rmq[i][0] = v[i];
    }
    lg[1] = 0;
    for (int i = 2; i <= n; ++i) lg[i] = lg[i / 2] + 1;
    for (int j = 1; (1 << j) <= n; ++j) {
        int limita = n - (1 << j) + 1;
        for (int i = 1; i <= limita; ++i) {
            rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j-1]);
        }
    }
    for (int i = 1; i <= q; ++i) {
        cin >> x >> y;
        int minim = min(rmq[x][lg[y - x + 1]], rmq[y - (1 << (lg[y - x + 1])) + 1][lg[y - x + 1]]);
        cout << minim << '\n';
    }
    return 0;
}