Cod sursa(job #491144)

Utilizator georgelRector George georgel Data 9 octombrie 2010 22:42:12
Problema Range minimum query Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 1.1 kb
#include <stdio.h>
#include <stdlib.h>
#define loga 20
#define MAX 100001
#define min(a,b) (a < b)? (a) : (b)


int M[loga][MAX],a[MAX],n,m;
void read()
{
    scanf("%d %d",&n,&m);
    int i,j;
    for(i = 0; i < n; i++)
    scanf("%d",&a[i]);

}
void preprocess(int M[loga][MAX],int a[MAX],int n)
{
    int i,j;
    for(i = 0; i < n; i++)
    M[i][0] = i;
    for(j = 1; (1 << j) <= n; j++)
    {
        for(i = 0; i+(1<<j)-1<n;i++)
        if(a[M[i][j-1]] < a[M[i+1<<(j-1)][j-1]])
        M[i][j] = M[i][j-1];
        else
        M[i][j] = M[i + (1 << (j - 1))][j - 1];
    }
}
void afis()
{
    freopen("rmq.out","wt",stdout);
    int i,x,y,k,l;
   //    for(i = 0; i < n; i++)
  //  printf("%d\n",a[i]);

    for(i = 0; i < m; i++)
    {
        scanf("%d %d",&x,&y);
        k=0;
        while((1<<k+1) < y-x+1)
        k = k+1;
        l = min(a[M[x][k]],a[M[y-(1<<k)+1][k]]);
       printf("%d\n",l);
            // printf("%d %d",x,y);

    }

}
int main()
{
    freopen("rmq.in","r",stdin);
    read();
    preprocess(M,a,n);
   afis();
    return 0;
}