Pagini recente » Cod sursa (job #1716623) | Cod sursa (job #2705057) | Cod sursa (job #2705071) | Cod sursa (job #3125868) | Cod sursa (job #2755919)
#include<iostream>
#include<fstream>
using namespace std;
ifstream in("rmq.in");
ofstream out("rmq.out");
const int mx=1e5+200;
int n,m,v[mx],dp[19][mx],lg2[mx];
void read(){
in>>n>>m;
for(int i=0;i<n;i++){
in>>v[i];
}
}
void preprocess(){
for(int i=0;i<n;i++){
dp[0][i]=v[i];
}
lg2[1]=0;
for(int i=2;i<mx;i++){
lg2[i]=1+lg2[i/2];
}
for(int i=1;(1<<i)<=n;i++){
int last=(1<<(i-1)),here=1<<(i);
for(int k=0;k<=n-here;k++){
dp[i][k]=min(dp[i-1][k],dp[i-1][k+last]);
}
}
}
int query(int l,int r){
int length=r-l+1,chunk=lg2[length],dim=1<<chunk;
return min(dp[chunk][l],dp[chunk][r-dim+1]);
}
void solve(){
preprocess();
int l,r;
for(int i=0;i<m;i++){
in>>l>>r;
out<<query(l-1,r-1)<<'\n';
}
}
int main(){
read();
solve();
return 0;
}