poj 2019 Cornfields(二维RMQ)

Cornfields
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5310 Accepted: 2613

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
 
题意:给出一个矩阵,k个询问,每个询问a b,输出以第a行第b列元素为左上角的大小为B*B的子矩阵中最大值与最小值之差。
思路:裸的二维RMQ
 
AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>
#define ll long long
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)

using namespace std;

const int INF = 1e9;
const int maxn = 250;

int a[maxn][maxn];
int pmax[maxn][maxn][15], pmin[maxn][maxn][15];
int n, d, q;
void init_rmq()
{
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            pmax[i][j][0] = pmin[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++)
            {
                pmax[i][j][k] = max(pmax[i][j][k - 1], pmax[i][j + (1 << (k - 1))][k - 1]);
                pmin[i][j][k] = min(pmin[i][j][k - 1], pmin[i][j + (1 << (k - 1))][k - 1]);
            }
}
int RMQ(int a,int b)
{
     int k = (int)(log(double(d)) / log(2.0));
     int maxans = 0, minans = INF;
     int l = b, r = b + d - 1;
     for(int i = a; i < a + d; i++)
     {
         maxans = max(maxans, max(pmax[i][l][k], pmax[i][r - (1 << k) + 1][k]));
         minans = min(minans, min(pmin[i][l][k], pmin[i][r - (1 << k) + 1][k]));
     }
     return maxans - minans;
}

int main()
{
    int x, y;
    while(~scanf("%d%d%d", &n, &d, &q))
    {
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                scanf("%d", &a[i][j]);
        init_rmq();
        while(q--)
        {
            scanf("%d%d", &x, &y);
            printf("%d\n", RMQ(x, y));
        }
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值