# 基础之基础线段树

#include<iostream>
#define maxn 1000
typedef struct lnode{
int max;
int min;
}lnode;
lnode node[maxn<<2];
int A[maxn],n;
int nowmax=-1,nowmin=100007;
using namespace std;
int qumax(int a,int b)
{
return a>b?a:b;
}
int qumin(int a,int b)
{
return a>b?b:a;
}
void pushup(int rt)
{
node[rt].max=qumax(node[rt<<1].max,node[rt<<1|1].max);
node[rt].min=qumin(node[rt<<1].min,node[rt<<1|1].min);
}
void build(int l,int r,int rt)
{
if(l==r){
node[rt].max=A[l];
node[rt].min=A[l];
return ;
}
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
pushup(rt);
}
void query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R){
nowmax=qumax(node[rt].max,nowmax);
nowmin=qumin(node[rt].min,nowmin);
return;
}
int m=(l+r)>>1;
if(L<=m) query(L,R,l,m,rt<<1);
if(R>m) query(L,R,m+1,r,rt<<1|1);
}
int main()
{
int n1,m1;
while(cin>>n1>>m1){
for(int i=1;i<=n1;++i){
cin>>A[i];
}
build(1,n1,1);
for(int i=0;i<m1;++i){
int x, y;
cin>>x;cin>>y;
query(x,y,1,n1,1);
cout<<nowmax-nowmin<<endl;
nowmax=-1,nowmin=1e7;
}
}
return 0;
}

posted @ 2018-10-27 22:52  tacore  阅读(79)  评论(0编辑  收藏  举报