标签:query print ret line ref ons min iostream type
题目链接:http://poj.org/problem?id=3264
题意:n个数,给定m个区间,求出每个区间内最大值和最小值之差
题解:区间最值问题,挺裸的一道题
1 //POJ 3264 Balanced Lineup 2 //区间最值 3 #include <cstdio> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 typedef long long LL; 9 const int N=50000+10; 10 const int INF=0x3f3f3f3f; 11 LL ans1,ans2,n,m; 12 13 struct Tree 14 { 15 LL l,r; 16 LL MAX,MIN; 17 }; 18 Tree tree[4*N]; 19 20 void pushup(LL x) //向上更新 21 { 22 LL tmp=x<<1; 23 tree[x].MAX=max(tree[tmp].MAX,tree[tmp+1].MAX); 24 tree[x].MIN=min(tree[tmp].MIN,tree[tmp+1].MIN); 25 } 26 27 void build(LL l,LL r,LL x) 28 { 29 tree[x].l=l; 30 tree[x].r=r; 31 if(l==r) 32 { 33 scanf("%lld",&tree[x].MAX); 34 tree[x].MIN=tree[x].MAX; 35 return ; 36 } 37 LL tmp=x<<1; 38 LL mid=(l+r)>>1; 39 build(l,mid,tmp); 40 build(mid+1,r,tmp+1); 41 pushup(x); 42 } 43 44 void query(LL l,LL r,LL x) 45 { 46 if(r<tree[x].l||l>tree[x].r) return ; 47 if(l<=tree[x].l&&r>=tree[x].r) 48 { 49 ans1=max(ans1,tree[x].MAX); 50 ans2=min(ans2,tree[x].MIN); 51 return ; 52 } 53 LL tmp=x<<1; 54 LL mid=(tree[x].l+tree[x].r)>>1; 55 if(r<=mid) query(l,r,tmp); 56 else if(l>mid) query(l,r,tmp+1); 57 else 58 { 59 query(l,mid,tmp); 60 query(mid+1,r,tmp+1); 61 } 62 } 63 64 int main(){ 65 LL A,B; 66 scanf("%lld%lld",&n,&m); 67 build(1,n,1); 68 for(int i=1;i<=m;i++){ 69 ans1=0;ans2=INF; 70 scanf("%lld%lld",&A,&B); 71 query(A,B,1); 72 printf("%lld\n",ans1-ans2); 73 } 74 return 0; 75 }
POJ 3264 Balanced Lineup(线段树 区间最值)
标签:query print ret line ref ons min iostream type
原文地址:http://www.cnblogs.com/Leonard-/p/7624806.html