POJ 2019 Cornfields 【二维线段树】

题目链接:POJ 2019 Cornfields

Cornfields
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6269 Accepted: 3089
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

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 250 + 10;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void add(LL &x, LL y) { x += y; x %= MOD; }
struct Nodey {
    int ly, ry, Max, Min;
};
int nx, ny;
int posx[MAXN], posy[MAXN];
struct Nodex {
    int lx, rx;
    Nodey treey[MAXN<<2];
    void Build_y(int o, int l, int r) {
        treey[o].ly = l; treey[o].ry = r;
        treey[o].Max = 0; treey[o].Min = 300;
        if(l == r) {
            posy[l] = o;
            return ;
        }
        int mid = (l + r) >> 1;
        Build_y(ll, l, mid);
        Build_y(rr, mid+1, r);
    }
    int Query_y(int o, int y1, int y2, int op) {
        if(treey[o].ly == y1 && treey[o].ry == y2) {
            return op ? treey[o].Max : treey[o].Min;
        }
        int mid = (treey[o].ly + treey[o].ry) >> 1;
        if(y2 <= mid) return Query_y(ll, y1, y2, op);
        else if(y1 > mid) return Query_y(rr, y1, y2, op);
        else return op ? max(Query_y(ll, y1, mid, op), Query_y(rr, mid+1, y2, op))
                       : min(Query_y(ll, y1, mid, op), Query_y(rr, mid+1, y2, op));
    }
};
Nodex treex[MAXN<<2];
void Build_x(int o, int l, int r) {
    treex[o].lx = l; treex[o].rx = r;
    treex[o].Build_y(1, 1, ny);
    if(l == r) {
        posx[l] = o;
        return ;
    }
    int mid = (l + r) >> 1;
    Build_x(ll, l, mid);
    Build_x(rr, mid+1, r);
}
int Query_x(int o, int x1, int x2, int y1, int y2, int op) {
    if(treex[o].lx == x1 && treex[o].rx == x2) {
        return treex[o].Query_y(1, y1, y2, op);
    }
    int mid = (treex[o].lx + treex[o].rx) >> 1;
    if(x2 <= mid) return Query_x(ll, x1, x2, y1, y2, op);
    else if(x1 > mid) return Query_x(rr, x1, x2, y1, y2, op);
    else return op ? max(Query_x(ll, x1, mid, y1, y2, op), Query_x(rr, mid+1, x2, y1, y2, op))
                   : min(Query_x(ll, x1, mid, y1, y2, op), Query_x(rr, mid+1, x2, y1, y2, op));
}
void PushUp(int x, int y, int v) {
    treex[x].treey[y].Max = max(treex[x].treey[y].Max, v);
    treex[x].treey[y].Min = min(treex[x].treey[y].Min, v);
}
void Update(int x, int y, int v) {
    for(int i = posx[x]; i ; i >>= 1) {
        for(int j = posy[y]; j ; j >>= 1) {
            PushUp(i, j, v);
        }
    }
}
int main()
{
    int b, m;
    scanf("%d%d%d", &nx, &b, &m); ny = nx;
    Build_x(1, 1, nx);
    for(int i = 1; i <= nx; i++) {
        for(int j = 1; j <= ny; j++) {
            int v; scanf("%d", &v);
            Update(i, j, v);
        }
    }
    while(m--) {
        int x, y; scanf("%d%d", &x, &y);
        printf("%d\n", Query_x(1, x, x + b - 1, y, y + b - 1, 1) - Query_x(1, x, x + b - 1, y, y + b - 1, 0));
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值