Pagini recente » Borderou de evaluare (job #1460904) | Borderou de evaluare (job #1020753) | Borderou de evaluare (job #2685714) | Borderou de evaluare (job #2685718) | Cod sursa (job #3161222)
#include <fstream>
using namespace std;
ifstream cin("rmq.in");
ofstream cout("rmq.out");
int r[20][100001],n,i,m,x,y,e[100001];
int main()
{
cin>>n>>m;
for(i=1;i<=n;i++)
cin>>r[0][i];
for(int p=1;(1<<p)<=n;p++)
for(i=1;i<=n;i++)
{
r[p][i]=r[p-1][i];
int j=i+(1<<(p-1));
if(j<=n&&r[p][i]>r[p-1][j])
r[p][i]=r[p-1][j];
}
e[1]=0;
for(i=2;i<=n;i++)
e[i]=1+e[i/2];
for(i=1;i<=m;i++)
{
cin>>x>>y;
int exp=e[x-y+1],len=(1<<exp);
cout<<min(r[exp][x],r[exp][y-len+1])<<'\n';
}
return 0;
}