POJ 3264 Balanced Lineup (线段树查找最大最小值)

http://poj.org/problem?id=3264

题意:给你一个长度为n的序列a[N] (1 ≤ N ≤ 50000),询问Q(1 ≤ Q ≤ 200000) 次,每次输出【L, R】区间最大值与最小值的差是多少。

只需把模板的求和改成求最大和最小即可

#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <queue>
using namespace std;
const int maxn = 1e5+ ;
const int inf = 0x3f3f3f3f; struct node{
int l,r;
int add;
int sum;
int mx;
int mn;
}tree[maxn<<]; int kase=;
int n,m,t;
int a,b,c;
int val = ;
int ans = ;
int mx,mn;
void pushup(int k)
{
//tree[k].sum = tree[k<<1].sum+tree[k<<1|1].sum;
tree[k].mx = max(tree[k<<].mx,tree[k<<|].mx);
tree[k].mn = min(tree[k<<].mn,tree[k<<|].mn);
} void build(int l,int r,int k)
{
tree[k].l = l; tree[k].r = r;
if(l == r){scanf("%d",&tree[k].sum); tree[k].mn = tree[k].mx = tree[k].sum; return; }
int mid = (l+r)>>;
build(l,mid,k<<);
build(mid+,r,k<<|);
pushup(k);
} void query(int k)
{
if(a <= tree[k].l && b >= tree[k].r)
{
mx = max(mx,tree[k].mx);
mn = min(mn,tree[k].mn);
return ;
} int mid = (tree[k].l+tree[k].r)>>;
if(a <= mid){ query(k<<);}
if(b > mid){ query(k<<|);}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
build(,n,);
while(m--)
{
scanf("%d%d",&a,&b);
mx = -;
mn = inf;
query();
ans = mx -mn;
printf("%d\n",ans);
}
}
}
上一篇:用 rsync 同步本地和服务器的文件


下一篇:javascript Navigator对象