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模板题,在这 ...
随机推荐
- Droid4x安装busybox
下载Busybox ...
- 腾讯云环境配置之PHP5.6.3 + redis扩展 稳定版
腾讯云环境配置之PHP5.6.3 + redis扩展 稳定版 时间:2015-01-18 01:41来源:linux.it.net.cn 作者:IT #由于上文装过yum groupinstall ...
- 【leetcode】N-Queens
N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...
- POJ 1917
http://poj.org/problem?id=1917 poj的字符串的一道水题. 题意么无关紧要, 反正输出的第一行就是把那个<>去掉,s1<s2>s3<s4&g ...
- linux shell 中 printf 与 echo的区别
echo echo是非常常用的shell命令.参数如下: -e:打开反斜杠字符backslash-escaped的解析,即对/n,/t等字符进行解析,而不视之为两个字符 -E:关闭反斜杠字符 ...
- dubbo main方法启动
public static void main(String[] args) { com.alibaba.dubbo.container.Main.main(args); } 以上就可以简单本地启动了
- 没有body怎么添加onload事件
<script type="text/javascript"> window.onload = function () { setup(); } </script ...
- MySQL 获得当前日期时间\时间戳 函数 ( 转自传智播客)
MySQL 获得当前日期时间 函数 1.1 获得当前日期+时间(date + time)函数:now() mysql> select now(); +-------+ | now() | +-- ...
- IOS-UIIamge初始化的几种方法的比较
一.imageNamed——方法介绍imageNamed:是UIImage的一个类方法,它做的事情比我们看到的要稍微多一些.它的加载流程如 下:1.系统回去检查系统缓存中是否存在该名字的图像,如果存在 ...
- IOS 去掉导航栏(UINavigationBar)下方的横线
这是导航栏的问题,将下边的代码放在 viewWillAppear 方法中就可以实现效果: - (void)viewWillAppear:(BOOL)animated{ // Called when ...