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模板题,在这 ...
随机推荐
- HDU——PKU题目分类
HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...
- NDK学习二: NDK目录结构
NDK目录结构 NDK下载好之后目录结构如下: 目录名 描述 build 存放和编译相关的脚本文件,最外面的ndk-build就是调用该目录下的makefile文件,其中mak ...
- Hexo
Hexo Hexo is a fast, simple & powerful blog framework powered by Node.js.
- poj 1035
http://poj.org/problem?id=1035 poj的一道字符串的水题,不难,但就是细节问题我也wa了几次 题意就是给你一个字典,再给你一些字符,首先如果字典中有这个字符串,则直接输出 ...
- C语言也能干大事1
今天看了个视频,叫C语言也能干大事,写了第一个WIN项目的代码,感觉特别好,就像以前刚刚学会写C语言一样, 然后就恶搞出一个东西,最后的结果就是这个东西退出不了了
- html表单样式, table隔行高亮, 长字符串自动换行
2016年1月14日 11:16:54 星期四 效果图: html: <!DOCTYPE html> <html lang="en"> <head&g ...
- web.config中配置页面出错后跳转指定错误页面
每当用户访问错误页面时,会出现不友好的404错误,所以为了防止这种不友好,我们在web.config中的<system.web>节点下配置 <customErrors>,在出现 ...
- python2.x和3.x的区别
这个星期开始学习Python了,因为看的书都是基于 Python2.x,而且我安装的是Python3.1,所以书上写的地方好多都不适用于Python3.1,特意在Google上search了一下 3. ...
- HTML超链接
打开网页在 想要查看的位置右键单击 审查元素 则可以查看代码 点击图片右键单独打开 则可以查看图片位置 一.超链接 a标签 <a href="地址"> ...
- cordova android platform cordova build 遇到的问题
版权声明:本文为博主原创文章,未经博主允许不得转载. 一.下载gradle-2.2.1-all.zip不成功,一直在下载,卡在这个downloading http://services.gradle. ...