区间最值
https://www.luogu.com.cn/problem/P3865
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define lson l,m,st<<1
#define rson m+1,r,st<<1|1
typedef long long ll;
const int maxn = 1e5 + 5;
int a[maxn];
int tree[maxn<<2];
void build(int l,int r,int st)
{
if(l==r)
tree[st]=a[l];
else
{
int m=(l+r)>>1;
build(lson);
build(rson);
tree[st]=max(tree[lson],tree[rson]);
}
}
int query(int L,int R,int l,int r,int st)
{
if(L<=l&&r<=R)
return tree[st];
int m=(l+r)>>1;
int ans=0;
if(L<=m) ans= max(query(L,R,lson),ans);
if(m<R) ans= max(query(L,R,rson),ans);
return ans;
}
int main() {
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,n,1);
int l,r;
while(m--)
{
scanf("%d%d",&l,&r);
printf("%d\n",query(l,r,1,n,1));
}
return 0;
}