Cod sursa(job #3161222)

Utilizator Dia3141Costea Diana Stefania Dia3141 Data 26 octombrie 2023 09:31:47
Problema Range minimum query Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.65 kb
#include <fstream>
using namespace std;
ifstream cin("rmq.in");
ofstream cout("rmq.out");
int r[20][100001],n,i,m,x,y,e[100001];
int main()
{
    cin>>n>>m;
    for(i=1;i<=n;i++)
        cin>>r[0][i];
    for(int p=1;(1<<p)<=n;p++)
        for(i=1;i<=n;i++)
        {
            r[p][i]=r[p-1][i];
            int j=i+(1<<(p-1));
            if(j<=n&&r[p][i]>r[p-1][j])
                r[p][i]=r[p-1][j];
        }
    e[1]=0;
    for(i=2;i<=n;i++)
        e[i]=1+e[i/2];
    for(i=1;i<=m;i++)
    {
        cin>>x>>y;
        int exp=e[x-y+1],len=(1<<exp);
        cout<<min(r[exp][x],r[exp][y-len+1])<<'\n';
    }
    return 0;
}