Pagini recente » Cod sursa (job #729798) | Cod sursa (job #431102) | Cod sursa (job #1258622) | Cod sursa (job #32647) | Cod sursa (job #2118205)
#include <bits/stdc++.h>
#define Nmax 100005
#define DIM 70000
#define ls (poz<<1)
#define rs ((poz<<1)+1)
using namespace std;
char buffer[DIM];
int poz=DIM-1;
void read(int &x)
{
x=0;
while(!isdigit(buffer[poz]))
if(++poz==DIM)
{
poz=0;
fread(buffer,1,DIM,stdin);
}
while(isdigit(buffer[poz]))
{
x=x*10+buffer[poz]-'0';
if(++poz==DIM)
{
poz=0;
fread(buffer,1,DIM,stdin);
}
}
}
int arb[Nmax*20];
int v[Nmax];
int lsh,rsh;
void build(int p, int q, int poz)
{
if(p==q)
arb[poz]=v[p];
else
{
int m=(p+q)>>1;
build(p,m,ls);
build(m+1,q,rs);
arb[poz]=min(arb[ls],arb[rs]);
}
}
int query(int p, int q, int poz)
{
if(lsh<=p and q<=rsh) return arb[poz];
int m=(p+q)>>1,m1=INT_MAX,m2=INT_MAX;
if(lsh<=m) m1=query(p,m,ls);
if(m<rsh) m2=query(m+1,q,rs);
return min(m1,m2);
}
int main()
{
freopen("rmq.in","r",stdin);
freopen("rmq.out","w",stdout);
int n,i,j,m;
read(n);
read(m);
for(i=1;i<=n;i++)
read(v[i]);
build(1,n,1);
while(m--)
{
read(lsh);
read(rsh);
printf("%d\n",query(1,n,1));
}
return 0;
}