POJ - 2019 Cornfields 【二维线段树】

题目链接:https://vjudge.net/problem/POJ-2019

我写的这个二维线段树没什么技巧,就是直接开一个 node[cur][cure],cur代表的是x轴的一维线段树,cure代表的是y轴的一维线段树。

初始化:

先从x轴开始做线段树的操作,当x轴到达叶节点时,再对y轴做线段树的操作,x轴返回到父节点时,一样要对y轴从1到N重头做一次线段树的操作,这是初始化的过程,总的时间是 log(n)*log(n)。也就是说,对每一个x轴节点都要从 1到N做一次y轴的线段树操作。

初始化的这部分我一开始贪简便,把x轴和y轴的线段树操作都写在一个自定义函数里面,导致程序在递归的过程中走了很多重复的步骤。所以对于x轴的线段树操作和y轴的线段树操作要尽量分开写。

查找:

查找的大致流程和初始化是差不多的,先把x轴的取值范围确定下来,然后再去确定y轴的取值范围。

#include <iostream>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>

using namespace std;

#define lson (cur<<1)
#define rson (cur<<1|1)
typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;
const long long LINF = 1e18;
const int Maxn = 250+10;
const int Mod = 1e9+7;

struct Node {
    int maxn, minx;
} node[Maxn<<2][Maxn<<2];

int a[Maxn][Maxn], N;

void pushup(int cur, int cure) {
    node[cur][cure].maxn = max(node[cur][cure<<1].maxn, node[cur][cure<<1|1].maxn);
    node[cur][cure].minx = min(node[cur][cure<<1].minx, node[cur][cure<<1|1].minx);
}

void updata_(int cur, int cure, int l, int r, int L, int R) {
	 if(l == r && L == R) {
        node[cur][cure].maxn = node[cur][cure].minx = a[l][L];
        return;
    }
    if(L == R) {
        node[cur][cure].maxn = max(node[cur<<1][cure].maxn, node[cur<<1|1][cure].maxn);
        node[cur][cure].minx = min(node[cur<<1][cure].minx, node[cur<<1|1][cure].minx);
        return;
    }
    int Mid = (L+R)>>1;
    updata_(cur, cure<<1, l, r, L, Mid);
    updata_(cur, cure<<1|1, l, r, Mid+1, R);
	pushup(cur, cure);
}

void updata(int cur, int cure, int l, int r, int L, int R) {
    int mid = (l+r)>>1;
    if(l != r) {
        updata(cur<<1, cure, l, mid, L, R);
        updata(cur<<1|1, cure, mid+1, r, L, R);
    }
    updata_(cur, cure, l, r, L, R);
}

int query(int cur, int cure, int l, int r, int L, int R, int x, int y, int B) {
    int xx = x+B-1, yy = y+B-1;
    if(x <= l && r <= xx && y <= L && R <= yy) return node[cur][cure].maxn;

    int mid = (l+r)>>1, Mid = (L+R)>>1, maxn = -INF;
    if(x <= l && r <= xx) {
        if(y <= Mid) maxn = max(maxn, query(cur, cure<<1, l, r, L, Mid, x, y, B));
        if(Mid+1 <= yy) maxn = max(maxn, query(cur, cure<<1|1, l, r, Mid+1, R, x, y, B));
    } else {
        if(x <= mid) maxn = max(maxn, query(cur<<1, cure, l, mid, L, R, x, y, B));
        if(mid+1 <= xx) maxn = max(maxn, query(cur<<1|1, cure, mid+1, r, L, R, x, y, B));
    }
    return maxn;
}

int Query(int cur, int cure, int l, int r, int L, int R, int x, int y, int B) {
    int xx = x+B-1, yy = y+B-1;
    if(x <= l && r <= xx && y <= L && R <= yy) return node[cur][cure].minx;

    int mid = (l+r)>>1, Mid = (L+R)>>1, minx = INF;

    if(x <= l && r <= xx) {
        if(y <= Mid) minx = min(minx, Query(cur, cure<<1, l, r, L, Mid, x, y, B));
        if(Mid+1 <= yy) minx = min(minx, Query(cur, cure<<1|1, l, r, Mid+1, R, x, y, B));
    } else {
        if(x <= mid) minx = min(minx, Query(cur<<1, cure, l, mid, L, R, x, y, B));
        if(mid+1 <= xx) minx = min(minx, Query(cur<<1|1, cure, mid+1, r, L, R, x, y, B));
    }
    return minx;
}

int main(void)
{
    int B, K;
    scanf("%d%d%d", &N, &B, &K);
    for(int i = 0; i < N; ++i)
        for(int j = 0; j < N; ++j) scanf("%d", &a[i][j]);

    updata(1, 1, 0, N-1, 0, N-1);
    int x, y;
    while (K--) {
        scanf("%d%d", &x, &y);
        x--; y--;
        int maxn, minx;
        maxn = query(1, 1, 0, N-1, 0, N-1, x, y, B);
        minx = Query(1, 1, 0, N-1, 0, N-1, x, y, B);
        printf("%d\n", maxn-minx);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值