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. 解决vi显示文件不能全屏的问题

    https://blog.csdn.net/ly890700/article/details/52735092 docker外:   vi ~/.vimrc

  2. 使用Git 上传文件到云端(版本库)

    第一步:本地初始化Git版本库 git init 第二步:链接码云(云端) git remote add orgin "你的远程仓库地址"(复制链接后结尾是.git,如果没有记得加 ...

  3. SpringMVC 拦截器原理

    前言 SpringMVC 拦截器也是Aop(面向切面)思想构建,但不是 Spring Aop 动态代理实现的, 主要采用责任链和适配器的设计模式来实现,直接嵌入到 SpringMVC 入口代码里面. ...

  4. jQuery-介绍 加载 选择器 样式操作 属性操作 绑定click事件

    jQuery - 介绍 加载 选择器 样式操作 属性操作 绑定click事件 注意:以下部分问题不能实现效果,因该是单词拼写错误(少个t)或者没有加引号(“swing”)... jquery介绍 jQ ...

  5. delphi窗体透明但上面的控件不透明怎么实现

    我不知道LAZARUS是什么玩意.纯用DELPHI的话.procedure TForm1.FormCreate(Sender: TObject);var mStyle, mExStyle: Longi ...

  6. NX二次开发-UFUN将建模绝对空间中的点映射到工程图坐标UF_VIEW_map_model_to_drawing

    #include <uf.h> #include <uf_ui.h> #include <uf_draw.h> #include <uf_view.h> ...

  7. Codeforces Round #525 D - Ehab and another another xor problem /// 构造

    题目大意: 本题有两个隐藏起来的a b(1<=a,b<=1e30) 每次可 printf("? %d %d\n",c,d); 表示询问 a^c 与 b^d 的相对大小 ...

  8. YARN设计思路

  9. Android开发 View_自定义圆环进度条View

    前言 一个实现,空心圆环的自定义View,已经封装完好,可以直接使用. 效果图 代码 import android.content.Context; import android.graphics.C ...

  10. JavaScript - DOM相关

    DOM节点分类 ( node ) : 元素节点 ( element node ) 属性节点 ( attribute node ) 文本节点 ( text node) DOM的相关操作 : 1. 查询元 ...