Cod sursa(job #2903766)

Utilizator Cris.CristinaPopescu Cristina Cris.Cristina Data 17 mai 2022 20:24:17
Problema Range minimum query Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.56 kb
#include <fstream>
#include <algorithm>
#define NRN 100005
using namespace std;
ifstream f("rmq.in");
ofstream g("rmq.out");
int rmq[20][NRN],n,m,l[NRN],i,j,x,y;
int main()
{
    f>>n>>m;
    for(i=1;i<=n;++i)
        f>>rmq[0][i];
    for(i=2;i<=n;++i)
        l[i]=l[i/2]+1;
    for(i=1;i<=l[n];++i)
        for(j=1;j<=n-(1<<(i-1));j++)
            rmq[i][j]=min(rmq[i-1][j], rmq[i-1][j+(1<<(i-1))]);

     for (i=1; i<=m; ++i)
    {
        f>>x>>y;
        int k=l[y-x+1];
        g<<min(rmq[k][x], rmq[k][y-(1<<k)+1])<<endl;
    }
    return 0;
}