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. Oracle set autotrace 时提示:Cannot find the Session Identifier. Check PLUSTRACE role is enabled

    SQL> set autotrace Usage: SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]] SQL ...

  2. Configuring HTTP and HTTPS

    Configuring HTTP and HTTPS .NET Framework (current version)   Other Versions   WCF services and clie ...

  3. Memcached和Redis异同

    在考虑对应用程序的性能表现进行提升时,缓存机制往往是解决问题的重要起点,而Memcached与Redis则经常被作为初步方案来加以比较.这两套声名显赫的缓存引擎拥有着诸多相似之处,但它们同样也具备大量 ...

  4. c语言筛选质数

    #include <stdio.h> #include <stdlib.h> #include <math.h> int isit(int num) { int i ...

  5. 【转】USB协议架构及驱动架构

    1. USB协议 1.1 USB主机系统 在USB主机系统中,通过根集线器与外部USB从机设备相连的处理芯片,称为USB主机控制器.USB主机控制器包含硬件.软件和固件一部分. 1.2 USB设备系统 ...

  6. Java 常调用的Webservice接口的方法

    WebService是基于Web的服务,WebService使用SOAP协议实现跨编程语言和跨操作系统平台,接收和响应外部系统的某种请求,从而实现远程调用.WebService采用HTTP协议传输数据 ...

  7. poj 2046 Gap(bfs+hash)

    Description Let's play a card game called Gap. You have cards labeled with two-digit numbers. The fi ...

  8. tool - 支持TestLink 1.93,将excel格式用例转化成可以导入的xml格式

     tool - 支持TestLink 1.93,将excel格式用例转化成可以导入的xml格式  https://github.com/zhangzheyuk/CaseConvert

  9. 手动同步chrome浏览器

    chrome浏览器每次设置好的标签在重新开机后都会变回设置前的状态,崩溃,每次设置好后还是手动同步一下吧. 1. 点击 工具(右上角的三个点)-->设置 2. 点击 高级同步设置 3. 点击 使 ...

  10. Java操作属性文件,支持新增或更新多个属性

    Java操作属性文件.支持新增或更新多个属性 一.更新或新增单个属性的方法 /** * 写入properties信息 * @param filePath 绝对路径(包含文件名称和后缀名) * @par ...