Pagini recente » Cod sursa (job #844108) | Cod sursa (job #2553327) | Cod sursa (job #2623924) | Cod sursa (job #2888477) | Cod sursa (job #3310706)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
const int NMAX = 100000 + 2;
int n, m;
int mat[18][NMAX];
int main()
{
fin >> n >> m;
for(int i = 1; i <= n; i++)
{
fin >> mat[0][i];
//fout << mat[0][i] << " ";
}
//fout << endl;
for(int i = 1; i <= 16; i++)
{
for(int j = 1; j <= n - (1 << i) + 1; j++)
{
mat[i][j] = min(mat[i - 1][j], mat[i - 1][j + (1 << (i - 1))]);
//fout << mat[i][j] << " ";
}
//fout << endl;
}
while(m--)
{
int l, r;
fin >> l >> r;
int putere = log2l(r - l + 1);
fout << min(mat[putere][l], mat[putere][r - (1 << putere) + 1]) << "\n";
}
return 0;
}