Cod sursa(job #1935423)

Utilizator DobosDobos Paul Dobos Data 22 martie 2017 12:36:03
Problema Range minimum query Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.74 kb
#include <bits/stdc++.h>
#define NMAX 100005
#define PMAX 20

using namespace std;

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

int M[NMAX][PMAX];

inline void buildrmq(const int &n){

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

}


int main()
{
    ios :: sync_with_stdio(false);

    int n,q,x,y,k;

    fin >> n >> q;

    for(int i = 0; i < n; i++)
        fin >> M[i][0];

    buildrmq(n);

    for(int i = 1; i <= q; i++){
        fin >> x >> y;
        x --; y --;
        k = log2(y - x + 1);

        fout << min(M[x][k],M[y - (1 << k) + 1][k]) << "\n";


    }
    return 0;
}