Pagini recente » Cod sursa (job #1441402) | Cod sursa (job #671786) | Cod sursa (job #7822) | Cod sursa (job #863823) | Cod sursa (job #3222739)
#pragma GCC target("sse4.2")
#include <fstream>
using namespace std;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
const int Nmax=1e5+5;
int a[Nmax];
int dp[20][Nmax];
int lg(int x) {return 31-__builtin_clz(x);}
int main()
{
ios_base::sync_with_stdio(0);
fin.tie(0),fout.tie(0);
int n,m;
fin>>n>>m;
for(int i=1;i<=n;i++) fin>>a[i];
int k=lg(n);
for(int i=1;i<=n;i++) dp[0][i]=a[i];
for(int i=1;i<=k;i++)
{
for(int j=1;j+(1<<i)-1<=n;j++)
{
dp[i][j]=min(dp[i-1][j],dp[i-1][j+(1<<(i-1))]);
}
}
for(int i=1;i<=m;i++)
{
int l,r;
fin>>l>>r;
int len=lg(r-l+1);
fout<<min(dp[len][l],dp[len][r-(1<<len)+1])<<'\n';
}
return 0;
}