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. Ext.grid.EditorGridPanel保存

    用get方法传递编辑的数据会出现乱码,解决get乱码的方法就是encodeURI(param),然后在后台转码: String strJson =  new String(request.getPar ...

  2. Web.py 框架学习笔记 - ctx

    摘要: ctx用于存取web请求的环境变量,基于ThreadedDict类进行实例化.ThreadedDict类可实例化字典类型的对象,该对象某些属性可用于存取处理线程的id. 这样字典化实例的线程池 ...

  3. mysql 数据库安装步骤个人总结

    1.mysql-5.7.19-winx64.zip(此为免安装版,318兆左右,还有一种是安装版,380兆左右mysql-installer-community-5.7.19.0.msi)将此安装包解 ...

  4. matlab-常用函数(1)

    rng('shuffle'): matlab help文档中的解释 rng('shuffle'): seeds the random number generator based on the cur ...

  5. 谈谈.NET,Java,php

    开通博客后,一直都是转点别的朋友写的有意思的博文,今天我来写我在博客园的第一篇文章,说的不对的地方请你指正.希望本文能为一些准备学习编程的朋友有一点帮助. 开发桌面程序一直都是c语言,c++的天下,因 ...

  6. Http get方式url参数长度以及大小

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp69 众所周知, 传递小量参数(在没有其他原因,例如隐藏参数值的情况下)推 ...

  7. yum安装mariadb-galera同步

    节点             ip地址      hostname                            系统版本   程序版本 node1 10.4.90.90 mysql1 db1 ...

  8. Linux-chown命令(1)

    chown  [chang owner]:更改文件的属主,也就是指定文件的拥有者改为另一个指定的用户或组. 命令格式: chown [选项]... [用户][:[组]] 文件... 例子:  sudo ...

  9. 【Java并发编程】之六:Runnable和Thread实现多线程的区别(含代码)

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/17161237 Java中实现多线程有两种方法:继承Thread类.实现Runnable接口 ...

  10. sudoku--SE第二次作业

    git传送门 编译环境: windows10.vs2017 所用语言: c++ 首先作为一个晚上闭眼的玩家,我先来讲一下我的心路历程: 最开始接到作业的时候心里是拒绝的,刚出了一趟小远门就这样,就很难 ...