Cod sursa(job #2972603)

Utilizator sandry24Grosu Alexandru sandry24 Data 29 ianuarie 2023 20:02:56
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second

void solve(){
    int n, q;
    cin >> n >> q;
    vi a(n+1);
    vector<vi> sparse_table(n+1, vi(25));
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        sparse_table[i][0] = a[i];
    }
    for(int j = 1; j < 25; j++){
        for(int i = 1; i + (1 << j) - 1 <= n; i++){
            sparse_table[i][j] = min(sparse_table[i][j-1], sparse_table[i + (1 << (j-1))][j-1]);
        }
    }
    for(int i = 0; i < q; i++){
        int a, b;
        cin >> a >> b;
        int w = (int)log2(b-a+1);
        cout << min(sparse_table[a][w], sparse_table[b - (1 << w) + 1][w]) << '\n';
    }
}  
 
int main(){
    freopen("rmq.in", "r", stdin);
    freopen("rmq.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
}