题意:
求区间最大值和最小值的差值。
题解:
线段树简单query,修改都没有~~~。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<vector>
#include<functional>
#include<utility>
#include<set>
#include<map>
#include<cmath>
#include<cctype>
#define INF 0x3f3f3f3fusing namespace std;#define lson 2*i,l,m
#define rson 2*i+1,m+1,rconst int maxn=200000+100;int maxv[maxn<<2],minv[maxn<<2];
void pushup(int i)
{maxv[i]=max(maxv[2*i],maxv[2*i+1]);minv[i]=min(minv[2*i],minv[2*i+1]);
}void build(int i,int l,int r)
{if(l==r){scanf("%d",&minv[i]);maxv[i]=minv[i];return ;}int m=(l+r)/2;build(lson);build(rson);pushup(i);
}int query_max(int ql,int qr,int i,int l,int r)
{if(ql<=l&&qr>=r){return maxv[i];}int res=-INF;int m=(l+r)/2;if(ql<=m)res=max(res,query_max(ql,qr,lson));if(qr>m)res=max(res,query_max(ql,qr,rson));return res;
}int query_min(int ql,int qr,int i,int l,int r)
{if(ql<=l&&qr>=r){return minv[i];}int res=INF;int m=(l+r)/2;if(ql<=m)res=min(res,query_min(ql,qr,lson));if(qr>m)res=min(res,query_min(ql,qr,rson));return res;
}
int main()
{int n,q;while(cin>>n>>q){build(1,1,n);while(q--){int l,r;scanf("%d%d",&l,&r);printf("%d\n",query_max(l,r,1,1,n)-query_min(l,r,1,1,n));}}
}