Pagini recente » Cod sursa (job #1906599) | Cod sursa (job #831084) | Cod sursa (job #2274101) | Cod sursa (job #445124) | Cod sursa (job #1046535)
#include <iostream>
#include<algorithm>
#include<fstream>
#include<math.h>
using namespace std;
int rmq[100000][20];
int main()
{ int n,i,e,m,a,b,max;
bool gasit;
ifstream f("rmq.in");
ofstream g("rmq.out");
f >> n >> m;
for ( i=0;i<n;++i)
{
f>>rmq[i][0];
}
for ( e=1;(1<<e)<n;++e)
for (i=0;i<n;++i)
{
rmq[i][e]=rmq[i][e-1];
if (i+(1<<(e-1))<n && rmq[i][e]>rmq[i+(1<<(e-1))][e-1])
rmq[i][e]=rmq[i+(1<<(e-1))][e-1];
}
for (i=1;i<=m;i++)
{
f >> a >> b;
--a;--b;
gasit=false;
if (a>b)
swap(a,b);
else
if (a==b)
{
cout << rmq[a][0] << "\n";
gasit = true;
}
int max=log2(a-b+1);
if (!gasit)
{
if (rmq[a][max] < rmq[b-(1<<max)+1][max])
cout << rmq[a][max] << "\n";
else cout << rmq[b-(1<<max)+1][max] << "\n";
}
}
return 0;
}