题面
RMQ问题
题解
线段树
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 100010;
int a[maxn];
struct node{
int l, r;
int val;
}sgt[maxn<<2];
void build(int p, int l, int r){
sgt[p].l = l, sgt[p].r = r;
if(l == r){
sgt[p].val = a[l];
}else{
int m = (l+r)/2;
build(p*2,l,m);
build(p*2+1,m+1,r);
sgt[p].val = min(sgt[p*2].val,sgt[p*2+1].val);
}
}
int query(int p, int l, int r){
if(l <= sgt[p].l && sgt[p].r <= r)return sgt[p].val;
int m = (sgt[p].l+sgt[p].r)/2, ans = 0xffffff;
if(l <= m)ans = min(ans, query(p*2,l,r));
if(r > m)ans = min(ans, query(p*2+1,l,r));
return ans;
}
int main(){
int n, m;
cin>>n>>m;
for(int i = 1; i <= n; i++)cin>>a[i];
build(1,1,n);
for(int i = 1; i <= m; i++){
int x, y; cin>>x>>y;
cout<<query(1,x,y)<<" ";
}
}