Pagini recente » Cod sursa (job #886704) | Borderou de evaluare (job #996823) | Cod sursa (job #389709) | Cod sursa (job #2704309) | Cod sursa (job #1499011)
#include <cstdio>
#include <algorithm>
using namespace std;
int rmq[50][100010];
int logg[100010];
int minim(int x,int y)
{
int p=logg[y-x+1];
return min(rmq[x][p],rmq[y-(1<<p)+1][p]);
}
int v[100010];
int main()
{
freopen("rmq.in","r",stdin);
freopen("rmq.out","w",stdout);
int n,m,x,y,r;
scanf("%d%d",&n,&m);
for(int i=2;i<=n;i++) logg[i]=logg[i/2]+1;
for(int i=1;i<=n;i++)
{
scanf("%d",&v[i]);
rmq[i][0]=v[i];
}
for(int j=1;j<=logg[n];j++)
for(int i=1;i<=n;i++)
rmq[i][j]=min(rmq[i][j-1],rmq[i+(1<<(j-1))][j-1]);
for(int k=1;k<=m;k++)
{
scanf("%d%d",&x,&y);
r=minim(x,y);
printf("%d\n",r);
}
return 0;
}