POJ3264(RMQ-ST算法)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 47087 | Accepted: 22101 | |
| 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
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN=;
int n,m;
int maxm[MAXN][],minm[MAXN][];
void init_st(int size)
{
for(int j=;j<;j++)
{
for(int i=;i<=n;i++)
{
if(i+(<<j)-<=n)
{
maxm[i][j]=max(maxm[i][j-],maxm[i+(<<(j-))][j-]);
minm[i][j]=min(minm[i][j-],minm[i+(<<(j-))][j-]);
}
}
}
}
int rmq_st(int l,int r)
{
int limit=(int)(log(0.0+(r-l+))/log(2.0));
int mn=min(minm[l][limit],minm[r-(<<limit)+][limit]);
int mx=max(maxm[l][limit],maxm[r-(<<limit)+][limit]);
return mx-mn;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
{
scanf("%d",&maxm[i][]);
minm[i][]=maxm[i][];
}
init_st(n);
for(int i=;i<m;i++)
{
int l,r;
scanf("%d%d",&l,&r);
int res=rmq_st(l,r);
printf("%d\n",res);
}
}
return ;
}
POJ3264(RMQ-ST算法)的更多相关文章
- [POJ3264]Balanced Lineup(RMQ, ST算法)
题目链接:http://poj.org/problem?id=3264 典型RMQ,这道题被我鞭尸了三遍也是醉了…这回用新学的st算法. st算法本身是一个区间dp,利用的性质就是相邻两个区间的最值的 ...
- 求解区间最值 - RMQ - ST 算法介绍
解析 ST 算法是 RMQ(Range Minimum/Maximum Query)中一个很经典的算法,它天生用来求得一个区间的最值,但却不能维护最值,也就是说,过程中不能改变区间中的某个元素的值.O ...
- 【原创】RMQ - ST算法详解
ST算法: ID数组下标: 1 2 3 4 5 6 7 8 9 ID数组元素: 5 7 3 1 4 8 2 9 8 1.ST算法作 ...
- HDU 3183 - A Magic Lamp - [RMQ][ST算法]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3183 Problem DescriptionKiki likes traveling. One day ...
- POJ 3264 Balanced Lineup RMQ ST算法
题意:有n头牛,编号从1到n,每头牛的身高已知.现有q次询问,每次询问给出a,b两个数.要求给出编号在a与b之间牛身高的最大值与最小值之差. 思路:标准的RMQ问题. RMQ问题是求给定区间内的最值问 ...
- 关于基础RMQ——ST算法
RMQ,Range Maximum/Minimum Query,顾名思义,就是询问某个区间内的最大值或最小值,今天我主要记录的是其求解方法--ST算法 相对于线段树,它的运行速度会快很多,可以做到O( ...
- POJ3264 (RMQのST解法)
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...
- POJ 3368 Frequent values RMQ ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
- RMQ st算法 区间最值模板
#include<bits/stdc++.h> ; ; int f[N][Logn],a[N],lg[N],n,m; int main(){ cin>>n>>m; ...
- RMQ问题(线段树+ST算法)
转载自:http://kmplayer.iteye.com/blog/575725 RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ ...
随机推荐
- gem Errno::ECONNRESET: Connection reset by peer - SSL_connect
问题描述 在使用gem安装软件包时,会时常遇到下面的问题: ERROR: While executing gem ... (Gem::RemoteFetcher::FetchError) Errno: ...
- FreeBSD 安装过程
FreeBSD安装步骤: 回车 按默认回车 输入服务器的计算机名 去掉games,加上src安装如下图 Lib32 ports src这三项一定要安装上 回车 选择Manual 进入以后点create ...
- StringTemplateLoader的用法
作为一个模板框架,freemarker的功能还是很强大的.在模板处理方面,freemarker有多种形式,最常见的方式是将模板文件放在一个统一的文件夹下面,如下形式:Configuration cfg ...
- Luogu-1527 [国家集训队]矩阵乘法
Luogu-1527 [国家集训队]矩阵乘法 题面 Luogu-1527 题解 昨天学CDQ分治时做了一些题,但是因为题(wo)太(tai)水(lan)了(le)并没有整理 学了一晚上的整体二分,拿这 ...
- Luogu-3346 [ZJOI2015]诸神眷顾的幻想乡
\(trie\)树建广义后缀自动机: \(dfs\)遍历\(trie\)树,将树上的一个节点插入\(sam\)时,将他的\(fa\)在\(sam\)上所在的节点作为\(last\) #include& ...
- 4950: [Wf2017]Mission Improbable
4950: [Wf2017]Mission Improbable Time Limit: 1 Sec Memory Limit: 512 MBSubmit: 608 Solved: 222[Sub ...
- spring boot 默认配置bug
问题场景:请求很耗时,当一次请求完成后,之后的20秒内的请求很快速,在之后的第一个请求很慢! 每隔一段时间,请求就会出发解压jar的操作,不确定是操作系统的问题还是sping-boot的bug &qu ...
- JavaScript -- 练习 window 流氓广告
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- UOJ66 新年的巧克力棒
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Installing StackTach
为StackTach创建database,默认使用MySql,也可以 在settings.py 文件中配置其他的. create stack db mysql -u root -p mysql> ...