[POJ] 3264 Balanced Lineup [ST算法]
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 34306 | Accepted: 16137 | |
| 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<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdbool.h> #define rep(i,a,b) for(i=(a);i<=(b);i++)
#define clr(x,y) memset(x,y,sizeof(x))
#define sqr(x) (x*x)
#define LL long long int i,j,n,m,x,y,q,maxn,minn,
d[][],f[][]; int min(int a,int b)
{
if(a<b) return a;
return b;
} int max(int a, int b)
{
if(a>b) return a;
return b;
} int RMQ_init()
{
int i; for(j=;(<<j)<=n;j++)
for(i=;i+(<<j)-<=n;i++) {
d[i][j]=min(d[i][j-],d[i+(<<(j-))][j-]);
f[i][j]=max(f[i][j-],f[i+(<<(j-))][j-]);
}
} int RMQ(int L,int R)
{
int k=; while(<<(k+)<=R-L+) k++;
minn=min(d[L][k],d[R-(<<k)+][k]);
maxn=max(f[L][k],f[R-(<<k)+][k]);
} int main()
{
int i; clr(d,);
clr(f,-);
scanf("%d%d",&n,&q);
rep(i,,n) {
scanf("%d",&d[i][]);
f[i][]=d[i][];
} RMQ_init(); while(q--) {
scanf("%d%d",&x,&y);
RMQ(x,y);
printf("%d\n",maxn-minn);
} return ;
}
[POJ] 3264 Balanced Lineup [ST算法]的更多相关文章
- 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 | st表
		
题意: 求区间max-min st表模板 #include<cstdio> #include<algorithm> #include<cstring> #inclu ...
 - 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  ST算法
		
题意:有n头牛,编号从1到n,每头牛的身高已知.现有q次询问,每次询问给出a,b两个数.要求给出编号在a与b之间牛身高的最大值与最小值之差. 思路:标准的RMQ问题. RMQ问题是求给定区间内的最值问 ...
 - poj 3264 Balanced Lineup 题解
		
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Subm ...
 - poj 3264 Balanced Lineup (RMQ)
		
/******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...
 - 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 ...
 
随机推荐
- Effective Java实作Comparator - 就是爱Java
			
如果集合或数组内的对象,有1个以上不同的排序逻辑时,那该如何处理呢?尤其是当已经实现了Comparable,又不能变动原本的逻辑时,Mix会采用Comparator来处理. 阅读全文>>
 - 调用Android系统设置中的Intent
			
开发Android软件时,常常需要打开系统设置或信息界面,来设置相关系统项或查看系统的相关信息,这时我们就可以使用以下语句来实现:(如打开“无线和网络设置”界面) Intent intent = ne ...
 - Http Authentication Java
			
http://docs.oracle.com/javase/7/docs/technotes/guides/net/http-auth.html Http Authentication Overvie ...
 - 关于group by
			
<pre name="code" class="sql">关于group by 排序问题 10g 以前sort group by 需要排序 10g ...
 - 能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而是可以按片自由搭配 | 36氪
			
能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而是可以按片自由搭配 | 36氪 能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而 ...
 - 马士兵Servlet&Jsp学习
			
Servlet&JSP 1>http常见错误信息: 404--url地址找不找, 403--禁止访问 500--服务器内部错误 2>Servlet的生命周期: *生命全过 ...
 - linux shell在while中用read从键盘输入
			
系统是ubuntu 14.04 64bit,之前曾想安装Stream来玩dota2,但最终没成功.由于Stream只有32bit,安装Stream时也安装了大量32bit的库.删除Stream后,这些 ...
 - uva 11081 - Strings(LCS)
			
题目链接:11081 - Strings 题目大意:给出三个字符串,从分别从第一个字符串和第二个字符串中挑选子串a,b,用a和b组成第三个字符串,问可组成的子串有多少种. 解题思路:说起来惭愧啊,题目 ...
 - 可用与禁用 E:enabled { sRules }
			
<!DOCTYPE html><html lang="zh-cmn-Hans"><head><meta charset="utf ...
 - SqlDbType与DbType这间的转换关系
			
SqlDbType => DbType SqlDbType.BigInt DbType.Int64 SqlDbType.Binary DbType.Binary SqlDbType.Bit Db ...