bzoj 1452: [JSOI2009]Count (二维树状数组)

链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1452

思路: 对每个颜色开一个二维树状数组维护就好了

实现代码:

#include<bits/stdc++.h>
using namespace std;
int bit[110][310][310];
int a[310][310],n,m,q;
int lowbit(int x){
    return x&-x;
}

void add(int x,int y,int z,int rt){
    for(int i = x;i <= n;i += lowbit(i)){
        for(int j = y;j <= m;j += lowbit(j)){
            bit[rt][i][j] += z;
        }
    }
}

int getsum(int x,int y,int rt){
    int res = 0;
    for(int i = x;i;i -= lowbit(i)){
        for(int j = y;j;j -= lowbit(j)){
            res += bit[rt][i][j];
        }
    }
    return res;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;i ++){
        for(int j = 1;j <= m;j ++){
            scanf("%d",&a[i][j]);
            add(i,j,1,a[i][j]);
        }
    }
    int op,x,y,z,x1,x2,y1,y2;
    scanf("%d",&q);
    while(q--){
        scanf("%d",&op);
        if(op == 1){
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,-1,a[x][y]);
            add(x,y,1,z);
            a[x][y] = z;
        }
        else{
            scanf("%d%d%d%d%d",&x1,&x2,&y1,&y2,&z);
            int num = getsum(x2,y2,z) - getsum(x1-1,y2,z) - getsum(x2,y1-1,z) + getsum(x1-1,y1-1,z);
            printf("%d\n",num);
        }
    }
    return 0;
}

 

posted @ 2019-03-27 11:23  冥想选手  阅读(143)  评论(0编辑  收藏  举报