poj 3264:Balanced Lineup(线段树,经典题)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 32820 | Accepted: 15447 | |
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 <iostream>
#include <stdio.h>
using namespace std; #define MAXN 50000
#define INF 99999999
int MaxV,MinV; struct Node{
int L,R;
int ma,mi; //区间[L,R]的最大值和最小值
}a[MAXN*]; int Max(int x,int y)
{
return x>y?x:y;
} int Min(int x,int y)
{
return x<y?x:y;
} void Build(int d,int l,int r) //建立线段树
{
if(l==r){ //找到叶子节点
scanf("%d",&a[d].ma);
a[d].mi = a[d].ma;
a[d].L = l;
a[d].R = r;
return ;
} //初始化当前节点的信息
a[d].L = l;
a[d].R = r; //建立线段树
int mid = (l+r)>>;
Build(d<<,l,mid);
Build(d<<|,mid+,r); //更新当前节点的信息
a[d].ma = Max(a[d<<].ma,a[d<<|].ma);
a[d].mi = Min(a[d<<].mi,a[d<<|].mi);
} void Query(int d,int l,int r) //查询区间[l,r]的最大值和最小值的差
{
if(a[d].ma<MaxV && a[d].mi>MinV) //优化,如果当前区间的最大值和最小值在MaxV和MinV之间,则没必要继续递归该区间。能快100MS
return ;
if(a[d].L==l && a[d].R==r){ //找到终止节点
MaxV = Max(MaxV,a[d].ma);
MinV = Min(MinV,a[d].mi);
return ;
} int mid = (a[d].L+a[d].R)/;
if(mid>=r){ //左孩子找
Query(d<<,l,r);
}
else if(mid<l){ //右孩子找
Query(d<<|,l,r);
}
else{ //左孩子、右孩子都找
Query(d<<,l,mid);
Query(d<<|,mid+,r);
}
} int main()
{
int n,q,A,B;
scanf("%d%d",&n,&q);
Build(,,n);
while(q--){ //q次询问
scanf("%d%d",&A,&B);
MaxV = -INF;
MinV = INF;
Query(,A,B);
printf("%d\n",MaxV-MinV);
}
return ;
}
Freecode : www.cnblogs.com/yym2013
poj 3264:Balanced Lineup(线段树,经典题)的更多相关文章
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- 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 Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- 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),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- poj 3264 Balanced Lineup(RMQ裸题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 43168 Accepted: 20276 ...
- poj 3264 Balanced Lineup (RMQ算法 模板题)
RMQ支持操作: Query(L, R): 计算Min{a[L],a[L+1], a[R]}. 预处理时间是O(nlogn), 查询只需 O(1). RMQ问题 用于求给定区间内的最大值/最小值问题 ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
随机推荐
- redis配置文件redis.conf参数说明
redis配置文件redis.conf参数说明 (2013-01-09 21:20:40)转载▼ 标签: redis配置 redis.conf 配置说明 杂谈 分类: nosql # By defau ...
- 【GoLang】GoLang 的流程与函数
003.GO流程与函数 1 概述 1.1 Go中流程控制分三大类:条件判断,循环控制和无条件跳转 2 流程 2.1 if 2.1.1 if条件判断语句中不需要括号 2.1.2 条件判断语句里面允许声明 ...
- 【GoLang】GoLang 遍历 map、slice、array方法
代码示例: map1 := make(map[string]string) map1["a"] = "AAA" map1["b"] = &q ...
- Maven学习总结
转载至:http://www.cnblogs.com/xdp-gacl/p/3498271.html 一 入门 一.Maven的基本概念 Maven(翻译为"专家","内 ...
- 《oracle每天一练》Oracle之物化视图
相关帖子思考和跟踪 本文转自Ronger 物化视图是一种特殊的物理表,“物化”(Materialized)视图是相对普通视图而言的.普通视图是虚拟表,应用的局限性大,任何对视图的查询,Oracle都实 ...
- 使用SharePoint 2010 母版页
SharePoint 2010母版页所用的还是ASP.NET 2.0中的技术.通过该功能,实现了页面框架布局与实际内容的分离.虽然在本质上自定义母版页的过程和以前版本的SharePoint大致相同,但 ...
- 手动fsck修复
[转自]http://blog.chinaunix.net/uid-26719405-id-3781541.html 由于硬盘常年读写,系统会造成系统文件损坏,导致重启后无法登陆到系统, fsck不仅 ...
- delphi XE5下安卓开发技巧
delphi XE5下安卓开发技巧 一.手机快捷方式显示中文名称 project->options->Version Info-label(改成需要显示的中文名即可),但是需要安装到安卓手 ...
- java web 学习 --第二天(Java三级考试)
第一天的学习在这http://www.cnblogs.com/tobecrazy/p/3444474.html 2.jsp 基础知识 Jsp页面中的Java脚本主要有3部分:声明(Declaratio ...
- Python: 安装BeautifulSoup4
python3.4.3 安装BeautifulSoup4: 使用pip install 安装: 在命令行cmd之后输入,pip install BeautifulSoup4 BeautifulSoup ...