题目链接:https://vjudge.net/problem/POJ-3264

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 <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 5e4+; int maxv[MAXN<<], minv[MAXN<<]; void push_up(int u)
{
maxv[u] = max(maxv[u*], maxv[u*+]);
minv[u] = min(minv[u*], minv[u*+]);
} void add(int u, int l, int r, int x, int val)
{
if(l==r)
{
maxv[u] = minv[u] = val;
return;
} int mid = (l+r)>>;
if(x<=mid) add(u*, l, mid, x, val);
else add(u*+, mid+, r, x, val);
push_up(u);
} int maxx, minn;
void query(int u, int l, int r, int x, int y)
{
if(x<=l && r<=y)
{
maxx = max(maxx, maxv[u]);
minn = min(minn, minv[u]);
return;
} int mid = (l+r)>>;
if(x<=mid) query(u*, l, mid, x, y);
if(y>=mid+) query(u*+, mid+, r, x, y);
} int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
int x, y;
for(int i = ; i<=n; i++)
{
scanf("%d", &x);
add(, , n, i, x);
} for(int i = ; i<=m; i++)
{
scanf("%d%d", &x, &y);
maxx = -INF, minn = INF;
query(, , n, x, y);
printf("%d\n", maxx-minn);
}
}
}

POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值的更多相关文章

  1. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  2. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  3. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  4. hdu 1166线段树 单点更新 区间求和

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

  5. hdu1166(线段树单点更新&区间求和模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...

  6. hdu2795(线段树单点更新&区间最值)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...

  7. HDU 3308 LCIS(线段树单点更新区间合并)

    LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...

  8. 【HDU】1754 I hate it ——线段树 单点更新 区间最值

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. hdu 1754 I Hate It 线段树 单点更新 区间最值

    线段树功能:update:单点更新 query:区间最值 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define r ...

随机推荐

  1. mysql语句查询:查询距离某个日期10天的数据

    select * from table  where state = '1' and DATE_SUB(CURDATE(), INTERVAL 10 DAY) <= begin_time

  2. 71.mybatis 如何获取插入的id【从零开始学Spring Boot】

    [从零开始学习Spirng Boot-常见异常汇总] 在之前的文章已经讲过spring boot集成mybatis了,但是忘记说一个很重要的知识点了,那就是获取获取主键id,这篇文章补充下,sprin ...

  3. SQL ROW_NUMBER() 通用分页存储过程

    --提取分页数据,返回总记录数 Create procedure [dbo].[sp_Common_GetDataPaging_ReturnDataCount] ( @SqlString varcha ...

  4. SPOJ LCS 后缀自动机找最大公共子串

    这里用第一个字符串构建完成后缀自动机以后 不断用第二个字符串从左往右沿着后缀自动机往前走,如能找到,那么当前匹配配数加1 如果找不到,那么就不断沿着后缀树不断往前找到所能匹配到当前字符的最大长度,然后 ...

  5. Hankson 的趣味题(codevs 1172)

    题目描述 Description Hanks 博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫Hankson.现在,刚刚放学回家的Hankson 正在思考一个有趣的问题.今天在 ...

  6. Android Notification通知简介

    Android Notification通知简介 根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面 ...

  7. idea-自定义Java模板文件

    自定义 idea Java 模板步骤. #parse("File Header.java")表示引用的模板文件,如下:

  8. msp430入门编程12

    msp430中C语言的模块化头文件及库文件12 msp430入门学习 msp430入门编程

  9. 如何判断一个app是原生app还是 webapp,或者是混合app

    1.(快速)滚动起来是否比较卡2.图片加载失败的图标 断网检查不是绝对的,web app并不一定是在远程服务器上的, 也能pack在程序里,load本地的资源也能算是web app.     web ...

  10. 学习swift从青铜到王者之swift枚举07

    空枚举 //空枚举 enum SomeEnumeration { // enumeration definition goes here } 枚举基本类型 //枚举基本类型 enum CompassP ...