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模板题,在这 ...
随机推荐
- phpcms更换域名||外网访问本地网站
在网站在发展的过程中,很可能多次的修改域名:然后就发现我的搜索页还是之前的域名的链接地址,那么在PHPCMS V9中我们要怎么进行设置呢? 请进行以下步骤的修改: 修改/caches/config ...
- BIOS开启虚拟化
启动时根据提示按del 键按 F10 键以配置 BIOS使用箭头键滚动到“System Configuration”选择“Virtualization Technology”,然后按 Enter 键选 ...
- 2.js模式-单例模式
1. 单例模式 单例模式的核心是确保只有一个实例,并提供全局访问. function xx(name){}; Singleton.getInstance = (function(){ var inst ...
- 没有body怎么添加onload事件
<script type="text/javascript"> window.onload = function () { setup(); } </script ...
- jquery stop( ) 的用法 (转)
目的:为了 了解stop()的用法,举个例子,直观的方式看看. 实物:一个id="animater"的div包含了一段文字.(以下用animator表示实物) 动画最终的完整效果: ...
- oracle触发器 ORA-01722:invalid number 解决方法
问题在于,远程库是nvarchar2类型,本地是number类型,同步的时候有问题. create or replace trigger tri_org_department after insert ...
- 【linux】学习3
鸟哥 书的第7章 从 /home/dtest1 跳入 /home/dtest2 目录: cd ../dtest2 注意 cd后有空格 ..后无空格 特殊目录: . 代表此层目录 .. ...
- c#指定日期格式
string TripCode = "BT"+DateTime.Now.ToString("yyyyMMddHHmmss");//出差单号
- PDO(数据访问抽象层)
自带事务功能,多条sql同时执行时,如果其中一条执行失败,那么所有的都执行失败.开启了事务,可以进行回滚操作,让程序变得更安全. 1.访问不同的数据库2.自带事务功能3.防止SQL注入:分两次发送 / ...
- Linux下C语言多线程,网络通信简单聊天程序
http://www.cnblogs.com/zhuxianji/archive/2011/01/06/1928970.html