Cod sursa(job #2366134)

Utilizator qwerty1234qwerty qwerty1234 Data 4 martie 2019 18:37:57
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb

#include <bits/stdc++.h>


using namespace std;

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

const int Nmax = 1e5 + 5;

int n , Q , P[Nmax] , a[Nmax] , rmq[17][Nmax];

int main()
{
    int x , y , k;
    fin >> n >> Q;
    for(int i = 1 ; i <= n ; i++)
        fin >> a[i];
    for(int i = 2 ; i <= n ; i++)
        P[i] = P[i / 2] + 1;
    for(int i = 1 ; i <= n ; i++)
        rmq[0][i] = a[i];
    for(int i = 1 ; (1 << i) <= n ; i++)
        for(int j = 1 ; j <= n ; j++)
            rmq[i][j] = min(rmq[i - 1][j] , rmq[i - 1][j - (1 << (i - 1))]);
    while(Q--)
    {
        fin >> x >> y;
        k = P[y - x + 1];
        fout << min(rmq[k][y] , rmq[k][x + (1 << k) - 1]) << "\n";
    }
    fin.close();
    fout.close();
}