poj 3264:Balanced Lineup(线段树,经典题)

Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 32820   Accepted: 15447
Case Time Limit: 2000MS

Description

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

Input

Line 1: Two space-separated integers, N and Q
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i 
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

Output

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

Source

 
  线段树,经典题
  题意
  给定N个数(1 ≤ N ≤ 50,000),询问Q次(1 ≤ Q ≤ 200,000),每次询问某一区间[A,B]内的最大值和最小值的差。
  思路
  很明显要用线段树来做,线段树一个很重要的问题就是要想清楚每个区间节点内存储的信息。这道题一开始最直接的想法是存储当前节点区间的最大值和最小值的差,但是每个区间的最大值和最小值又是各不相同的,很难达成统一,所以这个思路是不可行的。那么接下来就想到存储两个信息,区间内的最大值和最小值,这样查找一个区间的时候对比找到组成这个区间的所有区间中的最大值和最小值即可。然后输出两者之才差。
  注意
  所有的输入建议改为scanf即C的形式,否则用C++的形式会很容易超时。
  代码
 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 
 5 #define MAXN 50000
 6 #define INF 99999999
 7 int MaxV,MinV;
 8 
 9 struct Node{
10     int L,R;
11     int ma,mi;    //区间[L,R]的最大值和最小值
12 }a[MAXN*3];
13 
14 int Max(int x,int y)
15 {
16     return x>y?x:y;
17 }
18 
19 int Min(int x,int y)
20 {
21     return x<y?x:y;
22 }
23 
24 void Build(int d,int l,int r)    //建立线段树
25 {
26     if(l==r){    //找到叶子节点
27         scanf("%d",&a[d].ma);
28         a[d].mi = a[d].ma;
29         a[d].L = l;
30         a[d].R = r;
31         return ;
32     }
33 
34     //初始化当前节点的信息
35     a[d].L = l;
36     a[d].R = r;
37 
38     //建立线段树
39     int mid = (l+r)>>1;
40     Build(d<<1,l,mid);
41     Build(d<<1|1,mid+1,r);
42 
43     //更新当前节点的信息
44     a[d].ma = Max(a[d<<1].ma,a[d<<1|1].ma);
45     a[d].mi = Min(a[d<<1].mi,a[d<<1|1].mi);
46 }
47 
48 void Query(int d,int l,int r)    //查询区间[l,r]的最大值和最小值的差
49 {
50     if(a[d].ma<MaxV && a[d].mi>MinV)    //优化,如果当前区间的最大值和最小值在MaxV和MinV之间,则没必要继续递归该区间。能快100MS
51         return ;
52     if(a[d].L==l && a[d].R==r){    //找到终止节点
53         MaxV = Max(MaxV,a[d].ma);
54         MinV = Min(MinV,a[d].mi);
55         return ;
56     }
57 
58     int mid = (a[d].L+a[d].R)/2;
59     if(mid>=r){    //左孩子找
60         Query(d<<1,l,r);
61     }
62     else if(mid<l){    //右孩子找
63         Query(d<<1|1,l,r);
64     }
65     else{    //左孩子、右孩子都找
66         Query(d<<1,l,mid);
67         Query(d<<1|1,mid+1,r);
68     }
69 }
70 
71 int main()
72 {
73     int n,q,A,B;
74     scanf("%d%d",&n,&q);
75     Build(1,1,n);
76     while(q--){    //q次询问
77         scanf("%d%d",&A,&B);
78         MaxV = -INF;
79         MinV = INF;
80         Query(1,A,B);
81         printf("%d\n",MaxV-MinV);
82     }
83     return 0;
84 }

 

Freecode : www.cnblogs.com/yym2013

posted @ 2014-07-14 20:22  Freecode#  阅读(467)  评论(0编辑  收藏  举报