poj 3246 Balanced Lineup(线段树)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 38942 | Accepted: 18247 | |
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 cowi
Lines N+2..N+Q+1: Two integers A and B (1 ≤A ≤
B ≤ N), representing the range of cows from A toB 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<stdio.h>
#include<string.h>
#define max(a,b) a>b?a:b
#define min(a,b) a<b? a:b
#define INF 99999999
#define N 50005
struct tree{
int l,r,maxi,mini;
int mid(){
return l+r>>1;
}
}tree[N<<2];
int ma=-INF,mi=INF;
void build(int l,int r,int root)
{
tree[root].l=l;
tree[root].r=r;
tree[root].maxi=-INF;
tree[root].mini=INF; //初始化最大最小值
if(l==r){ return;
}
int mid=l+r>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
}
void update(int i,int z,int root)
{ if(tree[root].l==tree[root].r){
tree[root].mini=tree[root].maxi=z;
return;
}
tree[root].maxi=max(tree[root].maxi,z);
tree[root].mini=min(tree[root].mini,z); //每次都更新最大和最小值
if(i<=tree[root].mid())update(i,z,root<<1); //这里将i下面的节点所有更新。 而i与mid 是有关系的。
else update(i,z,root<<1|1);
}
void Query(int l,int r,int root)
{
if(tree[root].mini>=mi&&tree[root].maxi<=ma)return;
if(l==tree[root].l&&r==tree[root].r){
mi=min(mi,tree[root].mini);
ma=max(ma,tree[root].maxi);
return;
}
int mid=tree[root].l+tree[root].r>>1;
if(r<=mid){
Query(l,r,root<<1);
}
else if(l>mid){
Query(l,r,root<<1|1);
}
else {
Query(l,mid,root<<1);
Query(mid+1,r,root<<1|1);
}
return ;
}
int main()
{
int n,Q,cow[200005],a,b;
int i,j,k;
while(scanf("%d%d",&n,&Q)!=EOF)
{
build(1,n,1);
for(i=1;i<=n;i++)
{
scanf("%d",&cow[i]);
update(i,cow[i],1); //对于第i个数字进行插入
} while(Q--)
{
scanf("%d%d",&a,&b);
ma=-INF;
mi=INF;
Query(a,b,1);
printf("%d\n",ma-mi);
}
}
return 0;
}
poj 3246 Balanced Lineup(线段树)的更多相关文章
- [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 线段树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 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 3246 Balanced Lineup(线段树基础)
依旧是线段树基础题 询问区间的最大值和最小值之差,只有询问,没有插入删除.继续理解基础线段树 #include <iostream> #include <algorithm> ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
随机推荐
- JS-产生随机数的几个用法!
<script> function GetRandomNum(Min,Max) { var Range = Max - Min; var Rand = Math.random(); ret ...
- Android 5.0 怎样正确启用isLoggable(一)__使用具体解释
isLoggable是什么 在Android源代码中,我们常常能够看到例如以下代码: //packages/apps/InCallUI/src/com/android/incallui/Log.jav ...
- Java实现二维码技术探讨。
Java生成二维码方法有三种: 1: 使用SwetakeQRCode在Java项目中生成二维码 http://swetake.com/qr/ 下载地址 或着http://sourceforge.j ...
- 转:java中数组与List相互转换的方法
1.List转换成为数组.(这里的List是实体是ArrayList) 调用ArrayList的toArray方法. toArray public <T> T[] toArray(T[] ...
- list/tuple/dict/set
一.list(列表) 内置类型,长度可变的有序集合,索引从0开始,索引为负数是标识从右开始取,最右边第一个是-1,以此类推.里面的元素可以是不同类型的. 1.定义:a = [] #空列表 2.获取长度 ...
- Python进阶---python strip() split()函数实战(转)
先看一个例子: >>> ipaddr = 10.122.19.10 File "", line 1 ipaddr = 10.122.19.10 ^ SyntaxE ...
- Unity UI代码自动生成
最近在做新项目跟同事讨论UI制作方案, 这里就说下根据节点来生成UI代码, 这个工具可以根据预设生成一个分布类.目前组件还不是很完善, 自己使用需要修改部分代码 组件功能如下: 1. 自动设置引用 ...
- 数字图像和视频处理的基础-第4周运动预计matlab练习题
In this problem you will perform block matching motion estimation between two consecutive video fram ...
- 【PHP】组合条件搜索SQL
前端html多个搜索条件组合 后台一个sql语句,很方便和简洁:仅提供思路. 也可以配合着进行分页操作,非常赞~
- Mac系统使用命令行快捷打开Sublime
本篇文章由:http://xinpure.com/use-command-line-shortcuts-to-open-the-mac-system-sublime/ 方法一 使用软链接 ln -s ...