传送门:http://poj.org/problem?id=3264

Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 66241   Accepted: 30833
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

题意概括:

给出一段长度为 N 的序列,和 Q 次查询。

每次输入区间,求解该区间 最大值 - 最小值的结果。

解题思路:

RMQ问题可以线段树维护(甚至树状数组)复杂度 预处理 O(NlongN) 单次查询 O(logN)

不过这里用的是 ST表 预处理O(NlogN) 单次查询 O(1)

ST表 本质思想是 dp,这里用两个dp 维护区间最大值和最小值。

设 起点是 i 区间长度为

区间 【i , i +(1 << j )】的最大值为 dpmax[ i, j ],最小值为 dpmin[ i, j ]

转移方程:

dpmax[ i, j ] = max( dpmax[ i ][ j-1 ], dpmax[ i + (1<<(j-1)) ][ j ] );

dpmin[ i, j ] = min( dpmin[ i ][ j-1 ], dpmin[ i + (1<<(j-1)) ][ j ] );

实质就是按照二的幂次方关系,把一个区间分成了两个区间。

AC code:

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 5e4+;
int dpmax[MAXN][];
int dpmin[MAXN][];
int num[MAXN]; void make_maxRMQ(int N, int b[])
{
for(int i = ; i < N; i++){
dpmax[i][] = b[i];
}
for(int ilen = ; (<<ilen) <= N; ilen++)
for(int i = ; i+(<<ilen)- < N; i++){
dpmax[i][ilen] = max(dpmax[i][ilen-], dpmax[i+(<<(ilen-))][ilen-]);
}
} int get_max(int ll, int rr)
{
int k = (int)(log(rr-ll+1.0)/log(2.0));
return max(dpmax[ll][k], dpmax[rr-(<<k)+][k]);
} void make_minRMQ(int N, int a[])
{
for(int i = ; i < N; i++){
dpmin[i][] = a[i];
}
for(int ilen = ; (<<ilen) <= N; ilen++)
for(int i = ; i+(<<ilen)- < N; i++){
dpmin[i][ilen] = min(dpmin[i][ilen-], dpmin[i+(<<(ilen-))][ilen-]);
}
} int get_min(int ll, int rr)
{
int k = (int)(log(rr-ll+1.0)/log(2.0));
return min(dpmin[ll][k], dpmin[rr-(<<k)+][k]);
} int main()
{
int N, Q;
int L, R;
int ans;
while(~scanf("%d%d", &N, &Q)){
for(int i = ; i < N; i++){
scanf("%d", &num[i]);
}
make_maxRMQ(N, num);
make_minRMQ(N, num); while(Q--){
scanf("%d%d", &L, &R);
L--, R--;
ans = get_max(L, R) - get_min(L, R);
printf("%d\n", ans);
}
}
return ;
}

POJ 3264 Balanced Lineup 【ST表 静态RMQ】的更多相关文章

  1. POJ 3264 Balanced Lineup | st表

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

  2. [POJ] 3264 Balanced Lineup [ST算法]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  3. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  4. POJ - 3264 Balanced Lineup 线段树解RMQ

    这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...

  5. Poj 3264 Balanced Lineup RMQ模板

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

  6. poj 3264 Balanced Lineup (RMQ)

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

  7. POJ - 3264 Balanced Lineup (RMQ问题求区间最值)

    RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A中下标在i,j里的最小(大)值,也就 ...

  8. POJ 3264 Balanced Lineup(ST模板)

    链接:http://poj.org/problem?id=3264 题意:给n个数,求一段区间L,R的最大值 - 最小值,Q次询问 思路:ST表模板,预处理区间最值,O(1)复杂度询问 AC代码: # ...

  9. poj 3264 Balanced Lineup(RMQ裸题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 43168   Accepted: 20276 ...

随机推荐

  1. PHP学习2——基本语法

    主要内容: 二进制 数据类型 变量 常量 赋值 语句结构 函数 网站的核心功能是展现信息,文字,图片,视频,音频,对于计算机来说都是数据,这些数据按照二进制进行存储. 二进制 就是1100,0100, ...

  2. JMS - 基于JMS的RPC

    现在试试通过JMS,在应用程序之间发送消息.先看看spring提供的RPC方案(其实还有其他方案,只是没见过谁用).需要使用到这两个类:·org.springframework.jms.remotin ...

  3. JS实现队列

    JS实现队列: 队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插入操作的端称为队尾 ...

  4. django-admin管理后台高级自定义

    django自带的admin后台管理系统,在很多网站中被称为django的杀手级的应用.那么django-admin的适用情形倒底有哪些呢,一般 来说对于大型的商业性的项目通常不用采用django-a ...

  5. js 判断id 是否存在

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  6. git必会必知

    1 前言 git前身是BitKeeper,但是他不是开源软件,不符合当时开源趋势,于是就会有了开源的git,git开发只用了十天时间.目前git是公司开发必不可少的一个工具,用于多人开发的分布式版本控 ...

  7. PHP DES解密 对应Java SHA1PRNG方式加密

    背景及问题 背景:在和外部系统通过HTTP方式跳转时, 为保障传输参数安全性, 采用AES 加密参数. 关于对称加密中 AES, DES, CBC, ECB, PKCS5Padding 概念可参考ht ...

  8. kotlin语法

    https://try.kotlinlang.org/#/Examples/Hello,%20world!/Simplest%20version/Simplest%20version.kt /** * ...

  9. js基本数据类型和引用类型的区别详解-笔记

    原文参考http://mp.weixin.qq.com/s/apFyUgqT5N-bsDUjP4Eryg 笔记总结 首先记住js中的基础数据类型undefined,null,boolean,strin ...

  10. .NET开源工作流RoadFlow-流程运行-任务收回

    如果一个任务则发送,又觉得还要想修改可以立即收回刚刚发送的任务. 任务收回条件:任务发送后下一步处理人还没有打开该任务,则在已办事项中会看到 收回 按钮,否则不能收回. 点击收回按钮再确认即可收回刚刚 ...