Mai intai trebuie sa te autentifici.

Cod sursa(job #3293479)

Utilizator mihaigeorgescuGeorgescu Mihai mihaigeorgescu Data 11 aprilie 2025 19:55:16
Problema Range minimum query Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>

using namespace std;
ifstream fcin("rmq.in");
ofstream fout("rmq.out");
int n,q,v[100001],x,y;
struct sparse_table
{
    int spt[20][100101];
    int lg[100101];
    inline void build()
    {
        for(int i=2; i<=n; i++)
            lg[i]=1+lg[i>>1];
        for(int i=1; i<=n; i++)
            spt[0][i]=v[i];
        for(int i=1; i<=lg[n]; i++)
            for(int j=1; j<=n-(1<<i)+1; j++)
                spt[i][j]=min(spt[i-1][j], spt[i-1][j+(1<<(i-1))]);
    }
    inline void query(int st, int dr)
    {
        int lg2=lg[dr-st+1];
        fout<<min(spt[lg2][st],spt[lg2][dr-(1<<lg2)+1])<<'\n';
    }
};
sparse_table rmq;
int main()
{
    fcin>>n>>q;
    for(int i=1; i<=n; i++)
        fcin>>v[i];
    rmq.build();
    while(q--)
    {
        fcin>>x>>y;
        rmq.query(x,y);
    }
    return 0;
}