bzoj 1452 二维树状数组

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int>
 
using namespace std;
 
const int N = 300 + 7;
const int M = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
 
int n, m, a[N][N];
struct BIT {
    int a[301][301];
    void modify(int x, int y, int v) {
        for(int i = x; i <= n; i += i & -i)
            for(int j = y; j <= m; j += j & -j)
                a[i][j] += v;
    }
 
    int sum(int x, int y) {
        int ans = 0;
        for(int i = x; i; i -= i & -i)
            for(int j = y; j; j -= j & -j)
                ans += a[i][j];
        return ans;
    }
}bit[101];
 
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]);
            bit[a[i][j]].modify(i, j, 1);
        }
    }
 
    int q; scanf("%d", &q);
    while(q--) {
        int op; scanf("%d", &op);
        if(op == 1) {
            int x, y, c;
            scanf("%d%d%d", &x, &y, &c);
            bit[a[x][y]].modify(x, y, -1);
            bit[c].modify(x, y, 1);
            a[x][y] = c;
        } else {
            int x1, x2, y1, y2, c, ans = 0;
            scanf("%d%d%d%d%d", &x1, &x2, &y1, &y2, &c);
            ans += bit[c].sum(x2, y2);
            ans += bit[c].sum(x1 - 1, y1 - 1);
            ans -= bit[c].sum(x2, y1 - 1);
            ans -= bit[c].sum(x1 - 1, y2);
            printf("%d\n", ans);
        }
    }
    return 0;
}
/*
*/

 

posted @ 2018-07-07 20:22  NotNight  阅读(115)  评论(0编辑  收藏  举报