Cod sursa(job #2755358)

Utilizator FraNNNkieFrancesco-Gregorio Petrovici FraNNNkie Data 27 mai 2021 00:05:56
Problema Range minimum query Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>
#define nmax 100005
using namespace std;
int v[nmax], rmq[18][nmax], logaritmi[nmax];
int n, m;
int main()
{
    ifstream f("rmq.in");
    ofstream g("rmq.out");

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