Link:
BZOJ 1452 传送门
Solution:
二维树状数组模板题
发现颜色数很少$c<=100$,因此对于每个颜色都建一棵二维线段树即可
(第一次写二维数据结构,发现套个循环就行了?)
Code:
#include <bits/stdc++.h>using namespace std; const int MAXN=305,MAXC=105; int bit[MAXN][MAXN][MAXC],col[MAXN][MAXN],n,m,q;int lowbit(int x){return x&(-x);} void Update(int x,int y,int c,int val) {for(int i=x;i<=n;i+=lowbit(i))for(int j=y;j<=m;j+=lowbit(j))bit[i][j][c]+=val; } int Query(int x,int y,int c) {int ret=0;for(int i=x;i;i-=lowbit(i))for(int j=y;j;j-=lowbit(j))ret+=bit[i][j][c];return ret; }int main() {scanf("%d%d",&n,&m);for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)scanf("%d",&col[i][j]),Update(i,j,col[i][j],1);scanf("%d",&q);while(q--){int op,x1,x2,y1,y2,c;scanf("%d",&op);if(op==1){scanf("%d%d%d",&x1,&y1,&c);Update(x1,y1,col[x1][y1],-1);col[x1][y1]=c;Update(x1,y1,c,1);}else{scanf("%d%d%d%d%d",&x1,&x2,&y1,&y2,&c);int res=Query(x2,y2,c)+Query(x1-1,y1-1,c);res-=(Query(x2,y1-1,c)+Query(x1-1,y2,c));printf("%d\n",res);}}return 0; }