Cod sursa(job #2625575)

Utilizator ioanapintilie07Pintilie Ioana ioanapintilie07 Data 6 iunie 2020 00:55:20
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

long int rmq[18][100002], a[100002];

using namespace std;

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

    long int i, j, l, n, m, x, y, d;
    fin >> n >> m;
    for (i = 1; i <= n; i++)
        fin >> a[i];

    for (i = 1; i <= n; i++)
        rmq[0][i] = a[i];

    for (i = 1; (1 << i) <= n; i++)
        for (j = 1; j <= n - (1 << i) + 1; j++) {
            l = 1 << (i - 1);
            rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j + l]);
        }

    for (i = 1; i <= m; i++) {
        fin >> x >> y;
        d = y - x + 1;
        l = (int)log2(d);
        d = d - (1 << l);
        fout << min(rmq[l][x], rmq[l][x + d]) << "\n";
    }
    return 0;
}