Balanced Lineup

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 63040   Accepted: 29405
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

USACO 2007 January Silver

问题链接:POJ3264 Balanced Lineup

问题简述

给定N个数和Q个询问,计算区间最大值与最小值的差。

问题分析

这个问题可以线段树来解。

区间值可以通过前缀和之差来计算,然而如果某些值发生变化,通过前缀和来计算区间值就不适用了。通常线段树是用来计算区间和的,解决动态变化的问题。然而,这个问题要计算的不是区间和,而是区间最大值与最小值的差,可以通过改造的线段树进行计算。

需要注意的是,对于n个数,构造其线段树时,需要4倍的节点。另外,使用结构数组来存储线段树,比起带指针链接的线段树,计算效率要高一些。

这里构造的线段树,根节点下标为0。某个节点的下标为i,则其左右子节点的下标分别为i×2+1和i×2+2。

程序说明

使用线段树后,其他都是套路。

这里假设整数类型int是32位的。那么最大值就可以使用程序中定义的INF,最小值可以用-INF。

题记:(略)

参考链接:(略)

AC的C++语言程序如下:

/* POJ3264 Balanced Lineup */#include <iostream>
#include <stdio.h>using namespace std;const int INF = 0x3FFFFFFF;
const int N = 5e4;struct Node {int l, r;int minv, maxv;int mid() {return (l + r) >> 1;}     // (l + r) / 2
} tree[N * 4];int minv, maxv;void buildTree(int root, int l, int r)
{tree[root].l = l;tree[root].r = r;tree[root].minv = INF;tree[root].maxv = -INF;if(l != r) {buildTree(2 * root + 1, l, (l + r) >> 1);buildTree(2 * root + 2, ((l + r) >> 1) + 1, r);}
}void insert(int root, int i, int v)
{if(tree[root].l == tree[root].r)tree[root].minv = tree[root].maxv = v;else {tree[root].minv = min(tree[root].minv, v);tree[root].maxv = max(tree[root].maxv, v);if(i <= tree[root].mid())insert(2 * root + 1, i, v);elseinsert(2 * root + 2, i, v);}
}void query(int root, int s, int e)
{if(minv <= tree[root].minv && tree[root].maxv <= maxv);else if(tree[root].l == s && tree[root].r == e) {minv = min(minv, tree[root].minv);maxv = max(maxv, tree[root].maxv);} else if(e <= tree[root].mid())query(2 * root + 1, s, e);else if(s > tree[root].mid())query(2 * root + 2, s, e);else {query(2 * root + 1, s, tree[root].mid());query(2 * root + 2, tree[root].mid() + 1, e);}
}int main()
{int n, q;while(~scanf("%d%d", &n, &q)) {buildTree(0, 1, n);for(int i = 1; i <= n; i++) {int h;scanf("%d", &h);insert(0, i, h);}while(q--) {int s, e;scanf("%d%d", &s, &e);minv = INF;maxv = -INF;query(0, s, e);printf("%d\n", maxv - minv);}}return 0;
}

POJ3264 Balanced Lineup【线段树】相关推荐

  1. POJ3264——Balanced Lineup(线段树)

    本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中.取一段区间.然后在区间中找出最大的数和最小的数字.求这两个数字的差. 分析:按区间取值,非常明显使 ...

  2. poj3264 - Balanced Lineup(RMQ_ST)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 45243   Accepted: 21240 ...

  3. poj 3264 Balanced Lineup RMQ问题 线段树

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

  4. 【POJ3264】Balanced Lineup,线段树入门

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Case Time Limit: 2000MS Description For the ...

  5. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  6. POJ 3264 Balanced Lineup 【线段树】

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 50004 Accepted: 23434 Cas ...

  7. Poj 3246 Balanced Lineup(线段树基础)

    依旧是线段树基础题 询问区间的最大值和最小值之差,只有询问,没有插入删除.继续理解基础线段树 #include <iostream> #include <algorithm> ...

  8. bzoj 1699: [Usaco2007 Jan]Balanced Lineup排队【st表||线段树】

    要求区间取min和max,可以用st表或线段树维护 st表 #include<iostream> #include<cstdio> using namespace std; c ...

  9. POJ——3624 Balanced Lineup(线段树入门——区间最值问题)

    原题链接:http://poj.org/problem?id=3264 每天挤奶时,农夫John的N头奶牛(1≤N≤50,000头)总是按照相同的顺序排列.一天,农夫约翰决定和几头牛组织一场极限飞盘游 ...

最新文章

  1. 博弈最高位POJ 1704(Georgia and Bob-Nim博弈)
  2. bat脚本登陆ftp服务器
  3. linux shell 2 /dev/null的解释
  4. CodeForces - 1110E-Magic Stones(差分+思维)
  5. net空间一次购买终身使用_官方解答关于 Internet Download Manager IDM 终身许可证和1年许可证的相关说明!...
  6. python生成配置文件config_Python configparser模块封装及构造配置文件
  7. 【转】 android之如何在两个activity之间传递handler_利用broadcast广播机制
  8. 【Struts1.2总结系列】struts-config.xml配置
  9. 神舟微型计算机登录密码忘记,win10开机密码忘记按f2(win10忘记密码强制重置)
  10. 成都最稳定的dns服务器地址,成都首选DNS服务器地址
  11. OA实施成功的几个必备条件
  12. 好看视频出击,从Q2财报看百度的短视频谋局
  13. c#实现短信发送程序
  14. 平面设计中的抠图技法与修图思路
  15. 1天1个岗位画像洞察-无线DPM岗位
  16. Cadence OrCAD Capture CIS ODBC数据库文件在两台电脑上同步使用时一台电脑启动失败的问题解决图文教程
  17. python案例:股民福利,采集股票数据~
  18. Word2016如何去掉首页页码并从任意也开始页码
  19. alin的学习之路(数据库篇:二)(select查询,where条件查询,order by排序,单行函数,多行函数,group by分组)
  20. 安卓系统刷机怎么刷机_手机刷机怎么刷

热门文章

  1. 使用iframe实现在pc端预览移动端页面的效果
  2. linux下编辑文件实验,Linux实验_修改
  3. linux系统中怎么设置网络,vmware中linux怎么设置网络
  4. python怎样播放音乐_Python如何播放音乐?
  5. android 分割字符 指定长度_[Android]TextUtils.ellipsize()截取指定长度字符串(附图文混排)...
  6. 5.2.5 标准的原子整型的相关操作
  7. 大数据学习之Hadoop任务输出到多个目录中
  8. java多线程 run start_java多线程中run和start区别
  9. matlab提取线条,请问如何将图片中的红色激光线条给提取出来啊,有没有大佬救救孩子,贴出代码给瞅瞅啊!谢谢!...
  10. tensorflow精进之路(二十七)——人脸识别(中)(MTCNN人脸检查和人脸对齐+FaceNet模型)