Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 32778   Accepted: 15425
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

2014-9-4 12:07:18更新:

#include <stdio.h>
#include <algorithm>
#define inf 0x7fffffff
#define maxn 50002
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std; struct Node{
int maxv, minv;
} tree[maxn << 2];
int arr[maxn], minv, maxv; void pushUp(int rt){
tree[rt].maxv = max(tree[rt << 1].maxv, tree[rt << 1 | 1].maxv);
tree[rt].minv = min(tree[rt << 1].minv, tree[rt << 1 | 1].minv);
} void build(int l, int r, int rt)
{
if(l == r){
tree[rt].maxv = tree[rt].minv = arr[l];
return;
}
int mid = (l + r) >> 1;
build(lson); build(rson);
pushUp(rt);
} void query(int left, int right, int l, int r, int rt)
{
if(left == l && right == r){
maxv = max(maxv, tree[rt].maxv);
minv = min(minv, tree[rt].minv);
return;
}
int mid = (l + r) >> 1;
if(right <= mid) return query(left, right, lson);
else if(left > mid) return query(left, right, rson);
query(left, mid, lson); query(mid + 1, right, rson);
} int main()
{
int n, m, i, a, b;
while(scanf("%d%d", &n, &m) == 2){
for(i = 1; i <= n; ++i)
scanf("%d", &arr[i]);
build(1, n, 1);
while(m--){
scanf("%d%d", &a, &b);
minv = inf; maxv = 0;
query(a, b, 1, n, 1);
printf("%d\n", maxv - minv);
}
}
return 0;
}

#include <stdio.h>
#define maxn 200002
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1 struct Node{
int min, max;
} tree[maxn << 2];
int maxAns, minAns; int maxVal(int a, int b)
{
return a > b ? a : b;
} int minVal(int a, int b)
{
return a < b ? a : b;
} void build(int l, int r, int rt)
{
if(l == r){
scanf("%d", &tree[rt].min);
tree[rt].max = tree[rt].min;
return;
} int mid = (l + r) >> 1;
build(lson);
build(rson); tree[rt].max = maxVal(tree[rt << 1].max, tree[rt << 1 | 1].max);
tree[rt].min = minVal(tree[rt << 1].min, tree[rt << 1 | 1].min);
} void query(int left, int right, int l, int r, int rt)
{
if(left == l && right == r){
if(tree[rt].max > maxAns) maxAns = tree[rt].max;
if(minAns > tree[rt].min) minAns = tree[rt].min;
return;
} int mid = (l + r) >> 1;
if(right <= mid) query(left, right, lson);
else if(left > mid) query(left, right, rson);
else{
query(left, mid, lson);
query(mid + 1, right, rson);
}
} int main()
{
int n, q, i, a, b;
scanf("%d%d", &n, &q); build(1, n, 1); while(q--){
scanf("%d%d", &a, &b);
maxAns = 1; minAns = 1000000;
query(a, b, 1, n, 1);
printf("%d\n", maxAns - minAns);
} 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. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

  4. HDU 1166 敌兵布阵(线段树单点更新)

    敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...

  5. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  6. HDU 1166 敌兵布阵(线段树单点更新,板子题)

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

  7. POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Descrip ...

  8. HDU 1166 敌兵布阵(线段树单点更新,区间查询)

    描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...

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

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

  10. HDUOJ----1166敌兵布阵(线段树单点更新)

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

随机推荐

  1. 常用的find命令

    find命令 find [路径名] –name/-size/-perm find [路径名] –name “*p” 在路径搜索p结尾的文件夹及文件 find [路径名] –name “[ab]*” 在 ...

  2. python Tk()生成的桌面的具体设置方法

    rom tkinter import * root = Tk() root['height'] = 300 #设置高 root['width'] = 500 #设置宽 root.title('魔方小站 ...

  3. python类的__new__和__init__

    python的类,和其他语言有一点不太一样,就是,他把新建一个类和初始化一个类,分成了两个方法: __new__ __init__ 当然,想想就知道,肯定是__new__先发生,然后才是__init_ ...

  4. [BZOJ3027][Ceoi2004]Sweet 容斥+组合数

    3027: [Ceoi2004]Sweet Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 135  Solved: 66[Submit][Status] ...

  5. 转:Laravel 安装指南

    Git 介绍 之所以要说 Git,就是因为 Composre 有时需要用到 Git,还是安装上比较好,Composer 暂且不表,先来了解一下 Git 吧(已经安装的童鞋跳过这里,直接看 Compos ...

  6. 蓝牙遥控小车设计(三)——Amarino和 Android手机重力感应控制

    最近事真是多啊,一件接着一件的,加上自己拖延症~ - -! 遥控小车基本完成了,只是自己没时间来更新. 现在更新手机控制的部分 首先我们要熟悉一个软件—— 官网地址:http://www.amarin ...

  7. [scrapy] PIL老是出错,换成pillow解决问题

    使用scrapy下载图片的时候,用PIL老是下载不成功 出现如下错误: IOError: encoder jpeg not available 据说是安装PIL之前缺少一些相关的包 freetype ...

  8. HDU 1233.还是畅通工程-最小生成树(Prime)

    还是畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. OJP1147括号匹配加强版(栈)与P1153乱头发节(单调栈)

    惨兮兮的被刷掉2%的通过率后在经过思考和dalao的指点后终于A掉了这道题 强烈建议修改这题的样例,实在太迷惑人,各种错误算法都能过 比如说这是一份错误代码,看懂了也不要学思路,和正解不知道差到哪里去 ...

  10. CSS中的层叠、特殊性、继承、样式表中的@import

    CSS中的层叠.特殊性.继承.样式表中的@import 层叠 CSS有一个机制是层叠,层叠可以理解为对样式的覆盖,优先性为: 网站开发者的样式表 用户样式(通过设置浏览器的显示选项) 浏览器默认的样式 ...