Balanced Lineup

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

线段树,也叫区间树(interval tree),它在各个节点保存一条线段(即子数组)。设数列A 包含
N 个元素,则线段树的根节点表示整个区间A[1;N],左孩子表示区间A[1; (1 + N)/2],右孩子表
示区间A[(1 + N)/2 + 1;N],不断递归,直到叶子节点,叶子节点只包含一个元素。
线段树有如下特征:
• 线段树是一棵完全二叉树
• 线段树的深度不超过logL, L 是区间的长度
• 线段树把一个长度为L 的区间分成不超过2 logL 条线段
线段树的基本操作有构造线段树、区间查询和区间修改。
线段树通常用于解决和区间统计有关的问题。比如某些数据可以按区间进行划分,按区间动态
进行修改,而且还需要按区间多次进行查询,那么使用线段树可以达到较快的查询速度。
用线段树解题,关键是要想清楚每个节点要存哪些信息(当然区间起点和终点,以及左右孩子
指针是必须的���,以及这些信息如何高效查询,更新。不要一更新就更新到叶子节点,那样更新操作
的效率最坏有可能O(N) 的。

用数组存储完全二叉树,可以使速度更快~

#include <iostream>
#include <vector>
#include <algorithm>
#include<map>
#include<vector>
#include<queue>
#include<string>
#include<set>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstring>
//#pragma warning(disable:4996)
using namespace std;
#define MAXN 50001
#define INF 0x7fffffff;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define L(a) ((a)<<1)
#define R(a) (((a)<<1)+1)
//数组存储线段树
typedef struct node_t {
int left, right;
int maxx, minx;
} node_t;
int a[MAXN];
node_t node[MAXN * 4];
int minx, maxx;
void init() {
memset(node, 0, sizeof(node));
}
//以t为根节点,为区间l,r建立线段树
void build(int t, int l, int r) {
node[t].left = l, node[t].right = r;
if (l == r) {
node[t].maxx = node[t].minx = a[l];
return;
}
const int mid = (l + r) / 2;
build(L(t), l, mid);
build(R(t), mid + 1, r);
node[t].maxx = max(node[L(t)].maxx, node[R(t)].maxx);
node[t].minx = min(node[L(t)].minx, node[R(t)].minx);
}
void query(int t, int l, int r) {
if (t > 200000) {
minx = maxx = 0;
return;
}
if (node[t].left == l&&node[t].right == r) {
if (maxx < node[t].maxx)
maxx = node[t].maxx;
if (minx > node[t].minx)
minx = node[t].minx;
return;
}
const int mid = (node[t].left + node[t].right) / 2;
if (l > mid) {
query(R(t), l, r);
}
else if (r <= mid) {
query(L(t), l, r);
}
else {
query(L(t), l, mid);
query(R(t), mid + 1, r);
}
}
int main()
{
//freopen("s.txt", "r", stdin);
int n, q, i;
scanf("%d%d", &n, &q);
for (i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
init();
build(1, 1, n);
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
maxx = 0;
minx = INF;
query(1, a, b);
printf("%d\n", maxx - minx);
}
return 0;
}

初级线段树 POJ3264的更多相关文章

  1. poj3264(简单线段树)

    题目链接:https://vjudge.net/problem/POJ-3264 题意:线段树简单应用题,区间查询最大值和最小值的差. 思路:用线段树维护区间的最大值和最小值即可. AC代码: #in ...

  2. Balanced Lineup poj3264 线段树

    Balanced Lineup poj3264 线段树 题意 一串数,求出某个区间的最大值和最小值之间的差 解题思路 使用线段树,来维护最大值和最小值,使用两个查询函数,一个查区间最大值,一个查区间最 ...

  3. 几道简单的线段树入门题 POJ3264&&POJ3468&&POJ2777

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 40687   Accepted: 19137 ...

  4. [POJ3264]Balanced Lineup(线段树,区间最值差)

    题目链接:http://poj.org/problem?id=3264 一排牛按1~n标号记录重量,问每个区间最重的和最轻的差值. 线段树维护当前节点下属叶节点的两个最值,查询后作差即可. #incl ...

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

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

  6. poj3264(线段树区间求最值)

    题目连接:http://poj.org/problem?id=3264 题意:给定Q(1<=Q<=200000)个数A1,A2,```,AQ,多次求任一区间Ai-Aj中最大数和最小数的差. ...

  7. POJ3264(线段树求区间最大最小值)

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

  8. kuangbin专题七 POJ3264 Balanced Lineup (线段树最大最小)

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

  9. POJ-3264 Balanced Lineup(区间最值,线段树,RMQ)

    http://poj.org/problem?id=3264 Time Limit: 5000MS     Memory Limit: 65536K Description For the daily ...

  10. 【ACM/ICPC2013】线段树题目集合(一)

    前言:前一段时间在网上找了一个线段树题目列表,我顺着做了一些,今天我把做过的整理一下.感觉自己对线段树了解的还不是很深,自己的算法能力还要加强.光练代码能力还是不够的,要多思考.向队友学习,向大牛学习 ...

随机推荐

  1. 使用js闭包封装一个原生的模态框

    现在都是用的是人家封装的框架什么的,但是对于底层的了解也是必须的,不然就无法提升,下面分享一个2 years ago 自己封装的一个提示框 样式很简单(适用于任何分辨率) 具体代码如下 /** * 该 ...

  2. 大幅提升前端工作效率!Numeral.js数值格式化库来了!

    我们日常开发中,时常会碰到数值格式化操作的场景,今天了不起就为大家分享一款相对比较全面的数值格式化的JS库:Numeral.js Numeral.js Numeral.js 是一个用来对数值进行操作和 ...

  3. 使用 conda 和 Jupyter 在 R 中实现数据科学分析

    前两篇文章我们介绍了 Jupyter Notebook 的一些基础用法,今天我们来介绍一下如何使用 conda 和 Jupyter 在 R 中开始一个数据科学项目. 在开始之前我们先要明确一个概念:K ...

  4. 【Python&GIS】通过经纬度创建矢量点文件

         最近在做项目时,需要判断某个点是否在感兴趣区内.所以需要使用Python先根据经纬度的点创建矢量文件,再通过点文件和面文件的位置关系判断点是否在面内.         这里我们使用osgeo ...

  5. WPF中关于转换器

    实例:关于播放器按钮状态的改变 说明:对Kind(种类)的绑定首先在viewmodel中声明一个属性IsPlay,然后在转换器中实现按钮的状态 转换器 在解决方案中的项目里新建一个Converters ...

  6. RabbitMQ快速使用代码手册

    本篇博客的内容为RabbitMQ在开发过程中的快速上手使用,侧重于代码部分,几乎没有相关概念的介绍,相关概念请参考以下csdn博客,两篇都是我找的精华帖,供大家学习.本篇博客也持续更新~~~ 内容代码 ...

  7. 使用hashicorp Raft开发分布式服务

    使用hashicorp Raft开发高可用服务 开发raft时用到的比较主流的两个库是Etcd Raft 和hashicorp Raft,网上也有一些关于这两个库的讨论.之前分析过etcd Raft, ...

  8. fiddler抓包手机和部分app无法连接网络问题

    前言: 最近公司在做app项目,测试环境app包没有调试模式,导致测试过程中无法查看请求接口和请求的参数,故需要通过抓包工具抓包 一)fiddler安装配置 1.下载安装fiddler,这里不说明了, ...

  9. 理解ffmpeg

    ffmpeg是一个完整的.跨平台的音频和视频录制.转换和流媒体解决方案. 它的官网:https://ffmpeg.org/ 这里有一份中文的文档:https://ffmpeg.p2hp.com/ ff ...

  10. HCL实验:5.单臂路由实现不同vlan通信

    使用单臂路由实现不同vlan 互通 拓扑图 网关均为所在网段的第一个地址 交换机配置 创建vlan 划分端口 配置端口类型 显示简要信息 路由器配置 路由器的端口默认关闭,需要手动开启 进行子端口的划 ...