2、分析:二维树状数组裸题
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; int C[110][310][310]; int a[310][310]; int n, m; inline void add(int c, int x, int y, int z){ for(; x <= n; x += (x & -x)){ int yy = y; for(; yy <= m; yy += (yy & -yy)){ C[c][x][yy] += z; } } return; } inline int query(int c, int x, int y){ int res = 0; for(; x > 0; x -= (x & -x)){ int yy = y; for(; yy > 0; yy -= (yy & -yy)){ res += C[c][x][yy]; } } return res; } int main(){ scanf("%d%d", &n, &m); for(int i = 1; i <= n; i ++){ for(int j = 1; j <= m; j ++){ int c; scanf("%d", &c); add(c, i, j, 1); a[i][j] = c; } } int Q; scanf("%d", &Q); while(Q --){ int op, x1, y1, x2, y2, c; scanf("%d", &op); if(op == 1){ scanf("%d%d%d", &x1, &y1, &c); add(a[x1][y1], x1, y1, -1); add(c, x1, y1, 1); a[x1][y1] = c; } else{ scanf("%d%d%d%d%d", &x1, &x2, &y1, &y2, &c); printf("%d\n", query(c, x2, y2) - query(c, x1 - 1, y2) - query(c, x2, y1 - 1) + query(c, x1 - 1, y1 - 1)); } } return 0; }