Description
Input
Output
Sample Input
Sample Output
1
2
2
HINT
话说这好像是我第一次做树状数组的题
树状数组裸题,还是二维的,多一层for而已
代码如下:
#include<cmath> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; int a[310][310][110]; int c[310][310]; int n,m; int lowbit(int x){return x&-x;} void change(int x,int y,int cc,int d) { for(int i=x;i<=n;i+=lowbit(i)) for(int j=y;j<=m;j+=lowbit(j)) a[i][j][cc]+=d; } /* 3 3 1 2 3 3 2 2 2 1 3 */ int findsum(int x,int y,int cc) { int sum=0; for(int i=x;i>=1;i-=lowbit(i)) for(int j=y;j>=1;j-=lowbit(j)) sum+=a[i][j][cc]; return sum; } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf("%d",&c[i][j]); change(i,j,c[i][j],1); } int Q; scanf("%d",&Q); while(Q--) { int k,x,y,cc,tx,ty; scanf("%d",&k); if(k==1) { scanf("%d%d%d",&x,&y,&cc); change(x,y,c[x][y],-1); c[x][y]=cc; change(x,y,c[x][y],1); } else { scanf("%d%d%d%d%d",&x,&tx,&y,&ty,&cc); int ans; ans=findsum(tx,ty,cc)+findsum(x-1,y-1,cc); ans=ans-(findsum(x-1,ty,cc)+findsum(tx,y-1,cc)); printf("%d\n",ans); } } return 0; }
by_lmy