poj 3264 Balanced Lineup线段树区间最值差

本文介绍了一种使用线段树解决查询特定范围内最大值与最小值之差的问题。通过预先构建线段树并存储节点的最大值与最小值,可以高效地进行多次查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 50919 Accepted: 23849
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

题目链接:    http://poj.org/problem?id=3264

题目大意:   给出初始化的区间值,m次查询

                  每次查询区间[a,b]的最大值-最小值

解题思路:   线段树    更新: 无更新    查询:区间查询

                  建立线段树的时候,每个结点存储左右子树的最大值和最小值

                  查询时直接访问区间最大值和最小值,不需要查找到最低

                  查询时间复杂度O(logN)

代码:

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #define MAXN 70000  
  5. #define INF 0x3f3f3f3f  
  6. #define MAX(a,b) a>b?a:b  
  7. #define MIN(a,b) a<b?a:b  
  8. #define MID(a,b) (a+b)>>1  
  9. #define L(a) a<<1  
  10. #define R(a) (a<<1)+1  
  11.   
  12. typedef struct snode{  
  13.     int left,right;  
  14.     int max,min;  
  15. }Node;  
  16.   
  17. Node Tree[MAXN<<1];  
  18. int num[MAXN],minx,maxx;  
  19.   
  20. void Build(int t,int l,int r)    //以t为根结点建立左子树为l,右子树为r的线段树  
  21. {  
  22.     int mid;  
  23.     Tree[t].left=l,Tree[t].right=r;  
  24.     if(Tree[t].left==Tree[t].right)  
  25.     {  
  26.         Tree[t].max=Tree[t].min=num[l];  
  27.         return ;  
  28.     }  
  29.     mid=MID(Tree[t].left,Tree[t].right);  
  30.     Build(L(t),l,mid);  
  31.     Build(R(t),mid+1,r);  
  32.     Tree[t].max=MAX(Tree[L(t)].max,Tree[R(t)].max);  //更新结点的最大值=MAX(左子树,右子树)  
  33.     Tree[t].min=MIN(Tree[L(t)].min,Tree[R(t)].min);  //更新结点的最小时=MIN(左子树,右子树)  
  34. }  
  35.   
  36. void Query(int t,int l,int r)    //查询结点为t,左子树为l,右子树为r的最大值和最小值  
  37. {  
  38.     int mid;  
  39.     if(Tree[t].left==l&&Tree[t].right==r)  
  40.     {  
  41.         if(maxx<Tree[t].max)  
  42.             maxx=Tree[t].max;  
  43.         if(minx>Tree[t].min)  
  44.             minx=Tree[t].min;  
  45.         return ;  
  46.     }  
  47.     mid=MID(Tree[t].left,Tree[t].right);  
  48.     if(l>mid)  
  49.     {  
  50.         Query(R(t),l,r);  
  51.     }  
  52.     else if(r<=mid)  
  53.     {  
  54.         Query(L(t),l,r);  
  55.     }  
  56.     else  
  57.     {  
  58.         Query(L(t),l,mid);  
  59.         Query(R(t),mid+1,r);  
  60.     }  
  61. }  
  62.   
  63. int main()  
  64. {  
  65.     int n,m,a,b,i;  
  66.     memset(Tree,0,sizeof(Tree));  
  67.     scanf("%d%d",&n,&m);  
  68.     for(i=1;i<=n;i++)  
  69.         scanf("%d",&num[i]);  
  70.     Build(1,1,n);            //建立以1为根结点区间为[1,n]的线段树  
  71.     while(m--)  
  72.     {  
  73.         scanf("%d%d",&a,&b);  
  74.         maxx=0;minx=INF;     //初始化最大值为0,最小值为INF  
  75.         Query(1,a,b);        //查询区间[a,b]的最大值和最小值  
  76.         printf("%d\n",maxx-minx);  
  77.     }  
  78.     return 0;  
  79. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值