【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 34140 | Accepted: 16044 | |
| 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
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
Sample Input
6 3
1
7
3
4
2
5
1 5
4 6
2 2
Sample Output
6
3
0
Source
#include <cstdio>
#include <cstring> #define MAX(a, b) (a > b ? a : b)
#define MIN(a, b) (a < b ? a : b) //宏定义提高效率 const int LEN = ; struct Seg
{
int left, right;
int ma, mi;
}seg[LEN*]; void buildt(int l, int r, int step)
{
seg[step].left = l;
seg[step].right = r;
seg[step].ma = ;
seg[step].mi = 0x7fffffff;
if (l == r)
return;
int mid = (l + r)>>;
buildt(l, mid, step<<);
buildt(mid+, r, step<<|);
} void pushup(int step) //向上更新
{
seg[step].ma = MAX(seg[step<<].ma, seg[step<<|].ma);
seg[step].mi = MIN(seg[step<<].mi, seg[step<<|].mi);
} void update(int l, int r, int height, int step)
{
if (l == seg[step].left && r == seg[step].right){
seg[step].mi = height;
seg[step].ma = height;
return;
}
if (seg[step].left == seg[step].right)
return;
int mid = (seg[step].left + seg[step].right)>>;
if (r <= mid)
update(l, r, height, step<<);
else if (l > mid)
update(l, r, height, step<<|);
else{
update(l, mid, height, step<<);
update(mid+, r, height, step<<|);
}
pushup(step); //递归中更新完下一个节点后向上更新
} int queryma(int l, int r, int step) //求区间最大值
{
if (l == seg[step].left && r == seg[step].right){
return seg[step].ma;
}
if (seg[step].left == seg[step].right)
return ;
int mid = (seg[step].left + seg[step].right)>>;
if (r <= mid)
return queryma(l, r, step<<);
else if (l > mid)
return queryma(l, r, step<<|);
else{
int a = queryma(l, mid, step<<);
int b = queryma(mid+, r, step<<|); //防止使用宏定义时多次调用queryma,先调用得到返回值,再比较返回值
return MAX(a, b);
}
} int querymi(int l, int r, int step) //求区间最小值
{
if (l == seg[step].left && r == seg[step].right){
return seg[step].mi;
}
if (seg[step].left == seg[step].right)
return 0x7fffffff;
int mid = (seg[step].left + seg[step].right)>>;
if (r <= mid)
return querymi(l, r, step<<);
else if (l > mid)
return querymi(l, r, step<<|);
else{
int a = querymi(l, mid, step<<);
int b = querymi(mid+, r, step<<|); //同上
return MIN(a, b);
}
} int main()
{
int n, q;
scanf("%d %d", &n, &q);
buildt(, n, );
for(int i = ; i <= n; i++){
int t;
scanf("%d", &t);
update(i, i, t, );
}
for(int i = ; i < q; i++){
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", queryma(a, b, ) - querymi(a, b, ));
}
return ;
}
【POJ】3264 Balanced Lineup ——线段树 区间最值的更多相关文章
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- POJ 3264 Balanced Lineup (线段树)
Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...
- POJ - 3264 Balanced Lineup 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- BZOJ-1699 Balanced Lineup 线段树区间最大差值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...
- POJ3264 Balanced Lineup 线段树区间最大值 最小值
Q个数 问区间最大值-区间最小值 // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <i ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
随机推荐
- 三角形(hd1249)
三角形 Problem Description 用N个三角形最多可以把平面分成几个区域? Input 输入数据的第一行是一个正整数T(1<=T<=10000),表示测试数据的数量.然后 ...
- App_Code
App_Code,文件夹是·NET平台下.在创建网站时,系统为类自动放的位置.它位于Web应用程序根目录下,其存储所有应当作为应用程序的一部分动态编译的类文件.这些类文件自 动链接到应用程序,而不需要 ...
- Azure上如何在Linux下挂载数据磁盘
[原文首次发表于51cto http://cloudapps.blog.51cto.com/3136598/1653672] 在Azure上创建了虚拟机之后,我们在一些情况下会需要添加更多的数据磁盘来 ...
- Android中Handle详解
上图为本人总结的Handler,网上发现一片总结很好的博客就copy过来:作为参考 Handler有何作用?如何使用? 一 .Handler作用和概念 包含线程队列和消息队列,实现异步的消息处理机制, ...
- haproxy redirect prefix
acl short_domain hdr(Host) -i etiantian.org redirect prefix http://www.etiantian.org code 301 if sho ...
- win7系统怎样备份
利用系统自带的备份还原 1 这种方法的缺点是如果以后系统出现问题,无法进入系统的话,就无法恢复系统了.首先我们点击开始菜单,打开控制面板! 2 在控制面板中点击系统和安全! 3 我们选择备份和还原中的 ...
- 关于CMCC(中国移动)、CU(中国联通)、CT(中国电信)的一些笔记
一.三大运营商网络 CMCC(ChinaMobileCommunicationCorporation):GSM(2G).TD-SCDMA(3G).TD-LTE(4G); CU(China Unicom ...
- linux下如何产生core,调试core
linux下如何产生core,调试core 摘自:http://blog.163.com/redhumor@126/blog/static/19554784201131791239753/ 在程序不寻 ...
- 学习本课程需要具备哪些基础及微信小程序目录结构介绍
1.html css js 基础 2.ajax 基础 3.简单的面向对象基础
- poj2488 A Knight's Journey
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24840 Accepted: ...