Main topic
Give an initial full 0 matrix, Operation 1 to add the element (I,j) to the K
Action 2 is the output sub-matrix (A, b) for all elements and the C,d
Action 3 to end the program
Two-dimensional tree-like array template
The only thing to note is that the title is numbered from 0, so all coordinates are added 1 and then manipulated.
# include<iostream># include<cstdio># include<cmath># include<algorithm>Const intMN =1035;intN;structbinary_tree{intTR[MN][MN];voidAddintXintYintval) { for(inti=x;i<=n;i+=i&-i) for(intj=y;j<=n;j+=j&-j) Tr[i][j]+=Val;}intQsum (intXinty) { intret=0; for(intI=x;i>0; i-=i&-i) for(intJ=y;j>0; j-=j&-j) Ret+=Tr[i][j]; returnret;}} A;intMain () {intopt,a,b,c,d; scanf ("%d%d",&opt,&N); while(1) {scanf ("%d",&opt); if(opt==3) Break; Else if(opt==1) {scanf ("%d%d%d",&a,&b,&c); A.add (A+1, B +1, c); } Else{scanf ("%d%d%d%d",&a,&b,&c,&d); ++a,++b,++c,++D; printf ("%d\n", A.qsum (c,d) +a.qsum (a1, B-1)-a.qsum (c,b-1)-a.qsum (A-1, D)); } } return 0;}
POJ1195 Two-dimensional tree-like array