#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define inf 0x3f3f3f3f
#define L u<<1
#define R u<<1|1
const int N=2e5+5;
int n,m;
int a[N];
struct node{
int l,r,v;
}tr[N*4];
void pushup(int u){
tr[u].v=max(tr[L].v,tr[R].v);
}
void build(int u,int l,int r){
tr[u].l=l,tr[u].r=r;
if(l==r){
tr[u].v=a[l];
return;
}
int mid=l+r>>1;
build(L,l,mid),build(R,mid+1,r);
pushup(u);
}
int query(int u,int l,int r){
if(tr[u].l>=l&&tr[u].r<=r) return tr[u].v;
int mid=tr[u].l+tr[u].r>>1;
int maxx=-inf;
if(l<=mid) maxx=max(maxx,query(L,l,r));
if(r>mid) maxx=max(maxx,query(R,l,r));
return maxx;
}
void solve(){
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
build(1,1,n);
cin>>m;
while(m--){
int x,y;
cin>>x>>y;
cout<<query(1,x,y)<<endl;
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1;
// cin>>t;
while(t--) solve();
return 0;
}
1544:天才的记忆(线段树版)
最新推荐文章于 2024-11-04 21:39:11 发布