Cod sursa(job #2765252)

Utilizator Teodor_AxinteAxinte Teodor-Ionut Teodor_Axinte Data 26 iulie 2021 00:19:41
Problema Range minimum query Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <fstream>
#include <iostream>


using namespace std;

ifstream fin("rmq.in");
ofstream fout("rmq.out");

const int oo=INT32_MAX;
const int N=100010;

int n,m,o,a,b;
int input[N],segTree[4*N+10];

void constructTree(int input[],int segTree[],int low,int high,int pos)
{
    if(low==high)
    {
        segTree[pos]=input[low];
        return;
    }
    int mid=(low+high)/2;
    constructTree(input,segTree,low,mid,2*pos+1);
    constructTree(input,segTree,mid+1,high,2*pos+2);

    segTree[pos]=min(segTree[2*pos+1],segTree[2*pos+2]);
}

int rangeMinQuery(int segTree[], int Qlow, int Qhigh,int low,int high,int pos)
{
    if(Qlow<=low && Qhigh>=high) //total overlap
        return segTree[pos];

    if(Qlow>high || Qhigh<low) //no overlap
        return oo;

    //double overlap

    int mid = (low+high)/2;
    int fiu_st= rangeMinQuery(segTree,Qlow,Qhigh,low,mid,2*pos+1);
    int fiu_dr= rangeMinQuery(segTree,Qlow,Qhigh,mid+1,high,2*pos+2);
    return min(fiu_st,fiu_dr);
}

int main()
{
    fin>>n>>m;
    for(int i=0;i<n;i++)
        fin>>input[i];
    for(int i=0;i<4*n;i++)
        segTree[i]=oo;

    constructTree(input,segTree,0,n-1,0);

    for(;m!=0;m--)
    {
        fin>>a>>b;
        fout<<rangeMinQuery(segTree,a-1,b-1,0,n-1,0)<<'\n';
    }
    return 0;
}