Cod sursa(job #2984282)

Utilizator tescovschimarioTescovschi Mario tescovschimario Data 23 februarie 2023 20:36:12
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in ("rmq.in");
ofstream out ("rmq.out");
int n, m, x, y, v[100005][17], cnt;

void ask(int x, int y){
    int l = y - x + 1;
    cnt = 0;
    while( (1 << (cnt + 1)) <= l)
        cnt++;

    out << min(v[x][cnt], v[y - (1 << cnt) + 1][cnt]) << '\n';
}

int main() {
    in >> n >> m;
    for(int i = 1; i <= n; i++)
        in >> v[i][0];

    for(int i = 1; i < 17; i++)
        for(int j = 1; j + (1 << i) - 1 <= n; j++)
            v[j][i] = min(v[j][i-1], v[j + (1 << (i - 1))][i-1]);


    while(m--) {
        in >> x >> y;
        ask(x, y);
    }
            return 0;
}