Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 41162   Accepted: 19327
Case Time Limit: 2000MS

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 ≤ 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

Source

 
晚上BC、CF,敲一发线段树练练手感
/*
* @Author: LinK
* @Date: 2015-10-31 18:20:11
* @Last Modified by: LinK
* @Last Modified time: 2015-10-31 18:27:30
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ; struct Node {
int ma, mi;
} tree[maxn << ]; int num[maxn];
int n, q; void build(int rt, int l, int r) {
if (l == r) {
tree[rt].ma = num[l];
tree[rt].mi = num[l];
return;
}
int mid = (l + r) >> ;
build(rt << , l, mid);
build(rt << | , mid + , r);
tree[rt].ma = max(tree[rt << ].ma, tree[rt << | ].ma);
tree[rt].mi = min(tree[rt << ].mi, tree[rt << | ].mi);
} int query_ma(int rt, int l, int r, int L, int R) {
if (L <= l && R >= r) return tree[rt].ma;
int mid = (l + r) >> ;
if (R <= mid) return query_ma(rt << , l, mid, L, R);
if (L > mid) return query_ma(rt << | , mid + , r, L, R);
return max(query_ma(rt << , l, mid, L, R), query_ma(rt << | , mid + , r, L, R));
} int query_mi(int rt, int l, int r, int L, int R) {
if (L <= l && R >= r) return tree[rt].mi;
int mid = (l + r) >> ;
if (R <= mid) return query_mi(rt << , l, mid, L, R);
if (L > mid) return query_mi(rt << | , mid + , r, L, R);
return min(query_mi(rt << , l, mid, L, R), query_mi(rt << | , mid + , r, L, R));
} int main() {
while (~scanf("%d %d", &n, &q)) {
for (int i = ; i <= n; i ++) scanf("%d", &num[i]);
build(, , n);
int a, b;
while (q --) {
scanf("%d %d", &a, &b);
int ma = query_ma(, , n, a, b);
int mi = query_mi(, , n, a, b);
printf("%d\n", ma - mi);
}
} return ;
}

POJ3264(线段树求区间最大最小值)的更多相关文章

  1. poj 3264 线段树 求区间最大最小值

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

  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. 【线段树求区间第一个不大于val的值】Lpl and Energy-saving Lamps

    https://nanti.jisuanke.com/t/30996 线段树维护区间最小值,查询的时候优先向左走,如果左边已经找到了,就不用再往右了. 一个房间装满则把权值标记为INF,模拟一遍,注意 ...

  6. BZOJ 4127: Abs (树链剖分 线段树求区间绝对值之和 带区间加法)

    题意 给定一棵树,设计数据结构支持以下操作 1 u v d 表示将路径 (u,v) 加d(d>=0) 2 u v 表示询问路径 (u,v) 上点权绝对值的和 分析 绝对值之和不好处理,那么我们开 ...

  7. HDU6447 YJJ's Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门  原题目描述在最下面.  1e5个点,问 ...

  8. hdu1166 敌兵布阵(线段树 求区间和 更新点)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  9. hdu3074 线段树求区间乘积(单点更新)

    题意:       给你n个数,两种操作,(1) 把第b个数改成c (2)算出b-c的乘积,结果对1000000007取余. 思路:       线段树单点更新,简单题目,不多解释,具体看代码. #i ...

随机推荐

  1. Ubuntu下使用Git_1

    这里小小的记录一下我在Ubuntu下使用版本控制工具Git的过程.在学习使用Git的时候,我发现了一个很好的网站,这里分享一下,大家共同学习. 猴子都能懂的Git入门 http://git.wiki. ...

  2. Python-类-函数参数-takes 0 positional arguments but 1 was given

    在学习Python基础的时候,在创建某一个shownametest()函数,解析器会报错 TypeError: shownametest() takes 0 positional arguments ...

  3. Java中的while(true)

    while(true)是一个无限循环 在内部用break或return退出循环,否则一直循环

  4. python 网络编程(远程执行命令与粘包)

    远程执行命令 先来学习一个新模块 , 一会用到的.. 新模块: subprocess 执行系统命令 r = subprocess.Popen('ls',shell=True,stdout=subpro ...

  5. C# 删除文件错误 access denied

    使用以下代码正常删除整个文件夹内容时,报错如下: if (backupPathDir.Exists) { System.IO.DirectoryInfo di = new DirectoryInfo( ...

  6. golang交叉编译笔记

    GOOS:目标平台的操作系统(darwin.freebsd.linux.windows) GOARCH:目标平台的体系架构(386.amd64.arm) Mac 下编译 Linux 和 Windows ...

  7. UVA 11297 Census(二维线段树)

    Description This year, there have been many problems with population calculations, since in some cit ...

  8. chrome谷歌浏览器导致的密码被修改现象

    版本 68.0.3440.106(正式版本) (32 位)记住密码功能有个缺陷,会把自己的密码自动填写到别人的密码框中,假如这个时候点击保存密码,就会导致其他用户的密码被修改为登录用户的密码.   很 ...

  9. PAT L2-019 悄悄关注

    https://pintia.cn/problem-sets/994805046380707840/problems/994805059731177472 新浪微博上有个“悄悄关注”,一个用户悄悄关注 ...

  10. fetch_array()与fetch_assoc()的用法

    fetch_array()与fetch_assoc()用起来没有什么大的差别,主要是怎么用?fetch_array()如果单独作为while的条件,则能够取出结果集中的所有结果.如果单独作用于结果集, ...