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. python垃圾回收,判断内存占用,手动回收内存,二

    以下为例子,判断计算机内存并释放程序内存. # coding=utf8 import time import psutil, gc, commands,os from logger_until imp ...

  2. 《倾国倾城》全套源代码:client+服务端+资源,歧视复制帖子

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠,支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 游戏官方下 ...

  3. 大杂烩 -- Iterator 和 Iterable 区别和联系

    基础大杂烩 -- 目录 用Iterator模式实现遍历集合  Iterator模式是用于遍历集合类的标准访问方法.它可以把访问逻辑从不同类型的集合类中抽象出来,从而避免向客户端暴露集合的内部结构. 例 ...

  4. MongoDB管理

    前几篇文章都是从开发和使用的角度了解了MongoDB的各个知识点,这篇文章将从MongoDB管理的角度入手,了解MongoDB管理所要了解的基本知识. 数据库命令 在前面几篇文章中,已经接触了一些数据 ...

  5. Myecplise反编译工具安装

    我使用的是Myecplise8.5 下载jad.exe文件jad158g.win.zip:http://varaneckas.com/jad/ 下载jadeclipse插件 net.sf.jadcli ...

  6. [Command] sync - 同步内存与硬盘数据

    sync - 同步内存与硬盘之间的数据. sync [--help] [--version] sync 命令将内存中缓存的数据写入磁盘.这包括但不限于修改过的 superblock, inode 和延 ...

  7. MDK972-EK开发板裸调试设置和裸机程序烧写(转)

    硬件平台:MDK972-EK开发板编译调试软件:KEIL uVision4仿真工具:JLINK V7/V8   本例子从串口输出信息,如图:       KEIL uVision4调试设置如图所示: ...

  8. MVC的简单初步学习(2)

    今天似乎一切是正常的,我们的课依旧在进行着,但是恍惚脑海中并没有那样的平静,不知道在想些什么?而且今天是学习MVC的初步开始,我应该认真地学习才是正确的啊.但是我并不糊涂,今天是周一,也就是刚开始上课 ...

  9. react设置多个className

    在一个元素上设置样式,有一个固定的样式,然后还有一个使用三元运算符根据条件添加的样式. 比如说有一个固定样式"title": <div className="tit ...

  10. Qt下libusb-win32的使用方法

    之前一直找不到适合WIN7下的Tiny6410的USB下载软件,正好这几天开始学习USB,所以打算自己写一个专门用于Tiny6410的WIN7下的USB下载软件. 发现了libusb这个库可以用作无驱 ...