1. 程式人生 > >poj 2019 Cornfields(二維RMQ)

poj 2019 Cornfields(二維RMQ)

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

Source

二維RMQ模板,查詢左上右下,不過這個模板敲起來真的很麻煩

#include<iostream>
#include<Cstdio>
#include<cstring>
#include<cstring>
using namespace std;
#define maxn 255
int val[maxn][maxn];
int dmin[maxn][maxn][8][8];
int dmax[maxn][maxn][8][8];
void initrmq(int n,int m)
{
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++)
    dmin[i][j][0][0]=dmax[i][j][0][0]=val[i][j];
    for(int ii=0;(1<<ii)<=n;ii++)
    for(int jj=0;(1<<jj)<=m;jj++)
    if(ii+jj)
    for(int i=1;i+(1<<ii)-1<=n;i++)
    for(int j=1;j+(1<<jj)-1<=m;j++)
    if(ii)
    {
        dmin[i][j][ii][jj]=min(dmin[i][j][ii-1][jj],dmin[i+(1<<(ii-1))][j][ii-1][jj]);
        dmax[i][j][ii][jj]=max(dmax[i][j][ii-1][jj],dmax[i+(1<<(ii-1))][j][ii-1][jj]);

    }
    else
    {
        dmin[i][j][ii][jj]=min(dmin[i][j][ii][jj-1],dmin[i][j+(1<<(jj-1))][ii][jj-1]);
        dmax[i][j][ii][jj]=max(dmax[i][j][ii][jj-1],dmax[i][j+(1<<(jj-1))][ii][jj-1]);


    }
}
    int getmax(int x1,int y1,int x2,int y2)
    {
        int k1=0;
        while((1<<(k1+1))<=x2-x1+1)
        k1++;
        int k2=0;
        while((1<<(k2+1))<=y2-y1+1)
        k2++;
        x2=x2-(1<<k1)+1;
        y2=y2-(1<<k2)+1;
        return max(max(dmax[x1][y1][k1][k2],dmax[x1][y2][k1][k2]),max(dmax[x2][y1][k1][k2],dmax[x2][y2][k1][k2]));

    }
    int getmin(int x1,int y1,int x2,int y2)
    {
        int k1=0;
        while((1<<(k1+1))<=x2-x1+1)
        k1++;
        int k2=0;
        while((1<<(k2+1))<=y2-y1+1)
        k2++;
        x2=x2-(1<<k1)+1;
        y2=y2-(1<<k2)+1;
        return min(min(dmin[x1][y1][k1][k2],dmin[x1][y2][k1][k2]),min(dmin[x2][y1][k1][k2],dmin[x2][y2][k1][k2]));

    }
    int main()
    {
        int n,b,k;
        while(~scanf("%d%d%d",&n,&b,&k)&&n&&b&&k)
        {for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        scanf("%d",&val[i][j]);
        initrmq(n,n);
            while(k--)
            {
                int x1,y1;
                scanf("%d%d",&x1,&y1);
                int ans=getmax(x1,y1,x1+b-1,y1+b-1)-getmin(x1,y1,x1+b-1,y1+b-1);
                printf("%d\n",ans);
            }
        }
    return 0;
    }