Cod sursa(job #2755370)

Utilizator FraNNNkieFrancesco-Gregorio Petrovici FraNNNkie Data 27 mai 2021 00:20:17
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>
using namespace std;


int n, m;
int rmq[17][100001];
int logaritmi[100001];

int main()
{
    ifstream f("rmq.in");
    ofstream g("rmq.out");
    f >> n >> m;
    logaritmi[1] = 0;

    for(int i = 1; i <= n; i++){
        f >> rmq[0][i];
        if(i > 1){
            logaritmi[i] = logaritmi[i / 2] + 1;
        }
    }

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

    int x, a, b;
    for(int i = 1; i <= m; ++i)
    {
        f >> a >> b;
        x = logaritmi[b - a + 1];
        g << min(rmq[x][a + (1 << x) - 1], rmq[x][b]) << "\n";
    }

    return 0;
}