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

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

Submit Status

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.. NQ+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

1.朴素(遍历): 复杂度O(n)-O(qn)。

2.线段树 :复杂度O(n)-O(qlogn)。

3.ST(Sparse Table)算法 :O(nlogn)-O(q)

说下ST算法,由于每一个查询仅仅有O(1)。在处理大量查询的时候有优势。

<1>.预处理(动态规划DP)

对A[i]数列,F[i][j] 表示从第i个数起连续2^j 中的最大值(DP的状态),能够看到,F[i][0] 表示的是A[i](DP的初始值)。

最后。状态转移方程是

F[i][j]=max(F[i][j-1],F[i+2^(j-1)][j-1])

<2>查询

若查询区间为(a。b),区间长度为b-a+1,取k=log2(b-a+1),则Max(a。b)=max(F[a][k]。F[b-2^k+1][k])。

1.ST算法

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std; const int MAXN = 50050; int mins[MAXN][20];
int maxs[MAXN][20]; void RMQ(int n)
{
for (int j = 1; (1 << j) <= n;j++)
for (int i = 1; i + (1 << j) - 1 <= n; i++)
{
int p = (1 << (j - 1));
mins[i][j] = min(mins[i][j - 1], mins[i + p][j - 1]);
maxs[i][j] = max(maxs[i][j - 1], maxs[i + p][j - 1]);
}
} int queryMin(int l, int r)
{
int k = log((double)(r - l + 1))/log(2.0);
return min(mins[l][k], mins[r - (1 << k) + 1][k]);
} int queryMax(int l, int r)
{
int k = log((double)(r - l + 1))/log(2.0);
return max(maxs[l][k], maxs[r - (1 << k) + 1][k]);
} int main()
{
int n, q;
scanf("%d%d", &n, &q);
int num;
for (int i = 1; i <= n; i++)
{
scanf("%d", &num);
maxs[i][0] = mins[i][0] = num;
}
RMQ(n);
int a, b;
int ans;
for (int i = 0; i < q; i++)
{
scanf("%d%d", &a, &b);
ans= queryMax(a, b) - queryMin(a, b);
printf("%d\n", ans);
}
}

2.线段树

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b) using namespace std; const int MAXN = 50050; int num[MAXN]; struct node
{
int r;
int l;
int Max;
int Min;
}tree[3*MAXN]; void build(int l, int r, int i)
{
tree[i].l = l; tree[i].r = r;
if (l == r)
{
tree[i].Max = tree[i].Min = num[l];
return;
}
int m = (l + r) >> 1, ls = i << 1, rs = ls + 1;
build(l, m, ls);
build(m + 1, r, rs);
tree[i].Max = max(tree[rs].Max, tree[ls].Max);
tree[i].Min = min(tree[rs].Min, tree[ls].Min);
} int queryMax(int l, int r, int i)
{
if (tree[i].l == l&&tree[i].r == r)
return tree[i].Max;
int m = (tree[i].l + tree[i].r) >> 1, ls = i << 1, rs = ls + 1;
if (r <= m) return queryMax(l, r, ls);
else if (l > m) return queryMax(l, r, rs);
else return max(queryMax(l, m, ls), queryMax(m + 1, r, rs));
} int queryMin(int l, int r, int i)
{
if (tree[i].l == l&&tree[i].r == r)
return tree[i].Min;
int m = (tree[i].l + tree[i].r) >> 1, ls = i << 1, rs = ls + 1;
if (r <= m) return queryMin(l, r, ls);
else if (l > m) return queryMin(l, r, rs);
else return min(queryMin(l, m, ls), queryMin(m + 1, r, rs));
} int main()
{
int n, q;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++)
scanf("%d", &num[i]);
build(1, n, 1);
int a, b;
int ans;
for (int i = 0; i < q; i++)
{
scanf("%d%d", &a, &b);
ans = queryMax(a, b, 1) - queryMin(a, b, 1);
printf("%d\n", ans);
}
}

參考了http://blog.csdn.net/niushuai666/article/details/6624672/

POJ - 3264 Balanced Lineup (RMQ问题求区间最值)的更多相关文章

  1. Poj 3264 Balanced Lineup RMQ模板

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

  2. poj 3264 Balanced Lineup (RMQ)

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

  3. poj 3264 Balanced Lineup【RMQ-ST查询区间最大最小值之差 +模板应用】

    题目地址:http://poj.org/problem?id=3264 Sample Input 6 3 1 7 3 4 2 5 1 5 4 6 2 2 Sample Output 6 3 0分析:标 ...

  4. POJ 3264 Balanced Lineup 【线段树/区间最值差】

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 62103 Accepted: 29005 Cas ...

  5. POJ 3264 Balanced Lineup RMQ ST算法

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

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

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

  7. POJ 3264 Balanced Lineup -- RMQ或线段树

    一段区间的最值问题,用线段树或RMQ皆可.两种代码都贴上:又是空间换时间.. RMQ 解法:(8168KB 1625ms) #include <iostream> #include < ...

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

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

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

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

随机推荐

  1. 【转载】Java并发编程:volatile关键字解析 by 海子

    volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...

  2. linux automake 交叉编译

    . ├── aclocal.m4 ├── autoscan.log ├── config.log ├── config.status ├── configure ├── configure.in ├─ ...

  3. UVA 11107(Life Forms-后缀数组+二分)

    Problem C: Life Forms You may have wondered why most extraterrestrial life forms resemble humans, di ...

  4. 对 getaddrinfo Android 返回错误 EAI_BADFLAGS

    我们尝试使用 getaddrinfo 对 Android API 14 及以上 (在 c + + 代码使用 NDK r12) 从 IPV4 获得合成的 IPV6 地址 address .这是在 IPV ...

  5. vnstat 查看服务器带宽统计命令

    vnStat是一个Linux下的网络流量监控软件,它记录指定网卡每日的传输流量日志. 它并非基于网络包的过滤,而是分析文件系统- /proc, 所以vnStat无需root的权限就可使用. ,它还自带 ...

  6. PHP实现双向链表、栈

    前期写过一个PHP实现单向链表.实现排序单向链表的一篇文章,传送门:http://www.cnblogs.com/yydcdut/p/3777760.html.双向链表写过了,再拿出来提一提:http ...

  7. .NET-MVC站点部署到windows server2008r2服务器404错误

    iis站点搭建 产生原因: 由于服务器上的.net4.0没有进行注册导致的 解决方法: 注册.net 4.0 打开运行-cmd-输入如下命令: C:\WINDOWS\Microsoft.NET\Fra ...

  8. Error: Cannot find module 'express' 之 解决方案

    出现如题错误,是因为执行了#npm install -g express的缘故,express没有被写到package.json里面去. 解决也好办,在程序目录下执行#npm install expr ...

  9. (C++)浅谈using namespace std

    1.<iostream>和<iostream.h> 在你的编译器include文件夹里面可以看到,二者是两个文件,里面的代码是不一样的. 后缀为.h的头文件c++标准已经明确提 ...

  10. 流操作结束后,一定要调用close(). java有垃圾回收器, 这样做是多此一举吗?

    流不单在内存中分配了空间,也在操作系统占有了资源, java的gc是能从内存中回收不使用的对象, 但对操作系统分配的资源是无能为力的, 所以就要调用close()方法来通知OS来释放这个资源.