POJ2155 Matrix 二维树状数组应用

        一个N*N(1<=N<=1000)的矩阵,从(1,1)开始,给定一些操作C和一些查询Q,一共K条(1<=K<=50000):

C x1,y1,x2,y2 表示从x1行y1列到x2行y2列的元素全部反转(0变成1,1变成0);

Q x y表示询问x行y列的元素是0还是1。

        题目乍一看感觉还是很难,如果能记录每一个元素的状态值,那答案是显而易见的,但是元素过多,如果每次都对每一个元素进行更新状态的话,复杂度太高。实际上只要记录边界的特定坐标的反转次数,最好的选择那就是二维树状数组了。

        对于操作C,为了保证元素在子矩阵内的反转次数都增加了1,需要在(x1,y1)的位置上加上1,(x2 + 1, y1) 减去1,(x1, y2 + 1)减去1,(x2 +1, y2 + 1)加上1;

        对于操作Q,直接获取Bit(x,y) 的值就代表该元素反转了多少次。

#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <string.h>
#include <string>
#include <iostream>

#define MAXN 1001

template <class TYPE>
void BITAdd(TYPE array[MAXN], int i, TYPE addvalue, int n)
{
	while (i <= n)
	{
		array[i] += addvalue;
		i += i & -i;
	}
}

template <class TYPE>
TYPE BITGet(TYPE array[MAXN], int i)
{
	TYPE ss = 0;
	while (i > 0)
	{
		ss += array[i];
		i -= i & -i;
	}
	return ss;
}

template <class TYPE>
void BIT2DAdd(TYPE array[MAXN][MAXN], int i, int j,TYPE addvalue, int n, int m)
{
	while (i <= n)
	{
		BITAdd<TYPE>(array[i], j, addvalue, m);
		i += i & -i;
	}
}

template <class TYPE>
TYPE BIT2DGet(TYPE array[MAXN][MAXN], int i, int j)
{
	TYPE ss = 0;
	while (i > 0)
	{
		ss += BITGet<TYPE>(array[i], j);
		i -= i & -i;
	}
	return ss;
}

typedef int MYTYPE;

MYTYPE Bit[MAXN][MAXN];

int main()
{
#ifdef _DEBUG
	freopen("d:\\in.txt", "r", stdin);
#endif
	int x;
	scanf("%d", &x);
	for (int i = 0; i < x; i++)
	{
		memset(Bit, 0, sizeof(Bit));
		int n, k;
		scanf("%d %d\n", &n, &k);
		if (i >= 1)
		{
			printf("\n");
		}
		for (int j = 0; j < k; j++)
		{
			char ope;
			scanf("%c", &ope);
			if (ope == 'C')
			{
				int x1, x2, y1, y2;

				scanf("%d %d %d %d\n", &x1, &y1, &x2, &y2);
				BIT2DAdd<int>(Bit, x1, y1, 1, n, n);
				BIT2DAdd<int>(Bit, x2 + 1, y1, -1, n, n);
				BIT2DAdd<int>(Bit, x1, y2 + 1, -1, n, n);
				BIT2DAdd<int>(Bit, x2 + 1, y2 + 1, 1, n, n);
			}
			else
			{
				int x, y;
				scanf("%d %d\n", &x, &y);
				
				int thisvalue = BIT2DGet<int>(Bit, x, y);
				printf("%d\n", thisvalue & 1);
			}
		}
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值