const int N=5e5+5;
const int INF=0x3f3f3f3f;
int n,q;
int a[N],trmx[N],trmn[N];
//将原来的累加改为求最值
void add(int x,int k){
while(x<=n){
trmx[x]=max(trmx[x],k);
trmn[x]=min(trmn[x],k);
x+=lowbit(x);
}
}
//区间查询最大/小值
int fimx(int x,int y){
if(y>x){
if(y-lowbit(y)>x){
return max(trmx[y],fimx(x,y-lowbit(y)));
}
else{
return max(a[y],fimx(x,y-1));
}
}
return a[x];
}
int fimn(int x,int y){
if(y>x){
if(y-lowbit(y)>x){
return min(trmn[y],fimn(x,y-lowbit(y)));
}
else{
return min(a[y],fimn(x,y-1));
}
}
return a[x];
}
void solve(){
memset(trmn,INF,sizeof trmn);//求最小值记得初始化INF
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
add(i,a[i]);
}
while(q--){
int l,r;cin>>l>>r;
cout<<fimx(l,r)-fimn(l,r)<<endl;
}
}
标签:return,树状,int,lowbit,小值,数组,INF,fimn,trmn
From: https://www.cnblogs.com/TaopiTTT/p/18407031