【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模板题,在这 ...
随机推荐
- asp.net MVC4 +MVCpager 无刷新分页
本人菜鸟,最近在用MVC4和MVCpager做无刷新分页时,发现点击下一页时数据不是Ajax提交的,弄了好久终于找到原因,原来还是Jquery引用的问题,现在把代码粘出来,希望能帮到刚接触的程序员,第 ...
- SQL Server 连接和事务相关的问题。
方法 1. dbcc opentran + sys.dm_exec_connections dbcc opentran; dbcc opentran 针对当前数据库 dbcc opentran('St ...
- 10453 Make Palindrome (dp)
Problem A Make Palindrome Input: standard input Output: standard output Time Limit: 8 seconds By def ...
- linux下修改ip地址,默认网关以及DNS
*修改IP地址 即时生效: ifconfig eth0 192.168.1.100 netmask 255.255.255.0 重启生效: vim /etc/sysconfig/network-s ...
- Android NDK 下载
Android NDK Android NDK, Revision 10 (July 2014) Platform(32-bit target) Package Size (Bytes) MD5 Ch ...
- codeforces gym 100463I Yawner
//这题挂得让我怀疑我最近是不是做了什么坏事 题意:一个人有两个集合,先在其中一个集合选一个数x,然后向右走x布,然后再在另一个集合里选一个数y,向左走y步,问是否能走完数轴上所有点. 解:显然是求g ...
- 深入理解JavaScript的闭包特性 如何给循环中的对象添加事件(转载)
原文参考:http://blog.csdn.net/gaoshanwudi/article/details/7355794 初学者经常碰到的,即获取HTML元素集合,循环给元素添加事件.在事件响应函数 ...
- ORACLE表空间bigfile和smallfile
BIGFILE | SMALLFILE Use this clause to determine whether the tablespace is a bigfile or smallfile ta ...
- php正则表达式匹配函数
<?php function show($var=null){ if(empty($var)) { echo 'null'; }else if(is_array($var) || is_obj ...
- asp.net 跨页面传值常用方法
常用方法有以下: 1.queryString 2.form-post控件传递 3.cookie 4.application 5.session querystring: http://website. ...