POJ 3264 Balanced Lineup(线段树区间查询)
来源:互联网 发布:席德梅尔 知乎 编辑:程序博客网 时间:2024/05/19 20:49
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
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
Sample Input
6 31734251 54 62 2
Sample Output
630
题目大意:有一群高矮不同的牛排成一队并按1到N编号,对这群牛进行Q次查询操作,每次查询返回区间内最高的牛与最矮的牛的高度差。
解题思路:区间查询的问题,可以用线段树解决。同时维护区间最大最小值。
代码如下:
#include <stdio.h>#include <stdlib.h>#include <limits.h>#define MAXN 50005#define LEFT(X) 2 * (X) + 1#define RIGHT(X) 2 * (X) + 2typedef struct { int min; int max;} Node;Node tree[MAXN * 4];Node merge(Node x, Node y) { Node tmp; tmp.min = x.min < y.min ? x.min : y.min; tmp.max = x.max > y.max ? x.max : y.max; return tmp;}void build(int treeIndex, int *arr, int lo, int hi) { if (lo == hi) { tree[treeIndex].min = arr[lo]; tree[treeIndex].max = arr[lo]; return; } int mid = lo + (hi - lo) / 2; build(2 * treeIndex + 1, arr, lo, mid); build(2 * treeIndex + 2, arr, mid + 1, hi); tree[treeIndex] = merge(tree[LEFT(treeIndex)], tree[RIGHT(treeIndex)]);}Node query(int treeIndex, int lo, int hi, int i, int j) { if (lo > j || hi < i) { Node tmp; tmp.min = INT_MAX; tmp.max = 0; return tmp; } if (i <= lo && hi <= j) return tree[treeIndex]; int mid = lo + (hi - lo) / 2; if (i > mid) return query(RIGHT(treeIndex), mid + 1, hi, i, j); else if (j <= mid)return query(LEFT(treeIndex), lo, mid, i, j); Node leftQuery = query(LEFT(treeIndex), lo, mid, i, mid); Node rightQuery = query(RIGHT(treeIndex), mid + 1, hi, mid + 1, j); return merge(leftQuery, rightQuery);}int main(void) { int N, Q, a, b; int arr[MAXN]; Node ans; scanf("%d %d", &N, &Q); for (int i = 0; i < N; i++) scanf("%d", &arr[i]); build(0, arr, 0, N - 1); while (Q--) { scanf("%d %d", &a, &b); ans = query(0, 0, N - 1, a - 1, b - 1); printf("%d\n", ans.max - ans.min); } return 0;}
- POJ 3264 Balanced Lineup(线段树区间查询)
- POJ 3264 Balanced Lineup 线段树的构建+区间查询
- POJ 3264 Balanced Lineup (线段树单点更新 区间查询)
- POJ 3264-Balanced Lineup(线段树:单点更新,区间查询)
- poj 3264 Balanced Lineup(线段树单点修改区间查询)
- Balanced Lineup (POJ_3264) 线段树+区间查询
- G - Balanced Lineup POJ 3264 (线段树+区间查询无更新)
- 【POJ 3264】【RMQ 或者线段树】Balanced Lineup 【查询区间内最大最小值的差】
- POJ 3264-Balanced Lineup详解(线段树区间求值)
- poj 3264 Balanced Lineup线段树区间最值差
- POJ 3264 Balanced Lineup 求线段树区间最值
- poj 3264 Balanced Lineup(线段树 区间最值)
- poj Balanced Lineup (线段树区间最值+优化)
- POJ 3264 - Balanced Lineup(线段树)
- Balanced Lineup(POJ-3264)(线段树)
- POJ - 3264 - Balanced Lineup (线段树)
- poj 3264 Balanced Lineup(线段树)
- POJ 3264Balanced Lineup(线段树)
- Mybatis逆向工程
- hdu 3826 Hand in Hand 同构图★
- 见过最好的神经网络CNN解释
- 斐波切纳 数列 java
- c语言初步经典题5----求两个正整数的正差值
- POJ 3264 Balanced Lineup(线段树区间查询)
- 数组的静态初始化
- 2.常用控件:TextView
- RxJava Essentials 中文翻译版
- Python基础三
- 14. nohup
- 方法的调用
- link标签中的integrity和crossorigin字段
- Bomb [数位DP]