Pagini recente » Cod sursa (job #3236087) | Cod sursa (job #2687085) | Istoria paginii preoni-2008/runda-4/10 | Cod sursa (job #239883) | Cod sursa (job #3294799)
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <fstream>
#include<cmath>
using namespace std;
const int NMAX = 1e5;
int rmq[NMAX+1][20],n,m, Log2[NMAX+1];
void preproc_rmq(){
Log2[1]=0; //nu ex 0
for(int i=2;i<=NMAX;i++) Log2[i]=Log2[i/2]+1;
for(int j=1;(1<<j)<=n;j++) //dupa j crescator
for(int i=0;i+(1<<j)-1<n;i++) //pentru orice i cu 1+(2**j)-1<n
rmq[i][j]=min(rmq[i][j-1], rmq[i+(1<<(j-1))][j-1]); //1+2**j*/
}
int query_id(int x, int y){
int k = Log2[y - x + 1];
return min(rmq[x][k], rmq[y-(1<<k)+1][k]); //1...8
}
int query(int x, int y){
int dif = y - x + 1;
int r=rmq[x][0];
int j=x;
for(int k=0; (1<<k)<=dif; k++)
if((1<<k)&dif){
r=min(r, rmq[j][k]); //1...8
j+=(1<<k);
}
return r;
}
int main()
{
ifstream f("rmq.in");
ofstream g("rmq.out");
int x,y;
f>>n>>m;
for(int i=0;i<n;i++)
f>>rmq[i][0]; //a[i]
preproc_rmq();
for(int i=0;i<m;i++){
f>>x>>y;
g<<query(x-1,y-1)<<endl; //de la 0 indicii
}
f.close();
g.close();
return 0;
}