Pagini recente » Cod sursa (job #1438273) | Cod sursa (job #2184786) | Infoarena Monthly 2012 - Runda 9, Clasament | Cod sursa (job #2573326) | Cod sursa (job #2725577)
#include <fstream>
using namespace std;
const int NMAX = 100000;
const short LGMAX = 23;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
int rmq[NMAX + 5][LGMAX + 2];
int logx[NMAX + 5];
void constrLog()
{
int i;
for(i = 2; i <= NMAX; i++)
logx[i] = logx[(i >> 1)] + 1;
}
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(NULL);
constrLog();
int n,m,i,j,k,st,dr,x;
fin >> n >> m;
for(i = 1; i <= n; i++)
{
fin >> x;
rmq[i][0] = x;
}
k = logx[n];
for(j = 1; j <= k; j++)
for(i = 1; i + (1 << j) <= n + 1; i++)
rmq[i][j] = min(rmq[i][j - 1], rmq[i + (1 << (j - 1))][j - 1]);
while(m--)
{
fin >> st >> dr;
j = logx[dr - st + 1];
fout << min(rmq[st][j], rmq[dr - (1 << j) + 1][j]) << "\n";
}
fin.close();
fout.close();
return 0;
}