POJ-3264-Balanced Lineup-线段树模板题-查询区间内最大值和最小值之差
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 ( ≤ Q ≤ ,) potential groups of cows and their heights ( ≤ height ≤ ,,). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
input
Line : Two space-separated integers, N and Q.
Lines .. N+: Line i+ contains a single integer that is the height of cow i
Lines N+.. N+ Q+: Two integers A and B ( ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
output
Lines .. Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.
Sample Input
Sample output
题意:
给出n和q,给出n头奶牛的身高,q次询问,每次询问给出区间a、b,求出区间内的最大值和最小值之差
这里注意一下给出的样例:(解释一下输出)
给出的样例为1、7、3、4、2、5,表示区间1、2、3、4、5、6
思路:
线段树的模板题,求出区间内的最大值和最小值之差也就是查询已经建立好的线段树的最大值和最小值之差
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<cmath>
#include<queue>
#include<stdlib.h>
typedef long long ll;
using namespace std;
const int N=; //需要开到四倍空间
int a[*N];//max
int b[*N];//min //每个父节点记录的是它下面的两个节点的最大值
void build(int L,int R,int i)
{
if(L==R)
{
scanf("%d",&a[i]);
b[i]=a[i];
return;
}
int mid=(L+R)>>;
build(L,mid,i<<);
build(mid+,R,i<<|);
a[i]=max(a[i<<],a[i<<|]);
b[i]=min(b[i<<],b[i<<|]);
//pushup(i)////每次传的时候把根节点也往下去寻找最大值
////比较其左右两个节点的大小,取最大值
//看题目给的需要求什么
} //query(aa,bb,1,n,1)
int querymax(int left,int right,int L,int R,int i)//a求区间最小值
{
if(left<=L&&right>=R)
return a[i];
int mid=(L+R)>>;
int ans=-;
if(left<=mid)
ans=max(ans,querymax(left,right,L,mid,i<<));
// else
if(right>mid)
ans=max(ans,querymax(left,right,mid+,R,i<<|));
return ans;
} //query(aa,bb,1,n,1)
int querymin(int left,int right,int L,int R,int i)//b求区间最大值
{
if(left<=L&&R<=right)
return b[i];
int mid=(L+R)>>;
int ans=0x3f3f3f3f;
if(left<=mid)
ans=min(ans,querymin(left,right,L,mid,i<<));
// else
if(right>mid)
ans=min(ans,querymin(left,right,mid+,R,i<<|));
return ans;
} int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
// memset(a,0,sizeof(a));
build(,n,);//传入最左端点,最右端点,根节点进行建树
//建树的过程中输入每一个节点
for(int i=; i<m; i++)
{
int aa,bb;
scanf("%d %d",&aa,&bb);
int kk=querymax(aa,bb,,n,)-querymin(aa,bb,,n,);
printf("%d\n",kk);
}
}
return ;
}
树状数组:
这个代码思路看注释的话好理解,但是代码不好理解
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std; int a[],maxx[],minn[]; int lowbit(int x)
{
return x&(-x);
} int w(int L,int R)
{
int min1=a[R];
int max1=a[R];
while(L!=R)
{
for(R--; R-lowbit(R)>=L; R=R-lowbit(R))
{
min1=min(min1,minn[R]);
max1=max(max1,maxx[R]);
}
max1=max(max1,a[R]);
min1=min(min1,a[R]); }
return max1-min1; } int main()
{
std::ios::sync_with_stdio(false);
int n,q;
cin>>n>>q;
for(int i=; i<=n; i++)
{
cin>>a[i];
// update(i,a[i]);
maxx[i]=minn[i]=a[i];
for(int j=; j<lowbit(i); j*=)
{
maxx[i]=max(maxx[i],maxx[i-j]);
minn[i]=min(minn[i],minn[i-j]);
}
}
for(int i=; i<q; i++)
{
int aa,bb;
cin>>aa>>bb;
cout<<w(aa,bb)<<endl;
}
return ;
}
这个代码也A了,可以参考这个,类似树状数组的原模板,好理解,细节上变动一点就可以了
POJ-3264-Balanced Lineup-线段树模板题-查询区间内最大值和最小值之差的更多相关文章
- 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 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- 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 (线段树)
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 线段树模板题 询问区间最大最小值
这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...
随机推荐
- vue cli3 vue.config配置
跳地址:https://cli.vuejs.org/zh/config/#publicpath
- 谷歌浏览器控制台出现 Unchecked runtime.lastError: The message port closed before a response was received. 的报错
错误截图: 解决:经过网上搜索说是浏览器扩展程序的问题,把那个扩展程序删除或者禁用就可以了
- Delphi locate函数
使用ADO等数据控件的时候,经常会用到 locate 函数,在结果数据集中查询和定位,下面介绍一下: (一) function Locate(const KeyFields: String; cons ...
- NX二次开发-UFUN创建表达式UF_MODL_create_exp无TAG
NX9+VS2012 #include <uf.h> #include <uf_modl.h> UF_initialize(); //创建一个新的表达式,无TAG UF_MOD ...
- python入门 集合(四)
集合 集合是一个无序的不重复元素序列,可以迭代,也可以修改.集合迭代的时候元素是随机的. 集合通常用来 membership testing, 去重, 也可以用来求交集并集补集. 介绍一下如何创建集合 ...
- mysql中的Date日期格式的问题:只有日期没有时间及格式化时间
只有日期没有时间,把xml中的date改为timestamp 格式化最简单的方法:@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", time ...
- ECMAScript1.1 js书写位置 | 声明变量 | 基本数据类型 | 数据类型转换 | 操作符 | 布尔类型的隐式转换
js书写位置 由于在写css样式时使用的时双引号,所以我们在写js代码时建议使用单引号(‘’)! 行内式 <input type="button" value="点 ...
- adb shell top 使用
adb shell top 一.其中相关参数: >adb shell top -h Usage: top [ -m max_procs ] [ -n iterations ] [ -d del ...
- 跳一跳外挂的python实现--OpenCV步步精深
去我的个人网站看看吧 http://opencvblog.com/跳一跳外挂-python实现/ 都在这里啦
- HTTP协议:响应消息
一.请求消息:客户端发送给服务器端的数据 数据格式: 1.请求行 2.请求头 3.请求空行 4.请求体 二.响应消息:服务器端发送给客户的数据 数据格式: 1.响应行: 1.组成:协议/版本 响应状态 ...