Pagini recente » Cod sursa (job #1201644) | Cod sursa (job #2599703) | Cod sursa (job #66008) | Cod sursa (job #795131) | Cod sursa (job #3155165)
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
ifstream fin("rmq.in");
ofstream fout("rmq.out");
int st[100000][17];
int logg(int n) {
int cnt=0;
while(n!=0) {
n/=2;
cnt++;
}
return cnt-1;
}
int main()
{
int n,m,k,a,b,len,p;
fin>>n>>m;
for(int i=0;i<n;i++) {
fin>>st[i][0];
}
k=logg(n);
for(int j=1;j<=k;j++) {
for(int i=0;i<n-(1<<j)+1;i++) {
st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
}
}
for(int i=0;i<m;i++) {
fin>>a>>b;
a--;
b--;
len=b-a+1;
k=logg(len);
fout<<min(st[a][k],st[a+len-(1<<k)][k])<<endl;
}
return 0;
}