Pagini recente » Cod sursa (job #355420) | Cod sursa (job #2805323) | Cod sursa (job #48002) | Cod sursa (job #1390607) | Cod sursa (job #1659640)
#include <fstream>
#include <iostream>
using namespace std;
int rmq [100002][18];
int lg[100002];
int main ()
{
ifstream cin ("rmq.in");
ofstream cout ("rmq.out");
int n,d;
cin>>n>>d;
lg[1]=0;
for (int i=2;i<=n;++i)
{
lg[i]=lg[i/2]+1;
}
for (int i=0;i<n;++i)
{
cin>>rmq[i][0];
}
for (int i=1;i<=lg[n];++i)
{
for (int j=0;j+(1<<(i))-1<=n;++j)
{
rmq[j][i]=min (rmq[j][i-1] , rmq[j+(1<<(i-1))][i-1]);
}
}
for (int i=0;i<d;++i)
{
int x,y;
cin>>x>>y;
int diff=y-x+1;
cout<<min(rmq[x][lg[diff]] , rmq[y-(1<<lg[diff])+1][lg[diff]])<<"\n";
}
}