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. UVa 10253 - Series-Parallel Networks

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  2. Json数据,日期的转换

    using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE)) { con.Open(); using (SQLi ...

  3. HDU 5442 后缀自动机+kmp

    题目大意: 给定一个字符串,可理解成环,然后选定一位置,逆时针或顺时针走一遍,希望得到字典序最大,如果同样大,希望找到起始位置最小的,如果还相同,就默认顺时针 比赛一直因为处理最小位置出错,一结束就想 ...

  4. javaweb之Cookie篇

    Cookie是在浏览器访问某个Web资源时,由Web服务器在Http响应消息头中通过Set-Cookie字段发送给浏览器的一组数据. 一个Cookie只能表示一个信息对,这个信息对有一个信息名(Nam ...

  5. Android ScrollView与ViewPager滑动冲突

    前段时间做项目碰到在ScrollView里添加ViewPager,但是发现ViewPager的左右滑动和ScrollView的滑动冲突了,解决这个问题的方法是重写ScrollView. 代码: pub ...

  6. 算法(第4版)-1.1.7 API

    总结:本小姐讲述了API的定义.作用以及一些Java库的举例. 重点: 1.API的目的是将调用和实现分离:除了API中给出的信息,调用者不需要知道实现的其他细节,而实现也不应考虑特殊的应用场景.

  7. IOS开发在线文档 记录下

    View Programming Guide for iOS https://developer.apple.com/library/prerelease/ios/documentation/UIKi ...

  8. php大力力 [021节]mysql表名在mac下不能大写

    2015-08-27 php大力力021.mysql表名在mac下不能大写 刚才数据库里面,phpMyAdmin狂出错. Some errors have been detected on the s ...

  9. Longest Substring Without Repeating Characters ---- LeetCode 003

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  10. Angularjs相关理论

    1.AngularJS的工作流程: (1)浏览器载入HTML,然后把它解析成DOM (2)浏览器载入angularjs脚本 (3)AngularJS等到DOMContentLoaded事件触发 (4) ...