题意:对于一个N行N列的矩阵,有两种操作:
1、C x1 y1 x2 y2,将左上角(x1, y1),右下角(x2, y2)的矩阵反转(0变1,1变0)。
2、Q x y询问A[x, y]的值。
分析:
1、将A[x1][y1]加1(翻转一次),可使左上角(x1, y1),右下角(N,N)的矩阵中每个元素的sum(x, y)都加1,但是显然,只需要将左上角(x1, y1),右下角(x2, y2)的矩阵中每个元素的sum(x, y)都加1即可,所以要使其他无关元素再反转回来,即
(1)将左上角(x1 + 1, y1),右下角(N,N)的矩阵中每个元素的sum(x, y)再加1,这部分元素反转两次等于没反转,同理,
(2)将左上角(x1, y2 + 1),右下角(N,N)的矩阵中每个元素的sum(x, y)再加1
(3)将左上角(x2 + 1, y2 + 1),右下角(N,N)的矩阵中每个元素的sum(x, y)再加1
实质上,只将4个元素加1便达到了目的。
2、询问的时候,直接询问sum(x,y)%2,即左上角(1, 1),右下角(x, y)的矩阵中的加1元素的个数%2。
3、解题过程中充分利用sum函数的性质,即当元素(x, y)加1时,所有左上角(1,1),右下角(xx,yy)的矩阵sum(xx,yy)都加了1,即若要将左上角(x1, y1),右下角(x2, y2)的矩阵反转,无需将矩阵内每个元素都一一反转,而是用左上角(x1, y1)加1后的情况,代替了这一区域矩阵内元素都加1的情况。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) #define lowbit(x) (x & (-x)) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 1000 + 10; const int MAXT = 10000 + 10; using namespace std; int N; int matrix[MAXN][MAXN]; int sum(int x, int y){ int ans = 0; for(int i = x; i >= 1; i -= lowbit(i)){ for(int j = y; j >= 1; j -= lowbit(j)){ ans += matrix[i][j]; } } return ans; } void add(int x, int y, int value){ for(int i = x; i <= N; i += lowbit(i)){ for(int j = y; j <= N; j += lowbit(j)){ matrix[i][j] += value; } } } int main(){ int T; scanf("%d", &T); while(T--){ memset(matrix, 0, sizeof matrix); int k; scanf("%d%d", &N, &k); while(k--){ getchar(); char c; scanf("%c", &c); if(c == 'C'){ int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); add(x1, y1, 1); add(x1, y2 + 1, 1); add(x2 + 1, y1, 1); add(x2 + 1, y2 + 1, 1); } else{ int x, y; scanf("%d%d", &x, &y); printf("%d\n", sum(x, y) % 2); } } if(T) printf("\n"); } return 0; }