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

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

USACO 2007 January Silver

题解:线段树,向上更新区间最值

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 }

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

时间: 2024-11-01 14:07:47

【POJ】3264 Balanced Lineup ——线段树 区间最值的相关文章

POJ 3264 Balanced Lineup (线段树单点更新 区间查询)

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

POJ 3264 Balanced Lineup (线段树)

Balanced Lineup 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

POJ 3264 Balanced Lineup 线段树 第三题

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

poj 3264 Balanced Lineup(线段数求区间最大最小值)

链接:http://poj.org/problem?id=3264 Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32772   Accepted: 15421 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.

POJ 3264 Balanced Lineup(最大最小差值 线段树水题)

Language: Default Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 37122   Accepted: 17383 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.

POJ3264 Balanced Lineup 线段树区间最大值 最小值

Q个数 问区间最大值-区间最小值 1 // #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #i

poj 3264 Balanced Lineup(查询区间最大值与最小值的差)

1.代码: #include<stdio.h> #include<string.h> #include<math.h> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) #define N 100000 int a[N]; int ST1[N][20]; int ST2[N][20]; int n,q; void make_ST() { for(int j=1;(1&l

POJ 3264 Balanced Lineup ST表

链接:http://poj.org/problem?id=3264 题意:给一串数字,多次询问,求区间最大值和区间最小值的差. 思路:RMQ问题,可以用O(N^2)的预处理,然后每次O(1)的查询,可以用线段树,O(N)的建树,O(logN)的查询,可以用ST表记录,O(NlogN)的预处理,O(1)的查询. 实际上ST表的预处理过程也是一个DP的过程dp[i][j]表示从第i位开始连续2^j位的区间最值. 预处理:dp[i][j]=min(dp[i][j],dp[i+2^j][j]),查询:q

poj 3264 Balanced Lineup

题目链接:http://poj.org/problem?id=3264 题目大意:就是给你一串数,问你最大数和最小数的差值....... 思路:最基本的线段树,只需要建树和查询,修改都省啦,但是查询要写两个,一个查询最大值,一个查询最小值......然后就能AC掉.....但是话说poj把它分类到RMQ中.... code: #include<cstdio> #include<cmath> #include<algorithm> #include<iostream