poj 3264(RMQ或者线段树)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 42929 | Accepted: 20184 | |
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
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
题意:区间最大值与最小值之差RMQ版:(不懂的可以参考blog)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define N 50010 int a[N];
int max_dp[N][];
int min_dp[N][];
int MAX(int i,int j){
if(i>=j) return i;
return j;
}
int MIN(int i,int j){
if(i<=j) return i;
return j;
}
void init_MAX_RMQ(int n){
for(int i=;i<=n;i++) max_dp[i][]=a[i];
for(int j=;(<<j)<=n;j++){
for(int i=;i<=n-(<<j)+;i++){
///F[i, j]=max(F[i,j-1], F[i + 2^(j-1),j-1])。
max_dp[i][j] = MAX(max_dp[i][j-],max_dp[i+(<<(j-))][j-]);
}
}
}
int MAX_RMQ(int a,int b){
int k = (int)(log(b-a+1.0)/log(2.0));
///RMQ(A, i, j)=min{F[i,k],F[j-2^k+1,k]}
return MAX(max_dp[a][k],max_dp[b-(<<k)+][k]);
}
void init_MIN_RMQ(int n){
for(int i=;i<=n;i++) min_dp[i][]=a[i];
for(int j=;(<<j)<=n;j++){
for(int i=;i<=n-(<<j)+;i++){
min_dp[i][j] = MIN(min_dp[i][j-],min_dp[i+(<<(j-))][j-]);
}
}
}
int MIN_RMQ(int a,int b){
int k = (int)(log(b-a+1.0)/log(2.0));
return MIN(min_dp[a][k],min_dp[b-(<<k)+][k]);
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
init_MAX_RMQ(n);
init_MIN_RMQ(n);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",MAX_RMQ(a,b)-MIN_RMQ(a,b));
}
}
return ;
}
线段树:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define N 50010 struct Tree{
int l,r;
int Max,Min;
}tree[*N];
int a[N];
int MAX_VALUE;
int MIN_VALUE;
int MAX(int i,int j){
if(i>=j) return i;
return j;
}
int MIN(int i,int j){
if(i<=j) return i;
return j;
}
void PushUp(int idx){
tree[idx].Max = MAX(tree[idx<<].Max,tree[idx<<|].Max);
tree[idx].Min = MIN(tree[idx<<].Min,tree[idx<<|].Min);
}
void build(int l,int r,int idx){
tree[idx].l = l;
tree[idx].r = r;
if(l==r) {
tree[idx].Max = tree[idx].Min = a[l];
return ;
}
int mid=(l+r)>>;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
PushUp(idx);
}
void query(int l,int r,int idx){
if(tree[idx].l==l&&tree[idx].r==r){
MAX_VALUE = MAX(MAX_VALUE,tree[idx].Max);
MIN_VALUE = MIN(MIN_VALUE,tree[idx].Min);
return;
}
int mid=(tree[idx].l+tree[idx].r)>>;
if(mid>=r) query(l,r,idx<<);
else if(mid<l) query(l,r,idx<<|);
else{
query(l,mid,idx<<);
query(mid+,r,idx<<|);
}
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
build(,n,);
while(m--){
int b,c;
scanf("%d%d",&b,&c);
MAX_VALUE=-;
MIN_VALUE=;
query(b,c,);
printf("%d\n",MAX_VALUE-MIN_VALUE);
}
}
return ;
}
poj 3264(RMQ或者线段树)的更多相关文章
- 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 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- 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皆可.两种代码都贴上:又是空间换时间.. RMQ 解法:(8168KB 1625ms) #include <iostream> #include < ...
- POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新
题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为 ...
- POJ 3368 Frequent values 线段树与RMQ解法
题意:给出n个数的非递减序列,进行q次查询.每次查询给出两个数a,b,求出第a个数到第b个数之间数字的最大频数. 如序列:-1 -1 1 1 1 1 2 2 3 第2个数到第5个数之间出现次数最多的是 ...
随机推荐
- ContestHunter暑假欢乐赛 SRM 05
T1 组合数,求一下乘法逆元就行了 没取模 没1LL* 爆零了 T2 让最大子段和最小就行,跑最大子段和的时候若超过S就弹出堆中最大的数,每次有负数加进来不断弹出最小的数相加重新加进堆直到为正数,因为 ...
- caffe中的Accuracy+softmaxWithLoss
转:http://blog.csdn.net/tina_ttl/article/details/51556984 今天才偶然发现,caffe在计算Accuravy时,利用的是最后一个全链接层的输出(不 ...
- AIM Tech Round (Div. 2) B
B. Making a String time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- [POI2007] ZAP-Queries (莫比乌斯反演)
[POI2007] ZAP-Queries 题目描述 Byteasar the Cryptographer works on breaking the code of BSA (Byteotian S ...
- 1143: [CTSC2008]祭祀river(最长反链)
1143: [CTSC2008]祭祀river 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1143 Description: 在遥远的 ...
- Linux下实现文档在线浏览
使用php实现百度文库功能,网上搜索到的方案,实现doc转pdf,pdf转swf,然后显示出来. 这里简单的记录下,[doc转pdf,pdf转swf]两个功能的搭建流程. doc转pdf 使用到下列程 ...
- maven插件理解
maven插件的主要功能是对用到的jar包进行管理,jar包先从本地仓库中获取,如果没有找到,则从远处中央仓库下载(需要联外网).本地仓库中的jar包可供所有maven工程使用,属于公共模块. mav ...
- CSS hack浏览器兼容一览表
CSS hack是指我们为了兼容各浏览器,而使用的特别的css定义技巧.这是国外摘来的一张CSS hack列表,显示了各浏览器对css hack的支持程度,对我们制作兼容网页非常有帮助.
- salt总结
安装jdk jdk: file.managed: - source: salt://service/zabbix/files/jdk1.8.0_121.tar.gz - name: /usr/loca ...
- SVG(可缩放矢量图形)
SVG可缩放矢量图形(Scalable Vector Graphics)是基于可扩展标记语言(XML),用于描述二维矢量图形的一种图形格式.SVG是W3C("World Wide W ...