Pagini recente » Cod sursa (job #2421620) | Cod sursa (job #1140724) | Diferente pentru problema/minmaxstore intre reviziile 4 si 3 | Monitorul de evaluare | Cod sursa (job #1167430)
#include<cstdio>
#include<algorithm>
using namespace std;
const int NMAX = 100000+5;
const int LMAX = 18;
void Read(),Build(),Solve();
int N,M;
int RMQ[LMAX][NMAX];
int Query(int x,int y)
{
int dist,step,p;
dist = y - x + 1;
for(step = 0; (1<<step) <= dist; step++);
step--;
p = 1<<step;
return min(RMQ[step][x], RMQ[step][y-p+1]);
}
int main()
{
Read();
Build();
Solve();
return 0;
}
void Read()
{
int i;
freopen("rmq.in","r",stdin);
freopen("rmq.out","w",stdout);
scanf("%d%d",&N,&M);
for(i = 1; i <= N; i++)
scanf("%d",&RMQ[0][i]);
}
void Build()
{
int i,step;
for(step = 1; (1<<step) <= N; step++)
for(i = 1; i <= N - step + 1; i++)
RMQ[step][i] = min(RMQ[step-1][i], RMQ[step-1][i+(1<<(step-1))]);
}
void Solve()
{
int x,y;
for(; M; --M)
{
scanf("%d%d",&x,&y);
printf("%d\n",Query(x,y));
}
}