Cod sursa(job #2858163)

Utilizator MR0L3eXMaracine Constantin Razvan MR0L3eX Data 27 februarie 2022 09:29:41
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include "bits/stdc++.h"

using namespace std;

using ld = long double;
using ll = long long;
using ull = unsigned long long;

#if defined(ONPC)
#include "bits/debug.h"
#endif

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

vector<vector<int>> st;
vector<int> v;
int n, LOG;

void bld() {
    for (int k = 1; k < LOG; ++k) {
        for (int i = 0; i + (1 << k) - 1 < n; ++i) {
            st[i][k] = min(st[i][k - 1], st[i + (1 << (k - 1))][k - 1]);
        }
    }
}

int qry(int L, int R) {
    int k = 0, len = R - L + 1;
    while ((1 << (k + 1)) <= len) ++k;
    return min(st[L][k], st[R - (1 << k) + 1][k]);
}

    

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int m;
    fin >> n >> m;
    
    while ((1 << LOG) < n) ++LOG;

    v.resize(n);
    st.resize(n, vector<int>(LOG));
    for (int i = 0; i < n; ++i) {
        fin >> v[i];
        st[i][0] = v[i];
    }
    
    bld();

    while (m--) {
        int a, b;
        fin >> a >> b;
        --a, --b;
        fout << qry(a, b) << "\n";
    }
}