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. django文件上传

    -------------------上传图片-------------------1.model中定义属性类型为models.ImageField类型 pic=models.ImageField(u ...

  2. django模板(过滤器)

    -------------------django内建的过滤器-------------------1.add 使用形式为:{{ value | add: "2"}} 意义:将va ...

  3. javaScript手记(01)

    --------------------javaScript基础1.嵌入页面的方式 1.行间事件(主要用于事件): <input type="button" name=&qu ...

  4. DFS和BFS(无向图)Java实现

    package practice; import java.util.Iterator; import java.util.Stack; import edu.princeton.cs.algs4.* ...

  5. 一、Nginx安装手册

    1 nginx安装环境 nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境. gcc 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有g ...

  6. ireport导出中英文pdf

    准备: 报表开发工具:ireport 5.6.0 报表框架: jasperreport5.6.0 1.中文格式设置pdf fontname.isembedded.pdfencoding pdf fon ...

  7. CAP和BASE理论

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt370 1. CAP理论 2000年7月,加州大学伯克利分校的Eric Bre ...

  8. Java的构造器

    初始化和清理是涉及安全的两个问题.C++和Java都引入了构造器(constructor)的概念,这是一个在创建对象时被自动调用的特殊方法. 可以假想为编写的每个类都定义一个initialize()方 ...

  9. H5-html基础

    什么是 HTML? HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML 不是一种编程语言,而是一种标记语言 (ma ...

  10. jQuery高级Ajax

    .load();加载远程的HTML文件代码,并插入到指定的DOM节点中.可以只传入一个参数,表示加载一个静态的HTML代码片段. $("#div1").load("loa ...