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
思路很简单,就直接二维RMQ,dp[k][j][i]第k行的rmq区间维护。
我竟然wa了8次。。。
死都没看到求mi时dpi【】【】【】少了个i。。。。。。。
好不容易碰到自己想出来的题。。wa在这种弱智的地方。。醉了。。。。
加油。。
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
#define inf 0x3f3f3f3f
int dp[255][255][32];
int dpi[255][255][32];
int a[255][255];
void RMQ(int n)
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
dpi[k][i][0]=dp[k][i][0]=a[k][i];
for(int k=1;k<=n;k++)
for(int j=1;(1<<j)<=n;j++)
for(int i=1;i+(1<<j)-1<=n;i++)
{
dp[k][i][j]=max(dp[k][i][j-1],dp[k][i+(1<<j-1)][j-1]);
dpi[k][i][j]=min(dpi[k][i][j-1],dpi[k][i+(1<<j-1)][j-1]);
}
}
int main()
{
int n,b,k,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]);
int len=0;
while(1<<len+1<=b)
len++;
RMQ(n);
while(k--)
{
scanf("%d%d",&x,&y);
int mi,ma;
mi=inf;
ma=-inf;
// cout<<ma<<"++"<<mi<<"--"<<endl;
for(int i=x;i<x+b/*&&i<=n*/;i++)
{
// if(f>n)f=n;
ma=max(max(dp[i][y][len],dp[i][(y+b-1)-(1<<len)+1][len]),ma);
mi=min(min(dpi[i][y][len],dpi[i][(y+b-1)-(1<<len)+1][len]),mi);
}
// cout<<ma<<"++"<<mi<<endl;*/
cout<<ma-mi<<endl;
}
}
return 0;
}
人一我百,人百我万.夕林山寸,寻梦指尖!