Cod sursa(job #2291749)

Utilizator denmirceaBrasoveanu Mircea denmircea Data 28 noiembrie 2018 16:25:14
Problema Range minimum query Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.59 kb
#include <fstream>
using namespace std;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
int n,m,L[100002],i,j,i2,minn,x,y,p;
int a[100002][100];
int main(){
fin>>n>>m;
L[1]=0;
for(i=2;i<=n;i++)
      L[i]=L[i/2]+1;
for(i=1;i<=n;i++)
      fin>>a[0][i];
for(i=1;i<=L[n]+1;i++){
      for(j=1;j<=n;j++){
            minn=a[i-1][j];
            x=j+(1<<(i-1));
            if(x<=n)
                  minn=min(minn,a[i-1][x]);
            a[i][j]=minn;
      }
}
for(i=1;i<=m;i++){
      fin>>x>>y;
      p=L[y-x+1];
      fout<<min(a[p][x],a[p][y-(1<<p)+1])<<"\n";
}

}