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 线段树模板题 询问区间最大最小值
这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...
随机推荐
- FastStone Capture 文件名设置小记录
我使用的FastStone Capture 的文件名设置 文件名称模板 fs$Y$M$D#####@ 起始于: [1] 重置 [ 选中] 新的一天自动重置为1
- JavaScript ---- 原型,原型链(什么是原型)
和“闭包”一样,“原型”这个概念也经常被提起. 其实这个“概念”应该和构造函数,对象放在一起讲,但是由于时间关系,先把这部分抽取出来讲.再讲这个概念时我们先大致了解下JavaScript中的“对象”. ...
- Java 导出excel进行换行
在导出excel 的时候,如果原始文字中含有 \n 字符,生成的excel中 会生成 _0040_ 字样的乱码, 如果把 \n 替换为<br/>,excel不会识别成换行符 excel 认 ...
- 解析Spring MVC上传文件
新建一个普通的maven工程 在pom.xml文件中引入相应的坐标 <?xml version="1.0" encoding="UTF-8"?> & ...
- Linux基本使用命令
一.常用命令归纳分类 课外网站 http://man.linuxde.net/ http://www.jb51.net/linux/ http ...
- VS 2015 Download
企业版:http://download.microsoft.com/download/B/8/F/B8F1470D-2396-4E7A-83F5-AC09154EB925/vs2015.ent_chs ...
- 使用jQuery函数
1选择器 1.1说明 选择器本身只是一个有特定语法规则的字符串, 没有实质用处,它的基本语法规则使用的就是CSS的选择器语法, 并对基进行了扩展,只有调用$(), 并将选择器作为参数传入才能起作用. ...
- 打造“云边一体化”,时序时空数据库TSDB技术原理深度解密
本文选自云栖大会下一代云数据库分析专场讲师自修的演讲——<TSDB云边一体化时序时空数据库技术揭秘> 自修 —— 阿里云智能数据库产品事业部高级专家 认识TSDB 第一代时序时空数据处理工 ...
- bzoj1042题解
[题意分析] 有q个询问,每次询问求一个只有四个物品的背包方案数. [解题思路] 先计算出所有面值硬币的无限背包方案数. 考虑容斥,每个限制表示选取大于di个面值ci的硬币,总方案数=0限制方案数-1 ...
- NX二次开发-创建经典工具栏UF_UI_create_toolbar
NX9+VS2012 1.打开D:\Program Files\Siemens\NX 9.0\UGII\menus\ug_main.men 找到装配和PMI,在中间加上一段 TOGGLE_BUTTON ...