Bootstrap

矩阵Matrix(POJ2155)

给一个N*N的矩阵A,其中元素是0或1。A[i][j]表示在第i行第j列的数。最初时,A[i][j]=0(1<=i,j<=N)。我们以以下方式来改变矩阵,给定一个矩形的左上角为(x1,y1)和右下角为(x2,y2),我们对这个矩形范围内的所有元素进行“非”操作(如果它是一个'0',那么变化为'1',否则它变为'0')。

请你编写一个程序完成以下两种操作:

比较简单

用二维树状数组,每次区间增加1,然后单点查询每一个点对2取模的值。

好做完了。

#include <bits/stdc++.h>
using namespace std;
const long long N = 1050;
long long n,m;
class owl{
public:
	long long tr[N][N];
	long long lowbit(long long x){
		return x & -x;
	}
	void add(long long x,long long y,long long d){
		for (long long i = x; i <= n; i += lowbit(i)){
			for (long long j = y; j <= n; j += lowbit(j)){
				tr[i][j] += d;
			}
		}
	}
	long long sum(long long x,long long y){
		long long res = 0;
		for (long long i = x; i ; i -= lowbit(i)){
			for (long long j = y; j; j -= lowbit(j)){
				res += tr[i][j];
			}
		}
		return res;
	}
}A,B,C,D;
void owladd(long long a,long long b,long long c){
	A.add(a,b,c);
	B.add(a,b,c * a);
	C.add(a,b,c * b);
	D.add(a,b,a * b * c);
}
long long owlsum(long long a,long long b){
	return A.sum(a,b) * (a * b + a + b + 1) - B.sum(a,b) * (b + 1) - C.sum(a,b) * (a + 1) + D.sum(a,b);
}
int main(){
	int T;
	cin >> T;
	while (T -- ){
		cin >> n >> m;
		memset(A.tr,0,sizeof A.tr);
				memset(B.tr,0,sizeof B.tr);
				memset(C.tr,0,sizeof C.tr);
				memset(D.tr,0,sizeof D.tr);
		while (m -- ){
			char op;
			cin >> op;
			if (op == 'C'){
				long long a,b,c,d,z = 1;
				cin >> a >> b >> c >> d;
				owladd(a,b,z);
				owladd(a,d + 1,-z);
				owladd(c + 1,b,-z);
				owladd(c + 1,d + 1,z);
			}
			else if (op == 'Q'){
				long long a,b,c,d;
				cin >> a >> b;
				c = a,d = b;
				cout << (owlsum(c,d) - owlsum(a - 1,d) - owlsum(c,b - 1) + owlsum(a - 1,b - 1)) % 2 << endl;
			}
		}
		cout << endl;
	}
	return 0;
}
;