Description

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..N+Q+1: Two integers A and B (1 ≤ ABN), 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

Source

 
 
线段树 处理该问题   存储区间 最大值与最小值
 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#define INF 0xfffffff
using namespace std;
struct tree
{
int L;
int R;
int minv;
int maxv;
int mid()
{
return (L+R)/;
}
}tree[];
int maxv=-INF;
int minv=INF;
void buildtree(int root,int L,int R)
{
tree[root].L=L;
tree[root].R=R;
tree[root].minv=INF;
tree[root].maxv=-INF;
if(L!=R)
{
buildtree(*root+,L,(L+R)/);
buildtree(*root+,(L+R)/+,R);
}
}
void inse(int root,int i,int v)
{
if(tree[root].L==tree[root].R)
{
tree[root].minv=v;
tree[root].maxv=v;
return ;
}
tree[root].minv=min(tree[root].minv,v);
tree[root].maxv=max(tree[root].maxv,v);
if(i<=tree[root].mid())
inse(*root+,i,v);
else
inse(*root+,i,v);
}
void query(int root ,int s,int e)
{
if(tree[root].minv>minv&&tree[root].maxv<maxv)
return ;
if(tree[root].L==s&&tree[root].R==e)
{
minv=min(minv,tree[root].minv);
maxv=max(maxv,tree[root].maxv);
return ;
}
if(e<=tree[root].mid())
query(*root+,s,e);
else if(s>tree[root].mid())
query(*root+,s,e);
else
{
query(*root+,s,tree[root].mid());
query(*root+,tree[root].mid()+,e);
}
}
int main()
{
int n,q;
int re;
int ss,ee;
scanf("%d%d",&n,&q);
buildtree(,,n);
for(int i=; i<=n; i++)
{
//cout<<"**********"<<endl;
scanf("%d",&re);
inse(,i,re);
}
for(int j=; j<=q; j++)
{
scanf("%d%d",&ss,&ee);
minv=INF;
maxv=-INF;
query(,ss,ee);
printf("%d\n",maxv-minv);
} return ;
}
 

poj 3264 线段树 求区间最大最小值的更多相关文章

  1. POJ3264(线段树求区间最大最小值)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 41162   Accepted: 19327 ...

  2. 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

    原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...

  3. xdoj-1324 (区间离散化-线段树求区间最值)

    思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i]  覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...

  4. hdu 1754 I Hate It (线段树求区间最值)

    HDU1754 I Hate It Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u D ...

  5. POJ - 3264 线段树模板题 询问区间最大最小值

    这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...

  6. POJ 3264 线段树入门解题报告

    题意:给n个值, Q次询问, 每次询问给定一个区间, 要求输出该区间最大最小值之差 思路:暴力的话每次询问都要遍历多次for循环一定会超时, 用线段树记录区间的信息(左边界右边界, 该区间最大值最小值 ...

  7. 【线段树求区间第一个不大于val的值】Lpl and Energy-saving Lamps

    https://nanti.jisuanke.com/t/30996 线段树维护区间最小值,查询的时候优先向左走,如果左边已经找到了,就不用再往右了. 一个房间装满则把权值标记为INF,模拟一遍,注意 ...

  8. POJ——3264线段树

    题目: 输入两个数(m,n),m表示牛的头数,n表示查询的个数.查询时输入两个数(x,y),表示查询范围的起始值和终止值,查询结果是,这个区间内牛重量的最大值减去牛重量的最小值,数量级为1000,00 ...

  9. 滑动窗口(poj,线段树维护区间最值)

    题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...

随机推荐

  1. VS2010静态编译生成.exe可执行文件

    VS2010静态编译生成的.exe可执行文件,可以在其他未安装VS2010的电脑直接运行. 静态编译:就是在编译可执行文件的时候,将可执行文件需要调用的对应动态链接库(.so)中的部分提取出来,链接到 ...

  2. MongoDB Aggregate Methods(2) MonoDB 的 3 种聚合函数

    aggregate(pipeline,options) 指定 group 的 keys, 通过操作符 $push/$addToSet/$sum 等实现简单的 reduce, 不支持函数/自定义变量 g ...

  3. html中offsetTop、clientTop、scrollTop、offsetTop

    HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth scrollHeight: 获取对象的滚动高度. scrollLeft:设置或获取位于对 ...

  4. 使用plsql连接别人的oracle(转)

    文章来源:http://www.linuxidc.com/Linux/2013-04/82738.htm oracle服务有时候我们觉得太大,所以我们只需要在本机上装一个oracle客户端和plsql ...

  5. spark1.3.1安装和集群的搭建

    由于越来越多的人开始使用spark计算框架了,而且spark计算框架也是可以运行在yarn的平台上,因此可以利用单个集群,运行多个计算框架.这是一些大公司都是这么干的.好了,下面讲一下spark1.3 ...

  6. 2016-1-15 抽屉效果实现demo

    // // ViewController.m // 抽屉 // // Created by Mac on 16/1/15. // Copyright © 2016年 Mac. All rights r ...

  7. sql 如何把查询得到的结果如何放入一个新表中

    如何把这个查询到的结果放到一张新表中? 2014-03-13 15:26   提问者采纳   表已经存在:insert into 表名 (列名1... 列名n) select 列名1....列名n f ...

  8. Java异常机制

    Java异常分类 异常表明程序运行发生了意外,导致正常流程发生错误,例如数学上的除0,打开一个文件但此文件实际不存在,用户输入非法的参数等.在C语言中我们处理这类事件一般是将其与代码正常的流程放在一起 ...

  9. What is the difference Apache (Http Server) and Tomcat (Servlet Container)

    The Apache Project The Apache Project is a collaborative software development effort. Its goal is to ...

  10. LeetCode----Copy List with Random Pointer 深度拷贝,浅度拷贝,Lazy拷贝解析

    题目:A linked list is given such that each node contains an additional random pointer which could poin ...