Cod sursa(job #588277)

Utilizator BlaugranasEnal Gemaledin Blaugranas Data 7 mai 2011 15:36:21
Problema Range minimum query Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include<fstream.h>
#include<stdio.h>
#define N 100001
long n,m,a[N],i,j,b[N][18],k,x,y;
int main()
{ifstream f("rmq.in");
freopen("rmq.out","w",stdout);
f>>n>>m;
for(i=0;i<n;i++)
      f>>a[i];
for(i=0;i<n;i++)
      b[i][0]=i;
for(j=1;(1<<j)<=n;j++)
for(i=0;i+(1<<j)-1<n;i++)
if(a[b[i][j-1]]<a[b[i+(1<<(j-1))][j-1]])
      b[i][j]=b[i][j-1];
else
      b[i][j]=b[i+(1<<(j-1))][j-1];            
for(j=0;j<m;j++)
      {f>>x>>y;
      x--;
      y--;
      for(k=1;(1<<k)<=y-x+1;k++);
      k--;
      if(a[b[x][k]]<a[b[y-(1<<k)+1][k]])
              printf("%ld\n",a[b[x][k]]);
      else
              printf("%ld\n",a[b[y-(1<<k)+1][k]]);}
f.close();
fclose(stdout);
return 0;}