RMQ_st

RMQ问题的st算法,可以说是求区间最值很常用的算法,之前一直不知道,直到15年网络赛一道很简单的题看了犇哥用了这种方法决定脑补一下。虽然已经过去半年了,还是磨出来了,以poj的一道题为例。

poj 3264

Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 42407   Accepted: 19957
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
意思是数组长度是n,有q次询问,输出[A,B]中最大值和最小值的差。数据量非常大。

直接贴代码吧

/*
 * rmp_st.cpp
 *
 *  Created on: 2016年3月4日
 *      Author: Triose
 */
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define rep(i,a) for((i)=0; i<(a);(i)++)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define LL __int64
const double PI = acos(-1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
int n, m;
int x, y;
#define N 50000
struct node {
	int max_num;
	int min_num;
};
node dp[N][20];
int a[N];
void Init() {
	int high = (int)log2(n * 1.0) + 1;
	for(int j = 1; j < high; j++) {
		int k = 1 << (j - 1);
		for(int i = 1; i - 1 + 2 * k <= n; i++) {
			dp[i][j].max_num = Max(dp[i][j - 1].max_num,dp[i + k][j - 1].max_num);
			dp[i][j].min_num = Min(dp[i][j - 1].min_num,dp[i + k][j - 1].min_num);
		}
	}
}
int main() {
#ifndef ONLINE_JUDGE
//	freopen("in.txt","r",stdin);
//	freopen("Out.txt", "w", stdout);
#endif
	while(~sfd(n,m)) {
		for(int i = 1; i <= n; i++) {
			sf(a[i]);
			dp[i][0].max_num = a[i];
			dp[i][0].min_num = a[i];
		}
		Init();
		for(int i = 0; i < m; i++) {
			sfd(x,y);
			int k = (int)log2(y - x + 1.0);
			pf(Max(dp[x][k].max_num,dp[y - (1 << k) + 1][k].max_num) - Min(dp[x][k].min_num,dp[y - (1 << k) + 1][k].min_num));
		}
	}
	return 0;
}
这是一道一维的st,因为询问次数很多,所以必须要求O(1)。所以只能用预处理,但是呢,n的范围是  (1 ≤  N  ≤ 50,000) ,如果要dp[N][N],恐怕任何一个编译器也不会让你的代码过。所以只能是dp[N][lg2(N)]的数组来处理。下面讲原理:

RMQ_st_第1张图片

然后我准备做一道二维的rmq问题。。。。不知道再编辑这篇解题报告又是在何年何月了

特别难得,在两小时后我做出了另外一道题目——poj,2019

Cornfields
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 6212   Accepted: 3062

Description

FJ has decided to grow his own corn hybrid in order to help the cows make the best possible milk. To that end, he's looking to build the cornfield on the flattest piece of land he can find. 

FJ has, at great expense, surveyed his square farm of N x N hectares (1 <= N <= 250). Each hectare has an integer elevation (0 <= elevation <= 250) associated with it. 

FJ will present your program with the elevations and a set of K (1 <= K <= 100,000) queries of the form "in this B x B submatrix, what is the maximum and minimum elevation?". The integer B (1 <= B <= N) is the size of one edge of the square cornfield and is a constant for every inquiry. Help FJ find the best place to put his cornfield. 

Input

* Line 1: Three space-separated integers: N, B, and K. 

* Lines 2..N+1: Each line contains N space-separated integers. Line 2 represents row 1; line 3 represents row 2, etc. The first integer on each line represents column 1; the second integer represents column 2; etc. 

* Lines N+2..N+K+1: Each line contains two space-separated integers representing a query. The first integer is the top row of the query; the second integer is the left column of the query. The integers are in the range 1..N-B+1. 

Output

* Lines 1..K: A single integer per line representing the difference between the max and the min in each query. 

Sample Input

5 3 1
5 1 2 6 3
1 3 5 2 7
7 2 4 6 1
9 9 8 6 5
0 6 9 3 9
1 2

Sample Output

5
大意是:n * n的矩阵,让你求b * b的矩阵中最大值和最小值的差,然后会有k次询问,每次输入坐标x,y;

先上代码:

/*
 * train.cpp
 *
 *  Created on: 2016年3月5日
 *      Author: Triose
 */

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<map>
#include<set>
using namespace std;
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
#define rep(i,a) for((i)=0; i<(a);(i)++)
#define mem(a,b) (memset((a),b,sizeof(a)))
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sfs(a) scanf("%s",a)
#define pf(a) printf("%d\n",a)
#define pfs(a) printf("%s\n",a)
#define pfI(a) printf("%I64d\n",a)
#define LL __int64
const double PI = acos(-1.0);
template<class T> T gcd(T a, T b) { return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b)*b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
int n, m, q;
#define N 255
struct node {
	int max_num;
	int min_num;
};
node dp[N][N][10];
int x,y;
void RMQ_Init() {
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			sf(dp[i][j][0].max_num);
			dp[i][j][0].min_num = dp[i][j][0].max_num;
		}
	}
	int maxn = (int)log2(n * 1.0) + 1;
	for(int k = 1; k < maxn; k++) {
		int tmp = 1 << (k - 1);
		for(int i = 1; i + 2 * k - 1 <= n; i++) {
			for(int j = 1; j + 2 * k - 1 <= n; j++) {
				dp[i][j][k].max_num = Max(Max(dp[i][j][k - 1].max_num, dp[i + tmp][j][k - 1].max_num), Max(dp[i][j + tmp][k - 1].max_num,dp[i + tmp][j + tmp][k - 1].max_num));
				dp[i][j][k].min_num = Min(Min(dp[i][j][k - 1].min_num, dp[i + tmp][j][k - 1].min_num), Min(dp[i][j + tmp][k - 1].min_num,dp[i + tmp][j + tmp][k - 1].min_num));
			}
		}
	}
}
void search() {
	int k = (int)log2(m * 1.0);
	int limit_x = x + m;
	int limit_y = y + m;
	int up = Max(Max(dp[x][y][k].max_num,dp[limit_x - (1 << k)][y][k].max_num),Max(dp[x][limit_y - (1 << k)][k].max_num,dp[limit_x - (1 << k)][limit_y - (1 << k)][k].max_num));
	int down = Min(Min(dp[x][y][k].min_num,dp[limit_x - (1 << k)][y][k].min_num),Min(dp[x][limit_y - (1 << k)][k].min_num,dp[limit_x - (1 << k)][limit_y - (1 << k)][k].min_num));
	pf(up - down);
}
int main() {
#ifndef ONLINE_JUDGE
//	freopen("in.txt","r",stdin);
//	freopen("Out.txt", "w", stdout);
#endif
	while(~sft(n,m,q)) {
		RMQ_Init();
		for(int i = 0; i < q; i++) {
			sfd(x,y);
			search();
		}
	}
	return 0;
}

这道题和上面那道题相比其实就是维度的推广而已,基本想法其实一模一样,只是区间长度变成了矩阵的“面积”

RMQ_st_第2张图片

字丑勿喷

你可能感兴趣的:(RMQ_st)