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. 免费提供一些公开的SOCK4/5/HTTP/HTTPS代理服务器(经测试可以用)

    Last update IP address Port Country Type Anonymity 54 secs 116.228.55.217 8000 flag China HTTP High ...

  2. adMob iAd整合,随机根据网络状况自动显示。

    最近找整合的代码,找到的都不对,有个大概对的,但要奔溃退出,只要两个单独弄. adMob 下载好sdk,导入进去,iAd的加入iad framework. 使用方法,在viewController v ...

  3. 从ajax获取的数据无法通过Jquery选择器来调用事件

    如果标签是动态生成的,比如说div.tr.td等,若需通过Jquery来获取事件,那么需要用live来绑定相应的事件. 比如说绑定div的click事件 $("div").live ...

  4. Exif的Orientation信息说明

    EXIF Orientation 参数让你随便照像但都可以看到正确方向的照片而无需手动旋转(前提要图片浏览器支持,Windows 自带的不支持) 这个参数在佳能.尼康相机照的照片是自带的,但我的奥林巴 ...

  5. C++-类的const成员变量

    当类中用到一些固定值时,希望将其定义为const成员变量,防止被修改. 但因为const成员变量因为初始化之后就不能修改,因此只能在构造函数的初始化列表中初始化 如果是数组,则没有办法在初始化列表中初 ...

  6. android 判断网络是否连接

    package com.liucanwen.baidulocation.util; import android.app.Activity; import android.content.Contex ...

  7. idea快捷键(转)

    Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*...*/ )Ctrl+D 复制行Ctrl+X 删除行快速修复 alt+enter (modify/cast)代码提示 alt+/ctr ...

  8. greenDao 学习之坑 "java-gen" 目录下的类不能引用

    由于公司最近的项目需要频繁地操作数据库,所以选用greenDao. 网上搜了一 大堆教程,我卡在java工程运行后生成的几个类不能引用了. 看了一下区别,教程的java-gen 目录是蓝色的小框框 , ...

  9. SVN 首次用TortoiseSVN Checkout 提示Unexpected HTTP status 405

    权限错误 首次使用 因为没有 弹出 用户名密码输入框 无法输入 帐户信息. 解决办法: 点击 仓库地址输入栏 右边 的...按钮 此时弹出的输入框浏览器方式访问的.输入用户名和密码,然后在左侧出来的仓 ...

  10. ios页面间传递参数四种方式

    ios页面间传递参数四种方式 1.使用SharedApplication,定义一个变量来传递. 2.使用文件,或者NSUserdefault来传递 3.通过一个单例的class来传递 4.通过Dele ...