Description
Input
Output
Sample Input
1 2 10 C 2 1 2 2 Q 2 2 C 2 1 2 1 Q 1 1 C 1 1 2 1 C 1 2 1 2 C 1 1 2 2 Q 1 1 C 1 1 2 1 Q 2 1
Sample Output
1 0 0 1
思路:二维树状数组快速求数字子矩阵的和.
代码如下:
#include<stdio.h> #include<string.h> int c[1005][1005], n; int Lowbit(int t) { return t&(-t); } void add(int x, int y, int val) { int i=y; while(x<=n) { y=i; while(y<=n) { c[x][y]+=val; y+=Lowbit(y); } x+=Lowbit(x); } } int Sum(int x, int y) { int i=y, sum=0; while(x>0) { y=i; while(y>0) { sum+=c[x][y]; y-=Lowbit(y); } x-=Lowbit(x); } return sum; } int main() { int T, m, x1, y1, x2, y2, i; char ch; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); memset(c, 0, sizeof(c)); getchar(); for(i=0; i<m; i++) { scanf("%c", &ch); if(ch=='C') { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); x2++, y2++; add(x1, y1, 1); add(x2, y2, 1); add(x1, y2, -1); add(x2, y1, -1); } else { scanf("%d%d", &x1, &y1); printf("%d\n", Sum(x1, y1)%2); } getchar(); } printf("\n"); } }