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 ...
随机推荐
- 将本地jar包添加到maven中
将需要引入的jar包拷贝到maven项目的WEB-INF/lib中 在pom.xml中配置如下: <dependency> <groupId>com.xxxxx.union&l ...
- [Functional Programming Monad] Combine Stateful Computations Using A State Monad
The true power of the State ADT really shows when we start combining our discrete, stateful transact ...
- Android - Animation(二)
Android - Animation(一) 一文总结了Android中的补间动画(View Animation/Tween Animation)和帧动画(Drawable Animation/Fra ...
- 算法笔记_061:蓝桥杯练习 字串统计(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 给定一个长度为n的字符串S,还有一个数字L,统计长度大于等于L的出现次数最多的子串(不同的出现可以相交),如果有多个,输出最长的,如果仍然 ...
- vue 仿QQ 开发流程
技术客栈: vue-cli vue2 vue-router vuex axios stylus webpack2 muse-ui 1.安装脚手架 npm install -g vue-cli 2.开始 ...
- Vim快捷键整理
Vim主要分为两种模式一种是Insert模式,该模式下可以像其它文本编辑器一样正常输入字符:另一种是Normal模式,该模式下Vim监听用户的按键可以对文本进行快速修改. 想要从Insert模式切换到 ...
- 一个简单的python爬虫(转)
# -*- coding: utf-8 -*- #--------------------------------------- # 程序:百度贴吧爬虫 # 版本:0.1 # 作者:why # 日期: ...
- 转 理解与分析ios应用的崩溃报告
理解与分析ios应用的崩溃报告 源网址: http://developer.apple.com/library/ios/#technotes/tn2151/_index.html 当一个应用程序崩溃时 ...
- java 重载和多态的区别
虚拟函数表是在编译期就建立了,各个虚拟函数这时被组织成了一个虚拟函数的入口地址的数组.而对象的隐藏成员--虚拟函数表指针是在运行期--也就是构造函数被调用时进行初始化的,这是实现多态的关键. http ...
- Hive Group By 常见错误
Expression not in GROUP BY key ‘ xxx’ 遇到这么一个需求,输入数据为一个ID对应多个name,要求输出数据为ID是唯一的,name随便取一个就可以. 执行以下hiv ...