Pagini recente » Cod sursa (job #3199887) | Cod sursa (job #1176635) | Cod sursa (job #3267329) | Cod sursa (job #373026) | Cod sursa (job #3294797)
/******************************************************************************
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;
void preproc_rmq(){
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(int x, int y){
int k = log2(y - x + 1);
return min(rmq[x][k], rmq[y-(1<<k)+1][k]); //1...8
}
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;
}