# POJ3264 Balanced Lineup(线段树静态)

# include<stdio.h>
# include<stdlib.h>
# include<algorithm>
using namespace std;
int a[10000000],x,y;
struct node{
int b,e,s,c;
}b[1000000];
int big(int x,int y){//求x——y的最大值
if(x==y){
return a[x];
}
return max(big(x,(x+y)/2),big((x+y)/2+1,y));

}
int lit(int x,int y){//求x——y的最小值
if(x==y){
return a[x];
}
return min(lit(x,(x+y)/2),lit((x+y)/2+1,y));
}
void lily(int t,int x,int y){//建树
b[t].b=x;
b[t].e=y;
b[t].s=big(x,y);//求这个范围内的最大值
b[t].c=lit(x,y);
if(x==y)return;
lily(t*2,x,(x+y)/2);//第归建树
lily(t*2+1,(x+y)/2+1,y);
}
int find(int t,int r,int l){//查询最大值
if(r==x &&l==y)return b[t].s;//当前范围=需求范围返回最大值
if(r>=x &&l<=y){
return b[t].s;
}
int h=0,f=0;
if((r<x && l<x) ||(r>y && l>y))return 0;//不在范围内返回0
if(x<=(r+l)/2)//第归
h=find(t*2,r,(r+l)/2);
if(y>=(r+l)/2)
f=find(t*2+1,(r+l)/2+1,l);
return max(h,f);//求最大
}
int finds(int t,int r,int l){//查询最小值
if(r==x &&l==y){return b[t].c;}//当前范围=需求范围返回最小值
if(r>=x &&l<=y){
return b[t].c;
}
int h=0,f=0;
if((r<x && l<x) ||(r>y && l>y))return 0;//不在范围内返回0
if(x<=(r+l)/2)//第归
h=finds(t*2,r,(r+l)/2);
if(y>=(r+l)/2)
f=finds(t*2+1,(r+l)/2+1,l);
if(h!=0 &&f!=0)//返回小且不为0
return min(h,f);
if(h!=0)return h;
return f;
}
int main(){
int n,m,i;
scanf("%d%d",&n,&m);//读入
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
lily(1,1,n);
for(i=1;i<=m;i++){
scanf("%d%d",&x,&y);
printf("%d\n",find(1,1,n)-finds(1,1,n));//输出
}
return 0;
}

#### poj 3624 Balanced Lineup

2017-06-02 14:17:00

#### poj 3264——Balanced Lineup

2013-07-28 17:28:56

#### poj 3246 Balanced Lineup

2018-03-30 20:34:58

#### Poj 3264 Balanced Lineup

2012-01-20 20:52:00

#### POJ&nbsp;2364&nbsp;Balanced&nbsp;Lineup

2015-12-18 18:13:41

#### poj3264 - Balanced Lineup

2012-05-21 19:03:14

#### BZOJ 1699 [Usaco2007 Jan]Balanced Lineup排队 线段树

2015-08-18 21:53:55

#### Balanced Lineup（线段树—指针实现）

2015-05-09 09:49:24

#### poj3264 Balanced Lineup(线段树)

2015-01-19 20:54:40

#### poj3264，Balanced Lineup，线段树

2015-01-06 09:33:55