Pagini recente » Cod sursa (job #2269781) | Cod sursa (job #1116101) | Cod sursa (job #2518601) | Cod sursa (job #264140) | Cod sursa (job #1537644)
#include <iostream>
#include <fstream>
#define nmax 100005
using namespace std;
ifstream f("rmq.in");
ofstream g("rmq.out");
int a[20][nmax],pow[nmax], n, m, x, y;
void generare()
{
for(int i=1; i<=n; i++)
f >> a[0][i];
for(int i=1; (1<<i)<=n; i++)
for(int j=1; j+(1<<i)-1 <=n; j++)
a[i][j] = min(a[i-1][j], a[i-1][j+(1<<(i-1))]);
pow[1]=0;
for(int i=2; i<=nmax; i++)
if (i % 2 == 0) pow[i]=pow[i-1]+1;
else pow[i]=pow[i-1];
return;
}
int querry(int x, int y)
{
int h = (y - x -1);
h = pow[h];
return min(a[h][x], a[h][y-(1<<h)+1]);
}
int main()
{
f >> n >> m;
generare();
for(int i=1; i<=m; i++)
{
f >> x >> y;
g << querry(x, y) << "\n";
}
f.close();
g.close();
return 0;
}