Pagini recente » Cod sursa (job #253894) | Cod sursa (job #198106) | Cod sursa (job #64924) | Cod sursa (job #632095) | Cod sursa (job #1744084)
#include <fstream>
#include <cmath>
#define infile "rmq.in"
#define outfile "rmq.out"
using namespace std;
ifstream in(infile);
ofstream out(outfile);
int n, m;
int x, y;
int a[100005];
int mat[100005][20];
int minIndex(int x, int y)
{
int k = log2(y-x+1);
if(a[mat[x][k]] <= a[mat[y - (1<<k) + 1][k]]){
return a[mat[x][k]];
}else{
return a[mat[y - (1<<k) + 1][k]];
}
}
int main()
{
in >> n >> m;
for(int i=0; i<n; i++){
in >> a[i];
mat[i][0] = i;
}
for(int j=1; 1<<j <= n; j++){
for(int i=0; i + (1<<j) - 1 < n; i++){
if(a[mat[i][j-1]] < a[mat[i+(1<<j-1)][j-1]]){
mat[i][j] = mat[i][j-1];
}else{
mat[i][j] = mat[i+(1<<j-1)][j-1];
}
}
}
for(int i=0; i<m; i++){
in >> x >> y;
out << minIndex(x-1, y-1) << '\n';
}
return 0;
}