Cod sursa(job #2617939)

Utilizator OvidRata Ovidiu Ovid Data 23 mai 2020 13:08:52
Problema Range minimum query Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();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
ifstream fin("rmq.in"); ofstream fout("rmq.out");
#define cin fin
#define cout fout
#define int ll
int t, n, m, k, a[300010], q, l;
int mn[40][100010];






int32_t main(){
INIT
cin>>n>>q;

for(int i=1; i<=n; i++){
    cin>>a[i];
}
for(int i=0; (1<<i)<=n; i++ ){
    for(int j=1; j+(1<<i)-1<=n; j++  ){
            mn[i][j]=min( (i==0)?a[j]:mn[i-1][j], (i==0)?a[j]:mn[i-1][j+(1<<(i-1) )]    );
    }

}










for(;q;q--){
    int x, y;
    cin>>x>>y;
    int d=y-x;
    int l, r, m;
    l=0; r=32;
    while(l<r){
    m=(l+r)/2;

    if((1<<m)>=d){
        r=m;
    }
    else{
        l=m+1;
    }

    }

    int k=(l+r)/2;

    while( (1<<k)>d ){
        k--;
    }

    cout<<min(mn[k][x], mn[k][y-(1<<k)+1])<<"\n";

}





return 0;
}