Pagini recente » Cod sursa (job #2061633) | Cod sursa (job #578825) | Cod sursa (job #2268223) | Cod sursa (job #233677) | Cod sursa (job #2405568)
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
int N, List[100001], i, M[320], mn, sq, V, P, b, ind;
int main()
{
fin>>N>>P;
sq=sqrt(N); mn=100001; b=sq;
for(i=1; i<=N; ++i){
fin>>List[i];
if(i>sq) {M[++V]=ind; mn=100001; sq+=b;}
if(List[i]<mn){
mn=List[i]; ind=i;
}
}
if(N!=sq) M[++V]=ind;
for(i=1; i<=P; ++i){
int x, y, sol=100001;
fin>>x>>y;
for(b=x; b<=(x/sq+1)*sq && b<=y; ++b) sol=min(sol, List[b]);
if(y/sq-x/sq>1) for(b=x/sq+1; b<y/sq; ++b) sol=min(sol, List[M[b]]);
for(b=y; b>=(y/sq)*sq && b>=x; --b) sol=min(sol, List[b]);
fout<<sol<<"\n";
}
return 0;
}