Cod sursa(job #2623201)

Utilizator drknss_Hehe hehe drknss_ Data 2 iunie 2020 19:06:51
Problema Range minimum query Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.28 kb
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rc(s) return cout<<s,0
#define pi pair <int, int>
#define sz(x) (int)((x).size())
#define int long long

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const ll inf = 0x3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
const int N = 2e5 + 11;
const int INF = 2e9;
const ll INF64 = 3e18 + 1;
const double lil = 0.0000000000001;

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

int n, m, rmq[(int)log2(N) + 5][N];

int mn(int l, int r){

    int row = (int)log2(r - l + 1);

    return min(rmq[row][l], rmq[row][r - row + 1]);
}

void solve(){

    in >> n >> m;

    for(int i = 1; i <= n; i++)in >> rmq[1][i];

    for(int i = 2; i <= (int)log2(n); i++){
        for(int j = 1; j <= n - i + 1; j++){
            rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j + i - 1]);
        }
    }

    while(m--){
        int x, y;

        in >> x >> y;

        out << mn(x,y) << '\n';
    }

}


int32_t main(){
ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();

    int t = 1;

    //cin >> t;

    while(t--){

        solve();

    }

}