Cod sursa(job #2284670)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 17 noiembrie 2018 12:29:34
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.7 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMax = 1e5 + 5;
const int logNMax = 17;

int RMQ[logNMax][NMax];

int main() {

    ios::sync_with_stdio(false);

    int n, t;
    fin >> n >> t;

    for(int i = 1; i <= n; i++) fin >> RMQ[0][i];

    for(int i = 1; i <= log2(n); i++) {
        for(int j = 1; j + (1 << i) - 1 <= n; j++) {
            RMQ[i][j] = min(RMQ[i - 1][j], RMQ[i - 1][j + (1 << (i - 1))]);
        }
    }

    while(t--) {
        int x, y;
        fin >> x >> y;

        int k = log2(y - x + 1);

        fout << min(RMQ[k][x], RMQ[k][y - (1 << k) + 1]) << "\n";
    }
    return 0;
}