二维RMQ
两种做法:
1) O(n*m*log(m))-O(n)
2) O(n*m*log(n)*log(m)-O(1)
把每一行都当成一维RMQ处理
代码:
#include
#include
#include
#include
#include
#include
#include
首先,dp[ i ][ j ][ k ][ l ]表示从第一个点也就是左下角(i,j)起,横坐标长度为2^k, 纵坐标长度为2^l,即右上角(i+2^k-1,j+2^k-1)中的最大值。
一维RMQ是把一个区间划分成两个区间求最值,同理,在二维RMQ中,我们可以把一个大的矩阵划分成四个小矩阵求最值
但是要考虑到一种特殊情况:这个矩阵行为1或列为1时,需要特判断。
一共四种情况:
一个点:k=0,l=0(从点(i, j)横纵坐标长度都为1,所包含的点的最大值,即只有它本身) maxx[ i ][ j ][ k ][ l ] = mapp[ i ][ j ];
一列: k=0,l!=0(从点(i, j)横坐标长度为1,送坐标长度>1所包含的点的最大值,即矩阵为一列)maxx[ i ][ j ][ k ][ l ] = max(maxx[ i ][ j ][ k ][ l-1 ],maxx[ i ][ j+(1<<(l-1)) ][ k ][ l-1 ]);
一行: k!=0,l=0(从点(i, j)横坐标长度>1,纵坐标长度为1所包含的点的最大值,即矩阵为一行)maxx[ i ][ j ][ k ][ l ] = max(maxx[ i ][ j ][ k-1 ][ l ],maxx[ i+(1<<(k-1)) ][ j ][ k-1 ][ l ]);
其他: maxx[ i ][ j ][ k ][ l ] = maxm(maxx[ i ][ j ][ k-1 ][ l-1 ],maxx[ i+(1<<(k-1)) ][ j ][ k-1 ][ l-1 ], maxx[ i ][ j+(1<<(l-1)) ][ k-1 ][ l-1 ], maxx[ i+(1<<(k-1)) ][ j+(1<<(l-1)) ][ k-1 ][ l-1 ]);
同一维一样, 用maxx[i][j][k][l]表示左下角(i, j)到右上角(i+2^k, j+2^l)矩形内的最值,初始化maxx的时候,maxx[i][j][0][0] = mapp[i][j],接着用4层for去循环更新
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
scanf("%d", &mapp[i][j]);
maxx[i][j][0][0] = mapp[i][j];
}
}
for (int k=0; (1<
例题:
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,以及K次询问,然后给出一个点(Xi,Yi),输出,在以给出的点为左上角的点,边长为B的子矩阵中,最大值与最小值的差;
#include
#include
#include
#include
#include
#include
#include
/*
#include
#include
#include
#include
#include
#include
#include
#include