Test Instructions: given a n*n matrix, the matrix may contain only 0 or 1, and at first it is all 0. For the matrix can do two operations, the first is the input C x1 y1 x2 y2, to (x1, y1) for the upper left corner, with (x2, y2) for the lower right corner of the rectangular region composed of the number of all the inverse operation, that is, 0 change 1, 1 to 0. The second is Q x Y, which indicates whether the query is now the (X, Y) point of the matrix is 0 or 1. There are a total of T operations, for the C operation of the corresponding modification, for the Q operation to correspond to the output!
Analysis: It is said that the title of the building, I do not think of any efficient way to refer to the solution on the Internet, only to learn that the idea of solving problems originated from a national team paper, said that the simple point is the C operation of the topic requirements, in fact, as long as the update of the small rectangular area of the four points can be, that is not When the Q operation query is also just a two-dimensional tree array to the (X, Y) point of the rectangle to sum, and then the sum of the results of%2, if 1 is the point has been an odd number of operations, so should be 1, 0 is the reverse. As for why or how to do, the paper is more clear than speaking, please Baidu: "on the Informatics competition" 0 "and" 1 "
Blind thinking: It is worth learning whether the judgment of 0 or 1 is converted to a parity that determines the number of times the point is manipulated. There are changes to the interval, for one-dimensional only two points of the starting tag, the two-dimensional four points to start the mark, this is also a bad!
#include <stdio.h>#include<string.h>#defineLowbit (i) (i& (-i))Const intMAXN =1001;intC[MAXN][MAXN], N;inlinevoidAddintRowintCol) { for(intI=row; i<=n; i+=lowbit (i)) { for(intJ=col; j<=n; j+=Lowbit (j)) {C[i][j]++; } }}intSumintRowintCol) { intAns =0; for(intI=row; I>0; i-=lowbit (i)) { for(intJ=col; J>0; j-=Lowbit (j)) {ans+=C[i][j]; } } returnans;} InlinevoidInitializeintk) { for(intI=0; i<=k; i++) {memset (c[i],0,sizeof(C[i])); }}intMainvoid){ intncase; scanf ("%d", &ncase); while(ncase--) {scanf ("%d", &N); Initialize (n); intnum; scanf ("%d", &num); while(num--){ Charcommand; scanf ("%c", &command); if(command=='C'){ intx1, y1, x2, y2; scanf ("%d%d%d%d", &x1, &y1, &X2, &y2); Add (x1, y1); Add (x2+1, y1); Add (x1, y2+1); Add (x2+1, y2+1); }Else{ intx, y; scanf ("%d%d", &x, &y); intSUM =sum (x, y); printf ("%d\n", sum&1);//&1 and%2 are as efficient as they are! }} puts ("");//Note that there is a blank line between the use cases to avoid PE } return 0;}
View Code
POJ 2155 Matrix (tree array && interval count)