poj 3264 Balanced Lineup - 线段树

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 ≤ ABN), 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


  区间的什么的基本上可以先想到线段树,直接存储一段区间内最大值和最小的差反正我是没想出来,

所以我就把每一段的最大值和最小值存下来,到询问的时候,就去查找,然后得到最大值和最小值,然后

相减,输出。基本就完事了。

注:1.只用写pushup函数,因为没有区间更新

    2.更新时需要更新最大值和最小值(pushup建树时会被使用)

  3.用scanf读入

Code

  1 /*
  2  * poj.org
  3  * Problem#3264
  4  * Accepted
  5  * Time:3161ms
  6  * Memory:5076k
  7  */
  8 #include<iostream>
  9 #include<cstdio>
 10 using namespace std;
 11 int *a;
 12 typedef struct MyData{
 13     int maxv;
 14     int minv;
 15     MyData():maxv(0xffffffff),minv(0x7fffffff){}
 16 }MyData;
 17 typedef class TreeNode{
 18     private:
 19         void init(){
 20             minv = 0x7fffffff;
 21             maxv = 0xffffffff;
 22             left = NULL;
 23             right = NULL;
 24         }
 25     public:
 26         int from;
 27         int end;
 28         int minv;
 29         int maxv;
 30         TreeNode* left;
 31         TreeNode* right;
 32         TreeNode(){ init(); }
 33         TreeNode(int from, int end){
 34             init();
 35             this->from = from;
 36             this->end = end;
 37         }
 38 }TreeNode;
 39 typedef class Tree {
 40     private:
 41         static int _max(int a, int b){
 42             return (a > b)?(a):(b);
 43         }
 44         static int _min(int a, int b){
 45             return (a < b)?(a):(b);
 46         }
 47     public:
 48         int buffer[2];
 49         TreeNode *root;
 50         Tree():root(NULL){}
 51         Tree(int size){
 52             root = build(root, 1, size);
 53         }
 54         void pushUp(TreeNode* node){
 55             node->maxv = _max(node->left->maxv, node->right->maxv);
 56             node->minv = _min(node->left->minv, node->right->minv);
 57         }
 58         TreeNode* build(TreeNode* root, int from, int end){
 59             root = new TreeNode(from, end);
 60             if( from == end ){
 61                 root->minv = a[from];
 62                 root->maxv = a[from];
 63                 return root;
 64             }
 65             int mid = (from + end)>>1;
 66             root->left = build(root->left, from, mid);
 67             root->right = build(root->right, mid + 1, end);
 68             pushUp(root);
 69             return root;
 70         }
 71         MyData query(TreeNode* now, int from, int end){
 72             MyData result;
 73             if(from <= now->from && end >= now->end){
 74                 result.maxv = now->maxv;
 75                 result.minv = now->minv;
 76                 return result;
 77             }
 78             int mid = (now->from + now->end)>>1;
 79             if(end <= mid) return query(now->left, from, end);
 80             else if(from > mid) return query(now->right, from, end);
 81             else{
 82                 MyData cmp = query(now->left, from, mid);
 83                 result = query(now->right, mid + 1, end);
 84                 result.minv = _min(result.minv, cmp.minv);
 85                 result.maxv = _max(result.maxv, cmp.maxv);
 86                 return result;
 87             }
 88         }
 89 }Tree;
 90 Tree MyTree;
 91 int n,q;
 92 int d,c;
 93 MyData aaa;
 94 int main(){
 95     scanf("%d%d",&n,&q);
 96     a = new int[(const int)(n + 1)];
 97     for(int i = 1;i <= n;i++){
 98         scanf("%d",&a[i]);
 99     }
100     MyTree = Tree(n);
101     for(int i = 1;i <= q;i++){
102         scanf("%d%d",&d,&c);
103         aaa = MyTree.query(MyTree.root, d, c);
104         printf("%d\n",aaa.maxv - aaa.minv);
105     }
106     return 0;
107 }


 

posted @ 2016-07-13 11:44  阿波罗2003  阅读(...)  评论(...编辑  收藏