Pagini recente » Monitorul de evaluare | Cod sursa (job #4806) | Cod sursa (job #2858963) | Cod sursa (job #591736) | Cod sursa (job #1710990)
#include<cstdio>
#include <algorithm>
using namespace std;
FILE *in,*out;
const int N = 100010;
int rmq[20][N],p[N];
int main()
{
int n,q;
in = fopen("rmq.in","r");
out = fopen("rmq.out","w");
fscanf(in,"%d%d",&n,&q);
int i,j;
for (i = 1;i <= n;++i)
fscanf(in,"%d",&rmq[0][i]);
for (i = 2;i <= n;++i)
p[i] = 1 + p[i / 2];
for (i = 1;i <= p[n];++i)
for (int j = 1;(j + (1 << i) - 1) <= n;++j)
rmq[i][j] = min(rmq[i - 1][j], rmq[i - 1][j + (1<<(i-1))]);
for (int i = 1;i <= q;++i)
{
int x, y;
fscanf(in,"%d%d",&x,&y);
fprintf(out,"%d\n",min(rmq[p[y - x + 1]][x], rmq[p[y - x + 1]][y - (1 << p[y - x + 1]) + 1]));
}
return 0;
}