Cod sursa(job #2920250)

Utilizator OvidRata Ovidiu Ovid Data 23 august 2022 11:45:27
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include<bits/stdc++.h>
using namespace std;

string numeFisier="rmq";
ifstream fin(numeFisier+".in"); ofstream fout(numeFisier+".out");
#define cin fin
#define cout fout

#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
//#define int ll


int n, m, a[100010][18];
int lg[100010];


int32_t main(){
INIT

cin>>n>>m;
for(int i=1; i<=n; i++){
    cin>>a[i][0];
}

for(int j=1; j<=17; j++){
    for(int i=1; (i+(1ll<<j)-1)<=n; i++){
        a[i][j]=min(a[i][j-1], a[i+(1ll<<(j-1)) ][j-1]);
    }
}

for(int i=1, pow=0; i<=n; i++){
    if((1<<(pow+1) )<=i){
        pow++;
    }
    lg[i]=pow;
}


for(int i=1, l, r; i<=m; i++){
    cin>>l>>r;
    cout<<min( a[l][ lg[r-l+1] ], a[r-(1ll<<lg[r-l+1])+1 ][lg[r-l+1] ] )<<"\n";
}

return 0;
}