Balanced Lineup
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

Line 1: Two space-separated integers, N and Q
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

Lines 1..Q: 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问题。Sparse-Table算法的应用。预处理时间复杂度O(nlogn),查询时间复杂度O(1);
 
代码:
 #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算法]的更多相关文章

  1. poj 3264 Balanced Lineup (RMQ算法 模板题)

    RMQ支持操作: Query(L, R):  计算Min{a[L],a[L+1], a[R]}. 预处理时间是O(nlogn), 查询只需 O(1). RMQ问题 用于求给定区间内的最大值/最小值问题 ...

  2. POJ 3264 Balanced Lineup | st表

    题意: 求区间max-min st表模板 #include<cstdio> #include<algorithm> #include<cstring> #inclu ...

  3. Poj 3264 Balanced Lineup RMQ模板

    题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...

  4. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

  5. POJ 3264 Balanced Lineup RMQ ST算法

    题意:有n头牛,编号从1到n,每头牛的身高已知.现有q次询问,每次询问给出a,b两个数.要求给出编号在a与b之间牛身高的最大值与最小值之差. 思路:标准的RMQ问题. RMQ问题是求给定区间内的最值问 ...

  6. poj 3264 Balanced Lineup 题解

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Subm ...

  7. poj 3264 Balanced Lineup (RMQ)

    /******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...

  8. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  9. POJ - 3264——Balanced Lineup(入门线段树)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 68466   Accepted: 31752 ...

随机推荐

  1. java 发送 http 请求

    public class VoteHandler implements IVoteHandler { private static final Logger LOGGER = LoggerFactor ...

  2. 运用Swagger 添加WebAPI 文档

    1. Go to Web link https://www.nuget.org/packages/Swashbuckle/ and check which version do we want. 2. ...

  3. 关于TCP的三次握手和四次分手(整理)

    这个协议非常重要,这里把它的链接和释放整理一下 首先是三次握手: 1.  客户端发起,像服务器发送的报文SYN=1,ACK=0,然后选择了一个初始序号:seq=x. SYN是干什么用的? 在链接的时候 ...

  4. js 获取input file路径改变图像地址

    html代码 <img id="newImage" alt="100x100" src="__PUBLIC__/img/1.jpg" ...

  5. Spring 源码解读 推荐流程

    Spring源代码解析(一):IOC容器:http://www.javaeye.com/topic/86339 Spring源代码解析(二):IoC容器在Web容器中的启动:http://www.ja ...

  6. jQuery中ready与load事件的区别

    1.摘要 大家在编程中使用jQuery还有JS的时候一定会在使用之前这样: //document ready $(document).ready(function(){ ...code... }) / ...

  7. Live555 实战之框架简单介绍

    作者:咕唧咕唧liukun321 来自:http://blog.csdn.net/liukun321 上一篇文章简要介绍了怎样以共享库的方式交叉编译Live555,今天再来介绍live源代码框架. 先 ...

  8. AVL旋转树

    执行插入操作可能出现不平衡的情况,当平衡二叉树.AVL这树是一种自平衡二叉树,使二叉树又一次保持平衡.而且查找.插入和删除操作在平均和最坏情况下时间复杂度都是O(log n) AVL树的旋转一共同拥有 ...

  9. Cortex-A9 PWM Timer

    PWM定时器        4412时钟为我们提供了PWM定时器,在4412中共有5个32位的定时器,这些定时器可发送中断信号给ARM子系统.另外,定时器0.1.2.3包含了脉冲宽度调制(PWM),并 ...

  10. Easyui 排序时 自动向后排传sort order 你妹真坑爹

    request 的时候 发现 sort 竟然是个数组!