Cod sursa(job #3036726)

Utilizator begusMihnea begus Data 24 martie 2023 21:42:29
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.69 kb
#include <bits/stdc++.h>
using namespace std;

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

const int NMAX=1e5+1;

int v[NMAX], rmq[NMAX][17];

int main(){
    int n, m;
    fin >> n >> m;
    for(int i=1; i<=n; i++){
        fin >> v[i];
        rmq[i][0]=v[i];
    }

    for(int j=1; j<17; j++){
        for(int i=1; i+(1<<(j-1))-1<=n; i++){
            rmq[i][j]=min(rmq[i][j-1], rmq[i+(1<<(j-1))][j-1]);
        }
    }
    while(m--){
        int x, y;
        fin >> x >> y;
        int size = y-x+1;
        int pow2=0;
        while(1<<(pow2+1)<=size){
            pow2++;
        }
        fout << min(rmq[x][pow2], rmq[y-(1<<pow2)+1][pow2]) << '\n';

    }
}