POJ-3264 Balanced Lineup(区间最值,线段树,RMQ)
http://poj.org/problem?id=3264
Time Limit: 5000MS Memory Limit: 65536K
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
Sample Output
Source
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
//const double PI=acos(-1);
const int maxn=1e5+;
using namespace std;
//ios::sync_with_stdio(false);
// cin.tie(NULL); int n,q;
struct node
{
int l;
int r;
int MAX;
int MIN;
}SegTree[<<]; void PushUp(int rt)
{
SegTree[rt].MAX=max(SegTree[rt<<].MAX,SegTree[rt<<|].MAX);
SegTree[rt].MIN=min(SegTree[rt<<].MIN,SegTree[rt<<|].MIN);
} void Build(int l,int r,int rt)
{
SegTree[rt].l=l;
SegTree[rt].r=r;
if(l==r)
{
scanf("%d",&SegTree[rt].MAX);
SegTree[rt].MIN=SegTree[rt].MAX;
return;
}
int mid=(l+r)>>;
Build(l,mid,rt<<);
Build(mid+,r,rt<<|);
PushUp(rt);
} int Query_MAX(int L,int R,int rt)
{
int l=SegTree[rt].l;
int r=SegTree[rt].r;
if(L<=l&&R>=r)//一次也没有被涂过
{
return SegTree[rt].MAX;
}
int MAX=;
int mid=(l+r)>>;
if(L<=mid)
MAX=max(MAX,Query_MAX(L,R,rt<<));
if(R>mid)
MAX=max(MAX,Query_MAX(L,R,rt<<|));
return MAX;
} int Query_MIN(int L,int R,int rt)
{
int l=SegTree[rt].l;
int r=SegTree[rt].r;
if(L<=l&&R>=r)//一次也没有被涂过
{
return SegTree[rt].MIN;
}
int MIN=INF;
int mid=(l+r)>>;
if(L<=mid)
MIN=min(MIN,Query_MIN(L,R,rt<<));
if(R>mid)
MIN=min(MIN,Query_MIN(L,R,rt<<|));
return MIN;
} int main()
{
scanf("%d %d",&n,&q);
Build(,n,);
for(int i=;i<=q;i++)
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d\n",Query_MAX(a,b,)-Query_MIN(a,b,));
}
return ;
}
#include<iostream>
#include<cstring>
#include<cstdio>
#include<climits>
#include<cmath>
#include<algorithm>
using namespace std; const int N = ;
int FMAX[N][], FMIN[N][]; void RMQ(int n)
{
for(int j = ; j != ; ++j)
{
for(int i = ; i <= n; ++i)
{
if(i + ( << j) - <= n)
{
FMAX[i][j] = max(FMAX[i][j - ], FMAX[i + ( << (j - ))][j - ]);
FMIN[i][j] = min(FMIN[i][j - ], FMIN[i + ( << (j - ))][j - ]);
}
}
}
} int main()
{
int num, query;
int a, b;
while(scanf("%d %d", &num, &query) != EOF)
{
for(int i = ; i <= num; ++i)
{
scanf("%d", &FMAX[i][]);
FMIN[i][] = FMAX[i][];
}
RMQ(num);
while(query--)
{
scanf("%d%d", &a, &b);
int k = (int)(log(b - a + 1.0) / log(2.0));
int maxsum = max(FMAX[a][k], FMAX[b - ( << k) + ][k]);
int minsum = min(FMIN[a][k], FMIN[b - ( << k) + ][k]);
printf("%d\n", maxsum - minsum);
}
}
return ;
}
POJ-3264 Balanced Lineup(区间最值,线段树,RMQ)的更多相关文章
- POJ 3264 Balanced Lineup 区间最值
POJ3264 比较裸的区间最值问题.用线段树或者ST表都可以.此处我们用ST表解决. ST表建表方法采用动态规划的方法, ST[I][J]表示数组从第I位到第 I+2^J-1 位的最值,用二分的思想 ...
- poj 3264 Balanced Lineup 区间极值RMQ
题目链接:http://poj.org/problem?id=3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) alw ...
- POJ 3264.Balanced Lineup-结构体版线段树(区间查询最值)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 53721 Accepted: 25244 ...
- POJ 3264 Balanced Lineup(模板题)【RMQ】
<题目链接> 题目大意: 给定一段序列,进行q次询问,输出每次询问区间的最大值与最小值之差. 解题分析: RMQ模板题,用ST表求解,ST表用了倍增的原理. #include <cs ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
- POJ 3264 Balanced Lineup 【ST表 静态RMQ】
传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total S ...
- POJ - 3264 Balanced Lineup (RMQ问题求区间最值)
RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就 ...
- POJ 3264 Balanced Lineup 【线段树/区间最值差】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 62103 Accepted: 29005 Cas ...
- POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 53703 Accepted: 25237 ...
- POJ - 3264——Balanced Lineup(入门线段树)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 68466 Accepted: 31752 ...
随机推荐
- 理解Spring Boot Actuator
Spring Boot Actuator 用于监控和管理spring应用,可通过HTTP Endpoint或JMX Bean与其交互.
- Java中的合并与重组(下)
通过优锐课核心java学习笔记中,我们可以看到,码了很多专业的相关知识, 分享给大家参考学习. Java中的合并与重组上部分链接:https://www.cnblogs.com/youruike1/p ...
- Digital Twin——IoT的下一个浪潮
选自Medium 作者:Tomasz Kielar 京东云开发者社区编译 当前的商业格局受到了众多新技术的影响,有时确实让人很难能跟上技术迭新的速度.虽然很多人都熟悉工业4.0和物联网(IoT)这两个 ...
- 3D打印前途光明,它需要怎样的进化?
在很长一段时间内,笔者都认为3D打印只会存在于科幻场景内,众多的科技大佬在前几年也和我保持相当一致的看法,代工大王郭台铭曾口出狂言:如果3D打印能够普及,我就把"郭"字倒过来写,时 ...
- 1. laravel 学习 环境搭建
1. 项目环境 vagrant + laradock (因为 自己手动搭建环境太麻烦了 自己弄了一下 感觉还是有些漏洞 所以采用 laradock) 2. Vagrantfile 备注 : box ...
- Python说文解字_杂谈01
1. Python在Ubuntu下面下载Python 2. 安装依赖包 sudo apt-get update sudo apt-get install build-essential python- ...
- InnoDB和MyISAM区别总结
原来是MyISAM类型不支持事务处理等高级处理,而InnoDB类型支持. MyISAM类型的表强调的是性能,其执行数度比InnoDB类型更快,但是不提供事务支持,而InnoDB提供事务支持已经外部键等 ...
- 01 语言基础+高级:1-8 File类与IO流_day08【 File类、递归】
day08[File类.递归] 主要内容 File类 递归 教学目标 能够说出File对象的创建方式 能够说出File类获取名称的方法名称 能够说出File类获取绝对路径的方法名称 能够说出File类 ...
- PAT Vocabulary
数字 单词 词性 中文意 常用 音标 non-negative 非负数 non-negative number Positive 正整数 Positive integer Negative 负数 Ne ...
- Chrome使用频率最高的快捷键
标签 ctrl+T 打开新标签 ——— ctrl+W 关闭标签 ctrl+shift+T 打开上衣个被关闭的标签 ctrl+tab 标签向右切换 —— ctrl+shift+tab 标签向左切换 c ...