
倍增法
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define f first
#define s second
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
using namespace std;
//declare
const int maxn = 2e5+5;
int n,q;
int rmq[20][maxn];
vector<int> v;
//
void build(){
for(int i=0;i<n;i++) rmq[0][i] = v[i];
for(int i=1;i<=__lg(n);i++){
for(int j=0;j<n;j++){
rmq[i][j] = min(rmq[i-1][j], rmq[i-1][j + (1 << (i-1))]);
}
}
}
signed main(){
fastio;
cin>>n>>q;
for(int i=0;i<n;i++){
int x; cin>>x;
v.push_back(x);
}
build();
while(q--){
int L,R; cin>>L>>R; L--;R--;
int lg = __lg(R-L+1);
cout<<min(rmq[lg][L], rmq[lg][R-(1<<lg)+1])<<"\n";
}
return 0;
}

發佈留言