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<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define inf 1e18
using namespace std; int q, n;
const int maxn = 50005;
int cow[maxn], talltree[maxn << 2], shorttree[maxn<<2]; void tall_pushup(int rt)//更新
{
talltree[rt] = max(talltree[rt << 1], talltree[rt << 1 | 1]);
} void short_pushup(int rt)
{
shorttree[rt] = min(shorttree[rt<<1], shorttree[rt<<1|1]);
} void tall_build(int l, int r, int rt)
{
if(l == r){
talltree[rt] = cow[l];
return;
}
int m = (l + r) >> 1;
tall_build(l, m, rt << 1);
tall_build(m + 1, r, rt << 1 | 1);
tall_pushup(rt);
} void short_build(int l, int r, int rt)
{
if(l == r){
shorttree[rt] = cow[l];
return;
}
int m = (l + r) >> 1;
short_build(l, m, rt << 1);
short_build(m + 1, r, rt << 1 | 1);
short_pushup(rt);
} /*void pushdown(int rt, int ln, int rn)
{
if(lazy[rt]){
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
tree[rt << 1] += lazy[rt] * ln;
tree[rt << 1 | 1] += lazy[rt] * rn;
lazy[rt] = 0;
}
} void update(int L, int C, int l, int r, int rt)
{
if(l == r){
tree[rt] += C;
return;
}
int m = (l + r) >>1;
if(L <= m) update(L, C, l, m, rt << 1);
else update(L, C, m + 1, r, rt << 1 | 1);
pushup(rt);
} void update(int L, int R, int C, int l, int r, int rt)
{
if(L <= l && r <= R){//本区间完全在操作区间内
tree[rt] += C * (r - l + 1);
lazy[rt] += C;
return;
}
int m = (l + r) >> 1;
pushdown(rt, m - l + 1, r - m);
if(L <= m) update(L, R, C, l, m, rt << 1);
if(R > m) update(L, R, C, m + 1, r, rt << 1 | 1);
pushup(rt);
}*/ int tall_query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R){
return talltree[rt];
}
int m = (l + r) >> 1;
//pushdown(rt, m - l + 1, r - m); int ans = 0;
if(L <= m) ans = max(ans, tall_query(L, R, l, m, rt << 1));
if(R > m) ans = max(ans, tall_query(L, R, m + 1, r, rt << 1 | 1));
return ans;
} int short_query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R){
return shorttree[rt];
}
int m = (l + r) >> 1;
//pushdown(rt, m - l + 1, r - m); int ans = inf;
if(L <= m) ans = min(ans, short_query(L, R, l, m, rt << 1));
if(R > m) ans = min(ans, short_query(L, R, m + 1, r, rt << 1 | 1));
return ans;
} int main()
{
while(scanf("%d%d", &n, &q) != EOF){
for(int i = 1; i <= n; i++){
scanf("%d", &cow[i]);
}
tall_build(1, n, 1);
short_build(1, n, 1); while(q--){
int a, b;
scanf("%d%d", &a, &b);
cout<<tall_query(a, b, 1, n, 1) - short_query(a, b, 1, n, 1)<<endl;
}
}
return 0;
}

poj3264 balanced lineup【线段树】的更多相关文章

  1. POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值

    题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...

  2. POJ3264 Balanced Lineup 线段树区间最大值 最小值

    Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...

  3. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

  4. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  5. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  6. bzoj 1636: [Usaco2007 Jan]Balanced Lineup -- 线段树

    1636: [Usaco2007 Jan]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 772  Solved: 560线 ...

  7. poj3264 Balanced Lineup(树状数组)

    题目传送门 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 64655   Accepted: ...

  8. POJ 3264 Balanced Lineup 线段树 第三题

    Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...

  9. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  10. POJ 3264 Balanced Lineup (线段树)

    Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...

随机推荐

  1. Matlab 随机数字

    1.随机生成仅仅有0.1元素的矩阵(m行n列) A=round(rand(m,n)) 2.随机生成每行有若干个0,1元素的矩阵,比方每行仅仅有2个元素为1,其它元素为0 A=zeros(4,5) fo ...

  2. php 安装rabbitmq扩展无报错版

    需要安装rabbitmq-c,rabbitmq-c是一个用于C语言的,与AMQP server进行交互的client库.下载了v0.5.2版本(https://github.com/alanxz/ra ...

  3. PHP代码审计笔记--任意文件下载漏洞

    在文件下载操作中,文件名及路径由客户端传入的参数控制,并且未进行有效的过滤,导致用户可恶意下载任意文件.  0x01 客户端下载 常见于系统中存在文件(附件/文档等资源)下载的地方. 漏洞示例代码: ...

  4. React Native(十一)——删除事件以及刷新列表

    需求:删除列表中的某一项,但不刷新整个页面,底下的数据顺势而上(第一张是原始数据,第二张是删除掉"你会介今年"这条动态后显示的数据). 中间的过程比较曲折,只因为刚开始的时候自己只 ...

  5. RunLoop 总结及应用

      什么是RunLoop 注释:和ppt上总结的一样 和代码一块去理解 从字面上看 运行循环 跑圈 循环 基本作用 保持程序的持续运行(比如主运行循环) 处理App中的各种事件(比如触摸事件.定时器事 ...

  6. bigdecimal 与long int 之间转换

    BigDecimal与Long.int之间的互换 在实际开发过程中BigDecimal是一个经常用到的数据类型,它和int Long之间可以相互转换. 转换关系如下代码展示: int 转换成 BigD ...

  7. window下线程同步之(Mutex(互斥器) )

    使用方法: 1.创建一个互斥器:CreateMutex: 2.打开一个已经存在的互斥器:OpenMutex: 3.获得互斥器的拥有权:WaitForSingleObject.WaitForMultip ...

  8. 【Linux】 解决报错: ImportError: libSM.so.6: cannot open shared object file: No such file or directory

    centos7 +  python3.6.4 我使用 pip3 install opencv-python 安装了opencv-python  之后,在使用 import cv2  报错如下 报错原因 ...

  9. jQuery().end()的内部实现及源码分析

    jQuery().end()的作用是返回当前jQuery对象的上一个状态. 1.end()源码: // 所有通过pushStack方法获得的jQuery对象都可以通过end方法返回之前的状态   // ...

  10. 改变vux样式

    场景:修改 x-header 颜色 解决: 在创建文件路径如下 src/assets/less/theme.less ; 在build/webpack.base.conf.js下添加 这两行即可