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. invalid types 'int[int]' for array subscrip

    定义重复 如 一个int r 与一个 r[i] 重复

  2. 过滤器 Filter

    Filter(过滤器)简介 Filter 的基本功能是对发送到 Servlet 的请求进行拦截, 并对响应也进行拦截. Filter 程序是一个实现了 Filter 接口的 Java 类,与 Serv ...

  3. asp.net后台编写 loading效果

    From :http://www.cnblogs.com/ganmk/articles/1207832.html 使用方法: protected void Page_Load(object sende ...

  4. 【参考文献1】Word2010删除引用参考文献留下的横线

    那个莫名其妙不能选中,相信你也遇到.如果是这样,可能那个是尾注分隔符.你可以按照本文的方法解决.当然啦,这个也是按照网上的方法整理参考文献会留下的直线 Office Word2010 方法/步骤   ...

  5. [网络技术][转]PPTP协议解析

    PPTP协议大体上可以分为两部分:控制层连接和隧道,下面简要介绍两部分的功能.如果要详细了解PPTP协议请阅读RFC文档. 一. Control Connection Protol 控制层连接是基于T ...

  6. gsoap框架下的onvif程序流程分析

    SOAP_FMAC5 int SOAP_FMAC6 soap_serve(struct soap *soap) { do { unsigned int k = soap->max_keep_al ...

  7. 预写式日志WAL

    Chapter 25. 预写式日志(Write-Ahead Logging (WAL)) Table of Contents 25.1. WAL 的好处 25.2. WAL 配置 25.3. 内部 预 ...

  8. 理解smart pointer之三:unique_ptr

    unique_ptr最先在boost中被定义,后来被C++标准委员会选中为C++11的feature之一. std::unique_ptr is a smart pointer that retain ...

  9. poj2429 大数分解+dfs

    //Accepted 172 KB 172 ms //该程序为随机性算法,运行时间不定 #include <cstdio> #include <cstring> #includ ...

  10. fwrite错误

    使用fwrite出错 f:\dd\vctools\crt_bld\self_x86\crt\srt\write.cline:69expression:_osfile(fh)&FOPEN 使用w ...