现在的位置: 首页 > 综合 > 正文

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

2018年04月24日 ⁄ 综合 ⁄ 共 866字 ⁄ 字号 评论关闭
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000001;

inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-')f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x*f;
}

int n, m, q,mp[305][305], t[105][305][305];

inline void update(int x,int y,int c,int val){
    for(int i=x;i<=n;i+=i&(-i))
        for(int j=y;j<=m;j+=j&(-j))
            t[c][i][j]+=val;
}
inline int ask(int x,int y,int c){
    int res=0;
    for(int i=x;i;i-=i&(-i))
        for(int j=y;j;j-=j&(-j))
            res+=t[c][i][j];
    return res;
}
int main() {
    n = read();
    m = read();
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            mp[i][j] = read();
            update(i, j, mp[i][j], 1);
        }
    q = read();
    while (q--) {
        int opr = read();
        if (opr == 1) {
            int x = read(), y = read(), c = read();
            update(x, y, mp[x][y], -1);
            mp[x][y] = c;
            update(x, y, mp[x][y], 1);
        } else {
            int x1 = read(), x2 = read(), y1 = read(), y2 = read(), c = read();
            printf("%d\n", ask(x2, y2, c) + ask(x1 - 1, y1 - 1, c) - ask(x1 - 1, y2, c) - ask(x2, y1 - 1, c));
        }
    }
    return 0;
}

抱歉!评论已关闭.