【POJ】3264 Balanced Lineup ——线段树 区间最值

本文介绍如何使用线段树解决在一组数中找到一个子区间内的最大值和最小值,并计算它们之差的问题。具体包括构建线段树、更新区间值以及查询区间最大值和最小值的实现过程。

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

Balanced Lineup

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 34140 Accepted: 16044
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

 
题解:线段树,向上更新区间最值
AC代码:
  1 #include <cstdio>
  2 #include <cstring>
  3 
  4 #define MAX(a, b) (a > b ? a : b)
  5 #define MIN(a, b) (a < b ? a : b) //宏定义提高效率
  6 
  7 const int LEN = 50050;
  8 
  9 struct Seg
 10 {
 11     int left, right;
 12     int ma, mi;
 13 }seg[LEN*4];
 14 
 15 void buildt(int l, int r, int step)
 16 {
 17     seg[step].left = l;
 18     seg[step].right = r;
 19     seg[step].ma = 0;
 20     seg[step].mi = 0x7fffffff;
 21     if (l == r)
 22         return;
 23     int mid = (l + r)>>1;
 24     buildt(l, mid, step<<1);
 25     buildt(mid+1, r, step<<1|1);
 26 }
 27 
 28 void pushup(int step) //向上更新
 29 {
 30     seg[step].ma = MAX(seg[step<<1].ma, seg[step<<1|1].ma);
 31     seg[step].mi = MIN(seg[step<<1].mi, seg[step<<1|1].mi);
 32 }
 33 
 34 void update(int l, int r, int height, int step)
 35 {
 36     if (l == seg[step].left && r == seg[step].right){
 37         seg[step].mi = height;
 38         seg[step].ma = height;
 39         return;
 40     }
 41     if (seg[step].left == seg[step].right)
 42         return;
 43     int mid = (seg[step].left + seg[step].right)>>1;
 44     if (r <= mid)
 45         update(l, r, height, step<<1);
 46     else if (l > mid)
 47         update(l, r, height, step<<1|1);
 48     else{
 49         update(l, mid, height, step<<1);
 50         update(mid+1, r, height, step<<1|1);
 51     }
 52     pushup(step); //递归中更新完下一个节点后向上更新
 53 }
 54 
 55 int queryma(int l, int r, int step) //求区间最大值
 56 {
 57     if (l == seg[step].left && r == seg[step].right){
 58         return seg[step].ma;
 59     }
 60     if (seg[step].left == seg[step].right)
 61         return 0;
 62     int mid = (seg[step].left + seg[step].right)>>1;
 63     if (r <= mid)
 64         return queryma(l, r, step<<1);
 65     else if (l > mid)
 66         return queryma(l, r, step<<1|1);
 67     else{
 68         int a = queryma(l, mid, step<<1);
 69         int b = queryma(mid+1, r, step<<1|1); //防止使用宏定义时多次调用queryma,先调用得到返回值,再比较返回值
 70         return MAX(a, b);
 71     }
 72 }
 73 
 74 int querymi(int l, int r, int step) //求区间最小值
 75 {
 76     if (l == seg[step].left && r == seg[step].right){
 77         return seg[step].mi;
 78     }
 79     if (seg[step].left == seg[step].right)
 80         return 0x7fffffff;
 81     int mid = (seg[step].left + seg[step].right)>>1;
 82     if (r <= mid)
 83         return querymi(l, r, step<<1);
 84     else if (l > mid)
 85         return querymi(l, r, step<<1|1);
 86     else{
 87         int a = querymi(l, mid, step<<1);
 88         int b = querymi(mid+1, r, step<<1|1); //同上
 89         return MIN(a, b);
 90     }
 91 }
 92 
 93 int main()
 94 {
 95     int n, q;
 96     scanf("%d %d", &n, &q);
 97     buildt(1, n, 1);
 98     for(int i = 1; i <= n; i++){
 99         int t;
100         scanf("%d", &t);
101         update(i, i, t, 1);
102     }
103     for(int i = 0; i < q; i++){
104         int a, b;
105         scanf("%d %d", &a, &b);
106         printf("%d\n", queryma(a, b, 1) - querymi(a, b, 1));
107     }
108     return 0;
109 }

 

转载于:https://www.cnblogs.com/kevince/p/3894326.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值