Cornfields
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 7058 | Accepted: 3434 |
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.
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.
* 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
Source
题意:给出一个N*N矩形,每个格子上有一个价值。询问一个左上角为(x,y)的b*b的矩形中的最大值最小值和最小值的差是多少。
解题思路:RMQ
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
const int INF=0x3f3f3f3f;
#define LL long long
#define MAXN 260
int a[MAXN][MAXN];
int dp_max[MAXN][MAXN][11],dp_min[MAXN][MAXN][11];
void rmq_init(int n)
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
dp_max[i][j][0]=dp_min[i][j][0]=a[i][j];
for(int i=1; i<=n; i++)
{
for(int k=1; (1<<k)<=n; k++)
{
for(int j=1; j+(1<<k)-1<=n; j++)
{
dp_max[i][j][k]=max(dp_max[i][j][k-1],dp_max[i][j+(1<<(k-1))][k-1]);
dp_min[i][j][k]=min(dp_min[i][j][k-1],dp_min[i][j+(1<<(k-1))][k-1]);
}
}
}
}
void solve(int x,int y,int b)
{
int k=0;
while(1<<(k+1)<=b) k++;
int ma=-1,mi=INF;
int l=y,r=y+b-1;
for(int i=x; i<x+b; i++)
{
ma=max(ma,max(dp_max[i][l][k],dp_max[i][r-(1<<k)+1][k]));
mi=min(mi,min(dp_min[i][l][k],dp_min[i][r-(1<<k)+1][k]));
}
printf("%d\n",ma-mi);
}
int main()
{
int n,b,k;
int x,y;
while(~scanf("%d %d %d",&n,&b,&k))
{
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
scanf("%d",&a[i][j]);
rmq_init(n);
for(int i=0; i<k; i++)
{
scanf("%d%d",&x,&y);
solve(x,y,b);
}
}
return 0;
}