PS:c<=100 (原谅像我一样眼瞎的人吧
这题就是一个二维树状数组的模板啊,然后不会,然后学了学,然后好像没难度,自己看代码就会了。。
#include<cstdio> #include<iostream> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> using namespace std; int n,m,a[310][310],s[310][310][110]; int lowbit(int x){return x&-x;} void add(int x,int y,int c,int k) { int yy=y; while(x<=n) { y=yy; while(y<=n) { s[x][y][c]+=k; y+=lowbit(y); } x+=lowbit(x); } } int getsum(int x,int y,int c) { int ret=0,yy=y; while(x>0) { y=yy; while(y>0) { ret+=s[x][y][c]; y-=lowbit(y); } x-=lowbit(x); } 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",&a[i][j]); add(i,j,a[i][j],1); } int Q,op,x1,y1,x2,y2,c; scanf("%d",&Q); while(Q--) { scanf("%d",&op); if(op==1) { scanf("%d%d%d",&x1,&y1,&c); add(x1,y1,a[x1][y1],-1); a[x1][y1]=c; add(x1,y1,a[x1][y1],1); } else { scanf("%d%d%d%d%d",&x1,&x2,&y1,&y2,&c); printf("%d\n",getsum(x2,y2,c)-getsum(x1-1,y2,c)-getsum(x2,y1-1,c)+getsum(x1-1,y1-1,c)); } } return 0; }