POJ 题目3264 Balanced Lineup(RMQ)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 39046 | Accepted: 18291 | |
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
ac代码
#include<stdio.h>
#include<string.h>
#include<math.h>
#define max(a,b) (a>b? a:b)
#define min(a,b) (a>b? b:a)
int minv[50050][20],maxv[50050][20];
int a[50050];
void init(int n)
{
int i,j,k;
for(i=1;i<=n;i++)
{
maxv[i][0]=minv[i][0]=a[i];
}
for(j=1;(1<<j)<=n;j++)
{
for(k=1;k+(1<<j)-1<=n;k++)
{
minv[k][j]=min(minv[k][j-1],minv[k+(1<<(j-1))][j-1]);
maxv[k][j]=max(maxv[k][j-1],maxv[k+(1<<(j-1))][j-1]);
}
}
}
int q_max(int l,int r)
{
int k=(int)(log((double)(r-l+1))/(log(2.0)));
return max(maxv[l][k],maxv[r-(1<<k)+1][k]);
}
int q_min(int l,int r)
{
int k=(int)(log((double)(r-l+1))/(log(2.0)));
return min(minv[l][k],minv[r-(1<<k)+1][k]);
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
init(n);
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",q_max(l,r)-q_min(l,r));
}
}
}
POJ 题目3264 Balanced Lineup(RMQ)的更多相关文章
- poj 3264 Balanced Lineup (RMQ)
/******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
- 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: 34140 Accepted: 16044 ...
- poj 3264 Balanced Lineup (RMQ算法 模板题)
RMQ支持操作: Query(L, R): 计算Min{a[L],a[L+1], a[R]}. 预处理时间是O(nlogn), 查询只需 O(1). RMQ问题 用于求给定区间内的最大值/最小值问题 ...
- POJ 3264 Balanced Lineup -- RMQ或线段树
一段区间的最值问题,用线段树或RMQ皆可.两种代码都贴上:又是空间换时间.. RMQ 解法:(8168KB 1625ms) #include <iostream> #include < ...
- POJ 3264 Balanced Lineup RMQ ST算法
题意:有n头牛,编号从1到n,每头牛的身高已知.现有q次询问,每次询问给出a,b两个数.要求给出编号在a与b之间牛身高的最大值与最小值之差. 思路:标准的RMQ问题. 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裸题)
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 43168 Accepted: 20276 ...
随机推荐
- java利用反射访问类的私有(private)属性及方法
Java语言中,在一个类中,为了不让外界访问到有的属性和方法,通常将其设置为private,用正常的方式(对象名.属性名,对象名.方法名)将无法访问此属性与方法,但有没有其他方法可以访问呢?答案是有的 ...
- 【bzoj2132】圈地计划 网络流最小割
题目描述 最近房地产商GDOI(Group of Dumbbells Or Idiots)从NOI(Nuts Old Idiots)手中得到了一块开发土地.据了解,这块土地是一块矩形的区域,可以纵横划 ...
- pdf生成(itextSharp)
最近在工作中遇到一个问题,客户要求将系统中的表格全部导出成PDF格式.经过搜索,基本是三种思路: 直接用byte写PDF文件.(算你狠,霸王硬上弓) 通过Com组件转换.以Adobe Acrobat为 ...
- 分布式文件系统FastDFS集群部署
1.源码开放下载地址:https://github.com/happyfish100 早期源码开放下载地址:https://sourceforge.net/projects/fastdfs/files ...
- 冬训 day2
模拟枚举... A - New Year and Buggy Bot(http://codeforces.com/problemset/problem/908/B) 暴力枚举即可,但是直接手动暴力会非 ...
- AVRStudio 6 添加调试功能
下载这个文件并安装就可以了:http://avr-jungo-usb.software.informer.com/download/ 具体可以看这个贴子:http://electronics.stac ...
- 【HugeChm】HugeChm制作chm帮助文档
1.下载软件:HugeChm.exe 2.开始打包: 3.选择开始打包即可
- css 之 position定位
position属性一共有4个值,分别是static.absolute.relative.fixed. static为默认值,指块保持在原本应该在的位置上,即该值没有任何移动的效果. absolute ...
- C# 的 String.CompareTo Equals和==的比较
String.CompareTo 语法 public int CompareTo( string strB) 返回值 小于 0,实例小于参数 strB: 0,实例等于参数 strB: 大于 0, ...
- window postgresql 10.4安装
window installer下载地址:https://www.enterprisedb.com/downloads/postgres-postgresql-downloads 其他版本官网下载地址 ...