An n* m square, each square initially has an integer weight. Next, there are 2 operations each time: Change the weight of a grid; Find the number of occurrences of a particular weight in a submatrix. Input and output format Input format: The first line has two numbers N, M. Next N lines, each line has M numbers, and the jth number in the i + 1 line represents the initial weight of the lattice (i, j). Next enter an integer Q. After Q lines, each line describes an operation. Action 1: " 1 xyc" (without double quotes). Indicates that the weight of the lattice (x,y) is changed to c ( 1 <=x<=n, 1 <=y<=m, 1 <=c<= 100 ). Action 2: " 2 x1 x2 y1 y2 c" (without double quotes, x1<=x2,y1<=y2). It means to ask the number of all the grids (x, y) that satisfy the grid color c, and x1<=x<=x2, y1<=y<= y2. Output format: For each operation 2, according to the order in which it appears in the input, one line of integers is output in turn to represent the obtained number.
Two-dimensional point modification, interval query, so of course the tree array does not have to run. Two-dimensional and one-dimensional writing are the same, but when passing parameters, pass a few more, and when looping, it is only a two-layer loop. Then, of course, the weight tree array opens the bucket to record the number. One thing to note is that because the query is the number of a certain number in a certain sub-matrix, it is a little different from the one-dimensional one. Here, you need to add a large block like calculating the two-dimensional prefix sum. , then subtract two small pieces, and add the small piece that was repeatedly subtracted. I would say that I started wrong here?
Code:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int n,m,t,q,x,y,xx,yy,w,c[105][305][305],a[305][305]; 6 void update(int d,int x,int y,int w) 7 { for(int i=x;i<=n;i+=i&-i) 8 for(int j=y;j<=m;j+=j&-j) 9 c[d][i][j]+=w; 10 } 11 int query(int d,int x,int y) 12 { int ans=0; 13 for(int i=x;i>0;i-=i&-i) 14 for(int j=y;j>0;j-=j&-j) 15 ans+=c[d][i][j]; 16 return ans; 17 } 18 int main() 19 { freopen("count.in","r",stdin); 20 freopen("count.out","w",stdout); 21 scanf("%d%d",&n,&m); 22 memset(c,0,sizeof c); 23 for(int i=1;i<=n;i++)for(int j=1;j<=m;j++) 24 { scanf("%d",&a[i][j]); 25 update(a[i][j],i,j,1); 26 } 27 scanf("%d",&q); 28 for(int i=1;i<=q;i++) 29 { scanf("%d",&t); 30 if(t==1) 31 { scanf("%d%d%d",&x,&y,&w); 32 update(a[x][y],x,y,-1); 33 update(w,x,y,1); 34 a[x][y]=w; 35 }else 36 { scanf("%d%d%d%d%d",&x,&xx,&y,&yy,&w); 37 int ans=query(w,xx,yy)-query(w,x-1,yy)-query(w,xx,y-1)+query(w,x-1,y-1); 38 printf("%d\n",ans); 39 } 40 } 41 return 0; 42 }