【HDU5892 2016 ACM ICPC Asia Regional Shenyang Online A】【二维树状数组模板 区间修改】nn矩阵内子矩阵中各怪兽数量的奇偶性.cpp

Resident Evil

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 420    Accepted Submission(s): 90


Problem Description
The Umbrella Company has developed a new biochemical virus secretly. Lyon, a staff of the company, happened to find out the conspiracy and his company is now stopping him from discovering further evidence by releasing biochemical monster in his city (don’t ask me why the company is using this weird method).

The city can be described as an n*n grids. The Umbrella Company has 50 kinds of biochemical monster in total. The company will specify the type, quantity of biochemical monster and the rectangle area to put monster. For example, if the specified rectangle area has upper left corner (1, 1) and bottom right corner of (3, 3), and the company wishes to put 3 of A monster, 2 of B monster and 1 of C monster in it, then 3 of A monster, 2 of B monster and 1 of C monster is added to each and every grid inside the area.

However, Lyon risk his life of finding evidence by searching certain rectangle area. By doing this, all monsters inside the area would gather to him. He has two way of dealing with monster. If the number of a certain kind of monster is even, then he would choose to hide, otherwise withdraw.
 

Input
A line containing n and m (1<=n<=3000 1<=m<=100000). Representing the size of the city and the number of operations.

  Then m lines of operation and there are only two kinds of operation.

Letter ‘P’ means to release monster, followed by 4 integers x1, y1, x2, y2 (1<=x1, y1, x2, y2<=n) , describing the upper left corner and bottom right corner of the area. Then an integer K(1<=K<=50), meaning there will be k pair of number (A, B) given next. A (1<=A<=50) indicates the kind of the monster and B (1<=b<=100000) indicates the number of this kind being added to this area.

Letter ‘Q’ represents the query operation, followed by 4 integers x1, y1, x2, y2 (1<=x1, y1, x2, y2<=n), describing the upper left corner and bottom right corner of the area.
 

Output
For every ‘Q’ operation. Print 50 number in a line, meaning the kind of action he would take for different kinds of monsters. 1 represents hiding and 2 represents withdrawing.
 

Sample Input
  
  
2 2 P 1 1 2 2 1 1 1 Q 1 1 1 1
 

Sample Output
  
  
2 1 1 1 1 1 ........1 (one '2' and forty-nine '1')
 

Source

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define ls o<<1
#define rs o<<1|1
#define lson o<<1|1,l,mid
#define rson o<<1|1,mid+1,r
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 3003, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int n, m;
struct BIT
{
	LL v[N][N];
	LL vy[N][N];
	LL vx[N][N];
	LL vyx[N][N];
	void init()
	{
		//MS(v, 0);MS(vy, 0);MS(vx, 0);MS(vyx, 0);
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= n; ++j)
			{
				v[i][j] = vy[i][j] = vx[i][j] = vyx[i][j];
			}
		}
	}
	void add(int y, int x, LL val)
	{
		for (int i = y; i <= n; i += i&-i)
		{
			for (int j = x; j <= n; j += j&-j)
			{
				v[i][j] ^= val;
				if (y - 1 & 1)vy[i][j] ^= val;
				if (x - 1 & 1)vx[i][j] ^= val;
				if ((y - 1) * (x - 1) & 1)vyx[i][j] ^= val;
			}
		}
	}
	LL check(int y, int x)
	{
		LL ret = 0;
		for (int i = y; i; i -= i&-i)
		{
			for (int j = x; j; j -= j&-j)
			{
				if (y * x & 1)ret ^= v[i][j];
				if (x & 1)ret ^= vy[i][j];
				if (y & 1)ret ^= vx[i][j];
				ret ^= vyx[i][j];
			}
		}
		return ret;
	}
	void ADD(int y1, int x1, int y2, int x2, LL val)
	{
		add(y1, x1, val);
		add(y2 + 1, x1, val);
		add(y1, x2 + 1, val);
		add(y2 + 1, x2 + 1, val);
	}
	LL CHECK(int y1, int x1, int y2, int x2)
	{
		LL ret = 0;
		ret ^= check(y2, x2);
		ret ^= check(y2, x1 - 1);
		ret ^= check(y1 - 1, x2);
		ret ^= check(y1 - 1, x1 - 1);
		return ret;
	}
}bit;
int main()
{
	while (~scanf("%d%d", &n, &m))
	{
		bit.init();
		for (int i = 1; i <= m; ++i)
		{
			char op; scanf(" %c", &op);
			int x1, y1, x2, y2;
			scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
			//if (x1 > x2)swap(x1, x2);
			//if (y1 > y2)swap(y1, y2);
			if (op == 'P')
			{
				int k; scanf("%d", &k);
				LL V = 0;
				while(k--)
				{
					int x, g; scanf("%d%d", &x, &g);
					if (g & 1)V ^= 1ll << x;
				}
				bit.ADD(y1, x1, y2, x2, V);
			}
			else
			{
				LL ans = bit.CHECK(y1, x1, y2, x2);
				for (int i = 1; i <= 50; ++i)
				{
					printf("%d ", (ans >> i & 1) + 1);
				}puts("");
			}
		}
	}
	return 0;
}
/*
【题意】
有一个n*n的矩形,每个点都有可能有1~50只怪兽
有:二维区间怪兽+1、二维区间怪兽奇偶性查询两种操作

n为3000
m为1e5

【类型】
二维树状数组 or 二维线段树

【分析】
用LL存状态
用二维树状数组实现操作

【时间复杂度&&优化】
O(16mlognlogn)

【数据】
2 10
P 1 1 2 1 2 3 1 4 2
Q 1 1 2 1
Q 2 1 2 1
P 1 1 2 2 1 1 1
P 2 2 2 2 2 1 1 2 1
Q 1 1 1 1
Q 2 2 2 2
Q 1 1 2 2
Q 1 1 1 2
P 1 1 2 2 1 1 1
P 2 2 2 2 2 1 1 2 1
Q 1 1 1 1
Q 2 2 2 2
Q 1 1 2 2
*/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值