Cod sursa(job #2430920)

Utilizator bluestorm57Vasile T bluestorm57 Data 17 iunie 2019 11:50:37
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.64 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 1e5 + 5;
int mat[NMAX][20];

int main(){
    int n,m,x,y,k,dif,p,i,j;
    f >> n >> m;
    for(i = 0 ; i < n ; i++)
        f >> mat[i][0];
    for(j = 1 ; (1 << j) <= n ; j++)
        for(i = 0 ; i + (1 << j)  <= n ; i++)
            mat[i][j] = min(mat[i][j-1], mat[i + (1 << (j - 1))][j - 1] );

    for(i = 1 ; i <= m ; i++){
        f >> x >> y;
        dif = y - x + 1;
        k = log2(dif);
        p = dif - (1 << k);
        x--;
        g << min(mat[x][k], mat[x+p][k]) << "\n";
    }

    return 0;
}