Uva 11297 Census,二维线段树,板子


维护二维矩阵。

q  x1, y1, x2, y2 :   查询x1<=x<=x2, y1<=y<=y2的格子(x,y)的最大值和最小值。

c    x, y, v:    修改(x,y) = v。


矩阵大小(n,m<=500)。

查询次数q<=40000。




#include<algorithm>
using namespace std;

const int INF = 1<<30;
const int maxn = 2000 + 10;

int A[maxn][maxn];

struct IntervalTree2D {
    int Max[maxn][maxn], Min[maxn][maxn], n, m;
    int xo, xleaf, row, x1, y1, x2, y2, x, y, v, vmax, vmin; // 参数、查询结果和中间变量

    void query1D(int o, int L, int R) {
        if(y1 <= L && R <= y2) {
            vmax = max(Max[xo][o], vmax);
            vmin = min(Min[xo][o], vmin);
        } else {
            int M = L + (R-L)/2;
            if(y1 <= M) query1D(o*2, L, M);
            if(M < y2) query1D(o*2+1, M+1, R);
        }
    }

    void query2D(int o, int L, int R) {
        if(x1 <= L && R <= x2) {
            xo = o;
            query1D(1, 1, m);
        } else {
            int M = L + (R-L)/2;
            if(x1 <= M) query2D(o*2, L, M);
            if(M < x2) query2D(o*2+1, M+1, R);
        }
    }

    void modify1D(int o, int L, int R) {
        if(L == R) {
            if(xleaf) {
                Max[xo][o] = Min[xo][o] = v;
                return;
            }
            Max[xo][o] = max(Max[xo*2][o], Max[xo*2+1][o]);
            Min[xo][o] = min(Min[xo*2][o], Min[xo*2+1][o]);
        } else {
            int M = L + (R-L)/2;
            if(y <= M) modify1D(o*2, L, M);
            else modify1D(o*2+1, M+1, R);
            Max[xo][o] = max(Max[xo][o*2], Max[xo][o*2+1]);
            Min[xo][o] = min(Min[xo][o*2], Min[xo][o*2+1]);
        }
    }

    void modify2D(int o, int L, int R) {
        if(L == R) {
            xo = o;
            xleaf = 1;
            modify1D(1, 1, m);
        } else {
            int M = L + (R-L)/2;
            if(x <= M) modify2D(o*2, L, M);
            else modify2D(o*2+1, M+1, R);
            xo = o;
            xleaf = 0;
            modify1D(1, 1, m);
        }
    }

    // 只构建xo为叶子(即x1=x2)的y树
    void build1D(int o, int L, int R) {
        if(L == R) Max[xo][o] = Min[xo][o] = A[row][L];
        else {
            int M = L + (R-L)/2;
            build1D(o*2, L, M);
            build1D(o*2+1, M+1, R);
            Max[xo][o] = max(Max[xo][o*2], Max[xo][o*2+1]);
            Min[xo][o] = min(Min[xo][o*2], Min[xo][o*2+1]);
        }
    }

    void build2D(int o, int L, int R) {
        if(L == R) {
            xo = o;
            row = L;
            build1D(1, 1, m);
        } else {
            int M = L + (R-L)/2;
            build2D(o*2, L, M);
            build2D(o*2+1, M+1, R);
            for(int i = 1; i <= m*4; i++) {
                Max[o][i] = max(Max[o*2][i], Max[o*2+1][i]);
                Min[o][i] = min(Min[o*2][i], Min[o*2+1][i]);
            }
        }
    }

    void query() {
        vmax = -INF;
        vmin = INF;
        query2D(1, 1, n);
    }

    void modify() {
        modify2D(1, 1, n);
    }

    void build() {
        build2D(1, 1, n);
    }
};

IntervalTree2D t;

#include<cstdio>


int main() {
    int n, m, Q, x1, y1, x2, y2, x, y, v;
    char op[10];
    scanf("%d%d", &n, &m);
    t.n = n;
    t.m = m;
    for(int i=1; i<=n; ++i)
        for(int j=1; j<=m; ++j)
            scanf("%d", &A[i][j]);
    t.build();

    scanf("%d", &Q);
    while(Q--) {
        scanf("%s", op);
        if(op[0]=='q') {
            scanf("%d%d%d%d", &t.x1, &t.y1, &t.x2, &t.y2);
            t.query();
            printf("%d %d\n", t.vmax, t.vmin);
        } else {
            scanf("%d%d%d", &t.x, &t.y, &t.v);
            t.modify();
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值