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 ...
随机推荐
- Java笔记18:JUnit单元测试
1 从http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22junit%22%20AND%20a%3A%22junit%22 上下载最新的junit包. ...
- RocketMQ概念整理
DefaultMessageStore 消息的存储和提取. 相对重要的两个方法: 消息存储 PutMessageResult putMessage(MessageExtBrokerInner msg) ...
- centos 7安装 navicat
下载地址: http://download.navicat.com/download/navicat111_mysql_en.tar.gz 下载后copy到指定安装文件夹 [hcr@localhost ...
- 建站笔记1:centos6.5下安装mysql
近期买了个域名,想要玩玩自己建站点:接下来遇到的问题都会一次记录下来.以备自己以后复习查看: 首先建站方案选择: wordPress +centos6.5 +mysql; server买的:搬瓦工最低 ...
- 在LoadRunner中查找和替换字符串
参考<Search & Replace function for LoadRunner>: http://ptfrontline.wordpress.com/2009/03/13/ ...
- 学习EF之CodeFirst二(数据库对应映射)
在上一篇文章我们简单通过一个实例完成对CodeFirst的理解,我们通过实体生成数据库里的表和字段,虽然有一些默认的配置生成规定,但其实我们可以能过对实体进一步控制从而对生成的表字段进行更加符合我们要 ...
- ES6 set 应用场景
1.数组去重 let arr = [3, 5, 2, 2, 5, 5]; let unique = [...new Set(arr)]; // [3, 5, 2] 2.并集(Union).交集(Int ...
- Android Exception 12(has leaked ServiceConnection)
09-09 15:12:31.154: E/ActivityThread(18855): Activity com..xxx.xx.act.LoadingAct has leaked ServiceC ...
- FAT AP v200R005 配置二层透明模式(web&命令行,开局)
背景: vlan123:用户业务vlan,192.168.1.0/24 Vlan2001:管理vlan,172.168.129.0/24 vlan1:默认vlan,不建议使用. 注意事项: 配置服务集 ...
- 批量删除git分支
本篇文章由:http://xinpure.com/bulk-delete-git-branching/ 批量删除git分支 使用 git 时候,经常会发现,不知不觉就创建了大量的分支.那么,麻烦事就来 ...