Pagini recente » simulare.oji.2024.bv_11_12 | Istoria paginii runda/zbang/clasament | Cod sursa (job #1350634) | Cod sursa (job #2396413) | Cod sursa (job #1168689)
#include<cstdio>
#include<algorithm>
using namespace std;
const int NMAX = 100000+5;
const int LMAX = 17;
void Read(),Build(),Solve();
int N,M;
int RMQ[LMAX][NMAX];
int Query(int x,int y)
{
int dist = y - x + 1,step,p;
for(step = 0, p = 1; p <= dist; step++, p <<= 1);
step--;
p >>= 1;
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,p;
for(step = 1, p = 2; p <= N; step++, p <<= 1)
for(i = 1; i + (p>>1) <= N; i++)
RMQ[step][i] = min(RMQ[step-1][i], RMQ[step-1][i+(p>>1)]);
}
void Solve()
{
int x,y;
for(; M; --M)
{
scanf("%d%d",&x,&y);
printf("%d\n",Query(x,y));
}
}