标签:
0
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
const int maxn=50011;
const int inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,n) for(int i=0;i<(n);i++)
template<class T>inline T read(T&x)
{
char c;
while((c=getchar())<=32);
bool ok=false;
if(c=='-')ok=true,c=getchar();
for(x=0; c>32; c=getchar())
x=x*10+c-'0';
if(ok)x=-x;
return x;
}
template<class T> inline void read_(T&x,T&y)
{
read(x);
read(y);
}
template<class T> inline void read__(T&x,T&y,T&z)
{
read(x);
read(y);
read(z);
}
template<class T> inline void write(T x)
{
if(x<0)putchar('-'),x=-x;
if(x<10)putchar(x+'0');
else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
write(x);
putchar('\n');
}
//-------IO template------
typedef long long LL;
struct node
{
int maxv,minv;
}p[maxn<<3];
void build(int rt,int L,int R)
{
p[rt].maxv=-inf;
p[rt].minv=inf;
if(L==R)
{
int a;
read(a);
p[rt].maxv=p[rt].minv=a;
return ;
}
build(lson);
build(rson);
p[rt].maxv=max(p[rt<<1].maxv,p[rt<<1|1].maxv);
p[rt].minv=min(p[rt<<1].minv,p[rt<<1|1].minv);
}
int a,b;
void query(int rt,int L,int R,int x,int y)
{
if(L==x&&y==R)
{
a=max(a,p[rt].maxv);
b=min(b,p[rt].minv);
return ;
}
if(y<=M)
query(lson,x,y);
else if(x>M)
query(rson,x,y);
else
{
query(lson,x,M);
query(rson,M+1,y);
}
}
int main()
{
int n,m;
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
build(1,1,n);
while(m--)
{
int x,y;
read_(x,y);
a=-inf;b=inf;
query(1,1,n,x,y);
writeln(a-b);
}
}
return 0;
}
G - Balanced Lineup POJ 3264 (线段树+区间查询无更新)
标签:
原文地址:http://blog.csdn.net/u013167299/article/details/44904455