Cod sursa(job #1266845)

Utilizator gabimoiseMoise Gabriel gabimoise Data 19 noiembrie 2014 08:51:52
Problema Range minimum query Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.77 kb
#include <iostream>
#include <cstdio>

using namespace std;

long int i,n,j,v[100005],x[100005],rmq[20][100005],a,b,dif,s,m,l;
int main()
{
    freopen("rmq.in","r",stdin);
    freopen("rmq.out","w",stdout);
    scanf("%ld%ld",&n,&m);
    for (i=1;i<=n;i++) scanf("%ld",&v[i]);
    x[1]=0;
    for (i=2;i<=n;i++) x[i]=x[i/2]+1;
    for (i=1;i<=n;i++) rmq[0][i]=v[i];
    for (i=1;(i<<i)<=n;i++)
      for (j=1;j<=n-(1<<i)+1;j++)
        l=1<<(i-1);
        rmq[i][j]=min(rmq[i-1][j],rmq[i-1][j+1]);
    for (i=1;i<=m;i++)
    {
        scanf("%ld%ld",&a,&b);
        dif=b-a+1;
        l=x[dif];
        s=dif-(1<<l);
        if (rmq[l][a]>rmq[l][a+s])
        printf("%ld\n",rmq[l][a+s]);
            else printf("%ld\n",rmq[l][a]);
    }
    return 0;
}