poj2155

二维树状数组,对于一个矩阵,迅速修改其一个点的值,迅速求其左上角连续子矩阵的和。对于每个翻转修改矩阵的四个角,对于询问求出该点的左上角矩阵和看奇偶性。

#include <iostream>

#include <cstdlib>

#include <cstring>

#include <cstdio>

using namespace std;



#define maxn 1005



int c[maxn][maxn];

int Row, Col;



inline int Lowbit(const int &x)

{

	return x & (-x);

}



int Sum(int i, int j)

{

	int tempj, sum = 0;

	while (i > 0)

	{

		tempj = j;

		while (tempj > 0)

		{

			sum += c[i][tempj];

			tempj -= Lowbit(tempj);

		}

		i -= Lowbit(i);

	}

	return sum;

}



void Update(int i, int j, int num)

{

	int tempj;

	while (i <= Row)

	{

		tempj = j;

		while (tempj <= Col)

		{

			c[i][tempj] += num;

			tempj += Lowbit(tempj);

		}

		i += Lowbit(i);

	}

}



int main()

{

//	freopen("t.txt", "r", stdin);

	int x, n, t;

	scanf("%d", &x);

	while (x--)

	{

		memset(c, 0, sizeof(c));

		scanf("%d%d", &n, &t);

		getchar();

		Row = n;

		Col = n;

		for (int i = 0; i < t; i++)

		{

			char ch;

			int x1, x2, y2, y1;

			scanf("%c", &ch);

			if (ch == 'C')

			{

				scanf("%d%d%d%d", &x1, &y1, &x2, &y2);

				x2++;

				y2++;

				Update(x1, y1, 1);

				Update(x1, y2, -1);

				Update(x2, y1, -1);

				Update(x2, y2, 1);

			}

			else

			{

				scanf("%d%d", &x1, &y1);

				printf("%d\n", (1 & Sum(x1, y1)));

			}

			getchar();

		}

		printf("\n");

	}

	return 0;

}

你可能感兴趣的:(poj)