Cod sursa(job #3240543)

Utilizator EricRaiaEricRaia EricRaia Data 16 august 2024 12:37:59
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>

using namespace std;

ifstream cin ("rmq.in");
ofstream cout ("rmq.out");

int n,m,st,dr,e,Log[100002],rmq[17][100002];
int main()
{
    cin>>n>>m;
    Log[0]=-1;
    for(int i=1;i<=n;i++){
        cin>>rmq[0][i];
        Log[i]=1+Log[i>>1];
    }
    for(int p=1;(1<<p)<=n;p++){
        for(int i=1;i<=n;i++){
            rmq[p][i]=rmq[p-1][i];
            e=i+(1<<(p-1));
            if(e<=n && rmq[p-1][e]<rmq[p][i])
                rmq[p][i]=rmq[p-1][e];
        }
    }
    for(int i=1;i<=m;i++){
        cin>>st>>dr;
        e=Log[dr-st+1];
        cout<<min(rmq[e][st], rmq[e][dr-(1<<e)+1])<< '\n';
    }

    return 0;
}