POJ - 3264 Balanced Lineup
作者:互联网
线段树,求区间最大值减去区间最小值的值。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N = 5e4 + 10;
const int inf = 1e9 + 7;
int n,q;
#define mid ((l + r) >> 1)
#define ls (nod << 1)
#define rs (nod << 1 | 1)
#define lson ls,l,mid
#define rson rs,mid + 1,r
int mx[N << 2],mi[N << 2];
void build(int nod,int l,int r){
if(l == r) { scanf("%d",&mx[nod]); mi[nod] = mx[nod]; return ; }
build(lson); build(rson);
mx[nod] = max(mx[ls],mx[rs]); mi[nod] = min(mi[ls],mi[rs]);
}
int query(int nod,int l,int r,int ll,int rr,int pos){
if(l > rr || r < ll) return pos ? -inf : inf;
if(ll <= l && r <= rr) return pos ? mx[nod] : mi[nod];
return pos ? max(query(lson,ll,rr,pos), query(rson,ll,rr,pos)) : min(query(lson,ll,rr,pos), query(rson,ll,rr,pos));
}
int main(){
scanf("%d%d",&n,&q);
build(1,1,n);
while(q --){
int l,r; scanf("%d%d",&l,&r);
printf("%d\n",query(1,1,n,l,r,1) - query(1,1,n,l,r,0));
}
return 0;
}
标签:Lineup,const,int,ll,POJ,Balanced,inf,include,define 来源: https://www.cnblogs.com/zzhzzh123/p/13401345.html