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-线段树模板题-查询区间内最大值和最小值之差的更多相关文章

  1. POJ 3264 Balanced Lineup 线段树 第三题

    Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...

  2. POJ 3264 Balanced Lineup 线段树RMQ

    http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...

  3. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  4. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  5. poj 3264 Balanced Lineup (RMQ算法 模板题)

    RMQ支持操作: Query(L, R):  计算Min{a[L],a[L+1], a[R]}. 预处理时间是O(nlogn), 查询只需 O(1). RMQ问题 用于求给定区间内的最大值/最小值问题 ...

  6. 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 ...

  7. POJ - 3264 Balanced Lineup 线段树解RMQ

    这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...

  8. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  9. POJ - 3264 线段树模板题 询问区间最大最小值

    这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...

随机推荐

  1. Java面向对象的特征一:封装性

    1.4 面向对象的特征一:封装性 当创建了类的对象以后,如果直接通过"对象.属性"的方式对相应的对象属性赋值的话,可能会出现不满足实际情况的意外,我们考虑不让对象来直接作用属性,而 ...

  2. jQuery的两把利器

    1 jQuery核心函数 * 简称: jQuery函数($/jQuery) * jQuery库向外直接暴露的就是$/jQuery * 引入jQuery库后, 直接使用$即可 * 当函数用: $(xxx ...

  3. 字符串dp——牛客多校第五场G

    比赛的时候脑瘫了没想出来..打多校以来最自闭的一场 显然从s中选择大于m个数组成的数必然比t大,所以只要dp求出从s中选择m个数大于t的方案数 官方题解是反着往前推,想了下反着推的确简单,因为高位的数 ...

  4. 几道noip2018提高组初赛的题

    以下做法来均自llj @Nicodafagood 一.单项选择题 7. 在一条长度为 1 的线段上随机取两个点,则以这两个点为端点的线段的期望 长度是( ).A. 1 / 2B. 1 / 3C. 2 ...

  5. NX二次开发-UFUN获取图纸视图最大边界和视图中心点UF_DRAW_ask_view_borders

    NX9+VS2012 //获得视图的最大边界 ]; UF_DRAW_ask_view_borders(view_tag[j], view_borders); //获得视图原点 ]; ViewOrigi ...

  6. NX二次开发-NXOPEN工程图导出CAD图纸DxfdwgCreator *dxfdwgCreator1;

    没有什么可以看的,NXOPEN直接录制一下导出CAD就可以了.录制出来自己挑需要的代码拿过来改一下. NX9+VS2012 #include <NXOpen/Part.hxx> #incl ...

  7. C/C++:Windows编程—调用DLL程序的2种方法(转载)

    文章为转载,原文出处https://blog.csdn.net/qq_29542611/article/details/86618902 前言先简单介绍下DLL.DLL:Dynamic Link Li ...

  8. 源码安装zabbix-oracle

    源码安装zabbix_agent4.0.3   1.源码包下载地址:https://www.zabbix.com/download_sources 2.下载完后上传在任意目录用root用户创建以下脚本 ...

  9. 2018-06-05(thinking in java)

    1:类只能有2种访问权限,一种是不写访问权限:默认包访问权限,另一种是public.且类只能有一个pubic 2:方法有4种访问权限,pubic(都可以访问),protexted(继承访问权限),pr ...

  10. JS中 reduce() 的用法

    过去有很长一段时间,我一直很难理解 reduce() 这个方法的具体用法,平时也很少用到它.事实上,如果你能真正了解它的话,其实在很多地方我们都可以用得上,那么今天我们就来简单聊聊JS中 reduce ...