Pagini recente » Cod sursa (job #1714099) | Cod sursa (job #2021001) | Cod sursa (job #1867241) | Cod sursa (job #2281168) | Cod sursa (job #2891882)
#include <iostream>
#include <math.h>
#include <fstream>
using namespace std;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
int main()
{
int v[1000],n,m,minima[100][100];
fin>>n>>m;
for(int i = 1; i <= n; i++){
fin>>v[i-1];
minima[0][i-1] = v[i-1];
}
///RMQ
for(int i = 1; (1<<i) <= n; i++){
for(int j = 0; j <= n-(1<<(i-1)); j++){
minima[i][j] = min(minima[i-1][j], minima[i-1][j+(1<<(i-1))]);
}
}
/*
for(int i = 0; i<n;i++){
cout<<minima[0][i]<<" ";
}
cout<<endl;
for(int i = 1; i < log2(n); i++){
for(int j = 0; j+(1<<(i-1)) <= n; j++){
cout<<minima[i][j]<<" ";
}
cout<<endl;
}*/
///Rezolvarea query-urilor - O(1)
for(int i = 1; i <= m; i++){
int x,y;
fin>>x>>y;
int aux = log2(y-x+1);
///cout<<x<<" "<<y<<" "<<aux<<" "<<min(minima[aux][x], minima[aux][y-(1<<aux)])<<endl;
fout<<min(minima[aux][x], minima[aux][y-(1<<aux)])<<endl;
}
fin.close();
fout.close();
return 0;
}