Cod sursa(job #2998782)

Utilizator bogdann31Nicolaev Bogdan bogdann31 Data 9 martie 2023 23:04:55
Problema Range minimum query Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.54 kb
#include <bits/stdc++.h>
using namespace std;
#define mod                1000000007
#define ll                 long long 
#define all(v)             v.begin(), v.end()
#define fr(n)              for(ll i=0;i<n;++i)
#define ctz(x)             __builtin_ctzll(x)
#define clz(x)             __builtin_clzll(x)
#define pcount(x)          __builtin_popcountll(x)
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
#define cin fin
#define cout fout
ifstream fin("rmq.in");
ofstream fout("rmq.out");
//const ll maxn = 3e5 + 5;
//int f[maxn],nf[maxn],inv[maxn];
//const int M=998244353;
//void init(){
//inv[1]=1; for (int i=2;i<maxn;i++) inv[i]=M-1ll*(M/i)*inv[M%i]%M;
//f[0]=nf[0]=1; for (int i=1;i<maxn;i++) f[i]=1ll*f[i-1]*i%M,nf[i]=1ll*nf[i-1]*inv[i]%M;
//}
//int C(int x,int y){return 1ll*f[x]*nf[y]%M*nf[x-y]%M;}



void solve(){
    ll n, m;cin>>n>>m;
    ll temp=log2(n);
    ll dp[temp+1][n];
    fr(n){
        cin>>dp[0][i];
    }
    for(ll i=1; i<=temp; i++){
        ll p=1<<(i-1);
        for(ll j=0; j<n; j++){
            if(j+p*2-1<n){
                dp[i][j]=min(dp[i-1][j], dp[i-1][j+p]);
            }
            
        }
    }
    for(ll i=0; i<=temp; i++){
        for(ll j=0; j<n; j++){
            cout<<dp[i][j]<<" ";
        }
        cout<<"\n";
    }
    while(m--){
        ll a, b;cin>>a>>b;
        ll dif=log2(b-a+1);
        a--; b--;
        cout<<min(dp[dif][a], dp[dif][b-(1<<dif)+1]);
    }

    
}


int main(){
//    ios_base::sync_with_stdio(false); cin.tie(NULL);
//    ll t;cin>>t;while(t--){solve();cout<<endl;}
    solve();
}