Cod sursa(job #3155165)

Utilizator mhyasdArdelean Mihai mhyasd Data 7 octombrie 2023 15:22:58
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <fstream>
#include <bits/stdc++.h>

using namespace std;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
int st[100000][17];
int logg(int n) {
    int cnt=0;
    while(n!=0) {
        n/=2;
        cnt++;
    }
    return cnt-1;
}

int main()
{
    int n,m,k,a,b,len,p;
    fin>>n>>m;
    for(int i=0;i<n;i++) {
        fin>>st[i][0];
    }
    k=logg(n);
    for(int j=1;j<=k;j++) {
        for(int i=0;i<n-(1<<j)+1;i++) {
            st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
        }
    }
    for(int i=0;i<m;i++) {
        fin>>a>>b;
        a--;
        b--;
        len=b-a+1;
        k=logg(len);
        fout<<min(st[a][k],st[a+len-(1<<k)][k])<<endl;
    }

    return 0;
}