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. Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实 现)interface(接口

    匿名的内部类是没有名字的内部类.不能extends(继承) 其它类,但一个内部类可以作为一个接口,由另一个内部类实现

  2. h5 app 设置全屏

    h5 app的全屏和沉浸式状态栏是不一样的 全屏模式 常见使用场景:如果页面是全屏游戏,一般会直接让状态栏消失,也就是页面全屏.webview高度全屏了,状态栏没有了.写法: 终端支持:没有终端类型限 ...

  3. SpringBoot 之 Mybatis 逆向工程

    今天给大家介绍在 spring- boot 项目中如何使用 maven 插件逆向工程生成 Mybatis 代码. pom.xml 添加依赖和插件 <dependency> <grou ...

  4. css属性大全(基础篇)

      什么是CSS? CSS全称为Cascading Style Sheets,中文翻译为“层叠样式表”,简称CSS样式表,所以称之为层叠样式表(Cascading Stylesheet)简称CSS.在 ...

  5. zookeeper基本概述

    zookeeper是一个分布式的协调服务框架 其本质是一个分布式的小文件存储系统,可以存储一些小的文件,官方建议每个小文件不要超过一兆 zk一般都是装奇数台,便于zk内部的一些投票选举 leader: ...

  6. NOIp2018集训test-10-4/test-10-5 (联考四day1/day2)

    这个day1稍微有点毒瘤吧?? DAY1 排列 以前总是把day1t1想太复杂了翻车,差不多往正解的方向想了一下感觉不可能这么复杂这可是noipday1t1啊一定有非常简单的方法然后翻车了?? 题目转 ...

  7. Python内置的一个用于命令项选项与参数解析的模块argparse

    一.argparse简单使用 我们先来看一个简单示例.主要有三个步骤: 创建 ArgumentParser() 对象 调用 add_argument() 方法添加参数 使用 parse_args() ...

  8. 在WinDBG中查看内存的命令

    当我们在调试器中分析问题时, 经常需要查看不同内存块的内容以分析产生的原因, 并且在随后验证所做出的假设是否正确. 由于各个对象的状态都是保存在内存中的, 因此内存的内容也就相当于对象的状态. d命令 ...

  9. ionic-CSS:ionic checkbox(复选框)

    ylbtech-ionic-CSS:ionic checkbox(复选框) 1.返回顶部 1. ionic checkbox(复选框) ionic 里面的 Checkbox 和普通的 Checkbox ...

  10. 20140422 ALT+F8 四个强制类型转换

    一.static_cast, dynamic_cast, const_cast http://www.cnblogs.com/chio/archive/2007/07/18/822389.html h ...