Cod sursa(job #2725577)

Utilizator cyg_mihaizMIHAI ZARAFIU cyg_mihaiz Data 19 martie 2021 10:59:57
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>

using namespace std;
const int NMAX = 100000;
const short LGMAX = 23;

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

int rmq[NMAX + 5][LGMAX + 2];
int logx[NMAX + 5];

void constrLog()
{
    int i;
    for(i = 2; i <= NMAX; i++)
        logx[i] = logx[(i >> 1)] + 1;
}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(NULL);
    constrLog();

    int n,m,i,j,k,st,dr,x;
    fin >> n >> m;
    for(i = 1; i <= n; i++)
    {
        fin >> x;
        rmq[i][0] = x;
    }
    k = logx[n];
    for(j = 1; j <= k; j++)
        for(i = 1; i + (1 << j) <= n + 1; i++)
            rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
    while(m--)
    {
        fin >> st >> dr;
        j = logx[dr - st + 1];
        fout << min(rmq[st][j], rmq[dr - (1 << j) + 1][j]) << "\n";
    }

    fin.close();
    fout.close();
    return 0;
}