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 (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). 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 1: Two space-separated integers, N and Q
Lines 2.. N+1: Line i+1 contains a single integer that is the height of cow i 
Lines N+2.. NQ+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

Output

Lines 1.. 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

6 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0 题意:就是取最大值与最小值的差。
题解:就是线段树的基本功能里的。
 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
const int MAXN=;
char s[];
int a[MAXN]={},tree[MAXN*]={},fuck[MAXN*]={},t,num,x,n,y,q;
using namespace std;
void build(int l,int r,int p)
{
if (l==r) tree[p]=a[l];
else
{
int mid=(l+r)>>;
build(l,mid,p*);
build(mid+,r,p*+);
tree[p]=max(tree[p*],tree[p*+]);
}
}
void build_fuck(int l,int r,int p)
{
if (l==r) fuck[p]=a[l];
else
{
int mid=(l+r)>>;
build_fuck(l,mid,p*);
build_fuck(mid+,r,p*+);
fuck[p]=min(fuck[p*],fuck[p*+]);
}
}
int query(int l,int r,int p,int x,int y)
{
if (l==x&&r==y) return tree[p];
else
{
int mid=(l+r)>>;
if (y<=mid) return query(l,mid,p*,x,y);
else if(x>=mid+) return query(mid+,r,p*+,x,y);
else return max(query(l,mid,p*,x,mid),query(mid+,r,p*+,mid+,y));
}
}
int query_fuck(int l,int r,int p,int x,int y)
{
if (l==x&&r==y) return fuck[p];
else
{
int mid=(l+r)>>;
if (y<=mid) return query_fuck(l,mid,p*,x,y);
else if(x>=mid+) return query_fuck(mid+,r,p*+,x,y);
else return min(query_fuck(l,mid,p*,x,mid),query_fuck(mid+,r,p*+,mid+,y));
}
}
int main()
{
scanf("%d%d",&n,&q);
for (int i=;i<=n;i++)
scanf("%d",&a[i]);
build(,n,);
build_fuck(,n,);
while (q--)
{
scanf("%d%d",&x,&y);
printf("%d\n",query(,n,,x,y)-query_fuck(,n,,x,y));
}
}
 

poj3264 最大值与最小值的差的更多相关文章

  1. 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 d ...

  2. 求一个number数组中的最大值和最小值的差

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  3. hdu 5289 Assignment(给一个数组,求有多少个区间,满足区间内的最大值和最小值之差小于k)

    1.区间是一段的,不是断开的哟 2.代码是看着标程写的 3.枚举左端点,二分右端点流程: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L ...

  4. poj 3264 Balanced Lineup【RMQ-ST查询区间最大最小值之差 +模板应用】

    题目地址:http://poj.org/problem?id=3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0分析:标 ...

  5. 转载——JavaScript学习笔记:取数组中最大值和最小值

    转载自:http://www.w3cplus.com/javascript/calculate-the-max-min-value-from-an-array.html. 取数组中最大值 可以先把思路 ...

  6. JavaScript学习:取数组中最大值和最小值

    在实际业务中有的时候要取出数组中的最大值或最小值.但在数组中并没有提供arr.max()和arr.min()这样的方法.那么是不是可以通过别的方式实现类似这样的方法呢?那么今天我们就来整理取出数组中最 ...

  7. 【noip模拟赛6】收入计划 最大值的最小值 二分答案

    描述 高考结束后,同学们大都找到了一份临时工作,渴望挣得一些零用钱.从今天起,Matrix67将连续工作N天(1<=N<=100 000).每一天末他可以领取当天及前面若干天里没有领取的工 ...

  8. 算法进阶面试题02——BFPRT算法、找出最大/小的K个数、双向队列、生成窗口最大值数组、最大值减最小值小于或等于num的子数组数量、介绍单调栈结构(找出临近的最大数)

    第二课主要介绍第一课余下的BFPRT算法和第二课部分内容 1.BFPRT算法详解与应用 找到第K小或者第K大的数. 普通做法:先通过堆排序然后取,是n*logn的代价. // O(N*logK) pu ...

  9. C语言 · 最大值与最小值计算

    输入11个整数,计算它们的最大值和最小值. 样例输入 0 1 2 3 4 5 6 7 8 9 10 样例输出 10 0   #include<stdio.h> int main(){ ]; ...

随机推荐

  1. macaca 测试web(2)

    还是以博客园登录为例, 可以查看上一篇上一篇 这次只测试登录,使用ddt的数据驱动,这里我博客也有记录 地址 其实我在uiautomator +python 实现安卓自动化 一文当中我已经使用过ddt ...

  2. live事件的替代方法on的使用注意事项

    根据jQuery的官方描述,live方法在1.7中已经不建议使用,在1.9中删除了这个方法.并建议在以后的代码中使用on方法来替代. on方法可以接受三个参数:事件名.触发选择器.事件函数. 需要特别 ...

  3. C++ operator bool

    雕虫小技: #include <iostream> struct A{ operator bool(){ return false; } }; int main() { A a{}; if ...

  4. win10下移动硬盘位置不可用无法访问

    win10下移动硬盘位置不可用无法访问 网上搜索得到的答案是: 请参考以下步骤解决: 1.按Windows+R输入"CHKDSK H: /F /R"(H:是硬盘所在盘符./R 找到 ...

  5. 实现点击后创建div,若对div2秒无操作则将div隐藏,鼠标移上div让它不隐藏,移出div超过两秒则div隐藏

    $('.addLabel').on('click', function () { setTimeout(function(){ if(hoverflag==0){ $(".labelHide ...

  6. 在数组a中,a[i]+a[j]=a[k],求a[k]的最大值,a[k]max——猎八哥fly

    在数组a中,a[i]+a[j]=a[k],求a[k]的最大值,a[k]max. 思路:将a中的数组两两相加,组成一个新的数组.并将新的数组和a数组进行sort排序.然后将a数组从大到小与新数组比较,如 ...

  7. bootstrap导航条+模态对话框+分页样式

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. Java GUI+mysql+分页查询

    1.要求 : 创建一个学生信息管理数据库 2.实现分页查询 代码如下: a)学生实体类: /** * @author: Annie * @date:2016年6月23日 * @description: ...

  9. 团队作业9——展示博客(Beta版本)

    展示博客 1.团队成员的简介和个人博客地址,团队的源码仓库地址. 何琴琴(http://www.cnblogs.com/vviane/): 领导项目进行,协调各队员之间的矛盾合作,负责测试与需求分析. ...

  10. 团队作业八—第二次团队冲刺(Beta版本) 第 2 天

    一.每个人的工作 (1) 昨天已完成的工作 昨天的工作主要是一些界面的设计,我们顺利完成了复杂模式题目数目界面.复杂模式做题界面.结果统计界面的具体代码编写,和一些细节的完善.还有日常冲刺博客的编辑. ...