Cod sursa(job #1789471)

Utilizator GeanaVladGeana Vlad GeanaVlad Data 27 octombrie 2016 00:43:48
Problema Range minimum query Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <iostream>
#include<fstream>
#define inf 100000000000
using namespace std;
ifstream f("rmq.in");
ofstream g("rmq.out");
int v[100001],n,m,i,x,y,tree[400004];
void constructTree(int low,int high,int poz)
{
    if(low==high)
    {
        tree[poz]=v[low];
        return;
    }
    int mid=(low+high)/2;

    constructTree(low,mid,2*poz);
    constructTree(mid+1,high,2*poz+1);

    tree[poz]=min(tree[2*poz],tree[2*poz+1]);
}
int RMQ(int x,int y,int low,int high,int poz)
{
    if(x<=low && y>=high)
        return tree[poz];
    if(x>high || y<low)
        return inf;
    int mid=(low+high)/2;
    return min(RMQ(x,y,low,mid,2*poz),RMQ(x,y,mid+1,high,2*poz+1));
}
int main()
{
    f>>n>>m;
    for(i=1; i<=n; i++)
        f>>v[i];
    constructTree(1,n,1);
    for(i=1; i<=m; i++)
    {
        f>>x>>y;
        g<<RMQ(x,y,1,n,1)<<'\n' ;
    }
}