【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
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 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
Source
#include <cstdio>
#include <cstring> #define MAX(a, b) (a > b ? a : b)
#define MIN(a, b) (a < b ? a : b) //宏定义提高效率 const int LEN = ; struct Seg
{
int left, right;
int ma, mi;
}seg[LEN*]; void buildt(int l, int r, int step)
{
seg[step].left = l;
seg[step].right = r;
seg[step].ma = ;
seg[step].mi = 0x7fffffff;
if (l == r)
return;
int mid = (l + r)>>;
buildt(l, mid, step<<);
buildt(mid+, r, step<<|);
} void pushup(int step) //向上更新
{
seg[step].ma = MAX(seg[step<<].ma, seg[step<<|].ma);
seg[step].mi = MIN(seg[step<<].mi, seg[step<<|].mi);
} void update(int l, int r, int height, int step)
{
if (l == seg[step].left && r == seg[step].right){
seg[step].mi = height;
seg[step].ma = height;
return;
}
if (seg[step].left == seg[step].right)
return;
int mid = (seg[step].left + seg[step].right)>>;
if (r <= mid)
update(l, r, height, step<<);
else if (l > mid)
update(l, r, height, step<<|);
else{
update(l, mid, height, step<<);
update(mid+, r, height, step<<|);
}
pushup(step); //递归中更新完下一个节点后向上更新
} int queryma(int l, int r, int step) //求区间最大值
{
if (l == seg[step].left && r == seg[step].right){
return seg[step].ma;
}
if (seg[step].left == seg[step].right)
return ;
int mid = (seg[step].left + seg[step].right)>>;
if (r <= mid)
return queryma(l, r, step<<);
else if (l > mid)
return queryma(l, r, step<<|);
else{
int a = queryma(l, mid, step<<);
int b = queryma(mid+, r, step<<|); //防止使用宏定义时多次调用queryma,先调用得到返回值,再比较返回值
return MAX(a, b);
}
} int querymi(int l, int r, int step) //求区间最小值
{
if (l == seg[step].left && r == seg[step].right){
return seg[step].mi;
}
if (seg[step].left == seg[step].right)
return 0x7fffffff;
int mid = (seg[step].left + seg[step].right)>>;
if (r <= mid)
return querymi(l, r, step<<);
else if (l > mid)
return querymi(l, r, step<<|);
else{
int a = querymi(l, mid, step<<);
int b = querymi(mid+, r, step<<|); //同上
return MIN(a, b);
}
} int main()
{
int n, q;
scanf("%d %d", &n, &q);
buildt(, n, );
for(int i = ; i <= n; i++){
int t;
scanf("%d", &t);
update(i, i, t, );
}
for(int i = ; i < q; i++){
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", queryma(a, b, ) - querymi(a, b, ));
}
return ;
}
【POJ】3264 Balanced Lineup ——线段树 区间最值的更多相关文章
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- POJ 3264 Balanced Lineup (线段树)
Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...
- POJ - 3264 Balanced Lineup 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
- POJ3264 Balanced Lineup 线段树区间最大值 最小值
Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
随机推荐
- IEnumerable
C#基础之IEnumerable 1.IEnumerable的作用 在使用Linq查询数据时经常以IEnumerable<T>来作为数据查询返回对象,在使用foreach进行遍历时需要该对 ...
- 在Azure Cloud Service中部署Java Web App(2)
接上文. 9.在进行发布之前,需要对我们的订阅做一些设置,因为默认情况下,Azure的service end指向的是Azure global的站点,如果我们要将服务发布在Azure的中国站点,需要做下 ...
- UI产品设计流程中的14个要点
http://www.sj33.cn/digital/wyll/201404/38318.html 自从我在 Dribbble 上贴了一幅我的产品设计成果,受到了大家伙热烈的反馈,对此我深受鼓励,我决 ...
- View, Activity, Window
View, Activity, Window 2010-03-02 10:42:56| 分类: android|举报|字号 订阅 对于屏幕显示而言,整个是window,这个window里显示 ...
- Zigbee、WiFi和433MHz无线技术各有特点
Zigbee.WiFi和433MHz无线技术都属于近距离无线通讯技术,并且都使用ISM免执照频段,但它们各具特点. ZigBee的特点是低功耗.高可靠性.强抗干扰性,布网容易,通过无线中继器可以非 ...
- POJ 1562 Oil Deposits (并查集 OR DFS求联通块)
Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14628 Accepted: 7972 Des ...
- 线性表的Java实现
一.概念 对于常用的数据结构,可分为线性结构和非线性结构,线性结构主要是线性表,非线性结构主要是数和图.当n>0时,表可表示为:(a0,a1,a2,a3,…an) 1. 线性表的特征: 1.存在 ...
- DBS小结
<数据库系统原理>主要介绍的是数据库技术的基本原理.方法和应用技术. 它可以使我们能有效地使用现有的数据库管理系统和软件开发工具,掌握数据库结构的设计和数据库应用系统的开发原理. 在这里, ...
- gcc选项-g与-rdynamic的异同
摘自http://www.tuicool.com/articles/EvIzUn gcc选项-g与-rdynamic的异同 gcc 的 -g ,应该没有人不知道它是一个调试选项,因此在一般需要进行程序 ...
- Linux开发环境配置
配置JDK: tar -xzvf jdk-7u71-linux-x64.tar.gz rm -f jdk-7u71-linux-x64.tar.gz 测试:java -version 配置Grad ...