[JSOI2009]计数问题
数据范围不大,考虑开100个二维树状数组进行维护
#include <iostream>
using namespace std;
#define MAXN 305
int col[MAXN][MAXN];
int N,M;
struct Binary {
#define lowbit(x) (x&(-x))
int pre[MAXN][MAXN];
inline int query(int x,int y) {
int ans = 0;
for(;x;x-=lowbit(x))
for(int i=y;i;i-=lowbit(i)) ans += pre[x][i]; // 注意一下
return ans;
}
inline void update(int x,int y,int c) {
for(;x<=N;x+=lowbit(x))
for(int i=y;i<=M;i+=lowbit(i)) pre[x][i] += c;
}
} tr[MAXN];
int main() {
cin >> N >> M;
for(int i=1;i<=N;++i)
for(int j=1;j<=M;++j) {
cin >> col[i][j];
tr[col[i][j]].update(i,j,1);
}
int Q; cin >> Q;
while(Q--) {
int opt,x1,y1,x2,y2,c;
cin >> opt;
if(opt==1) {
cin >> x1 >> y1 >> c;
tr[col[x1][y1]].update(x1,y1,-1);
tr[col[x1][y1] = c].update(x1,y1,1);
}
else {
cin >> x1 >> x2 >> y1 >> y2 >> c;
cout << tr[c].query(x2,y2) - tr[c].query(x2,y1-1) -tr[c].query(x1-1,y2) + tr[c].query(x1-1,y1-1) << '\n';
}
}
return 0;
}