CodeForces 341D Iahub and Xors(二维树状数组)

思路:典型的二维异或树状数组,有一个结论是在x位置放置了一个元素,只会对x+2,x+4等位置产生影响


#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn = 1005;
LL lowbit(LL x){return x&(-x);}
LL c[2][2][maxn][maxn];
int n,m;
void update(int x1,int y1,LL v)
{
    for(int i = x1;i<=maxn;i+=lowbit(i))
		for(int j = y1;j<=maxn;j+=lowbit(j))
			c[x1&1][y1&1][i][j]^=v;
}
LL query(int x1,int y1)
{
	LL ans = 0;
	for(int i=x1;i;i-=lowbit(i))
		for(int j = y1;j;j-=lowbit(j))
			ans^=c[x1&1][y1&1][i][j];
	return ans;
}
int main()
{
    scanf("%d%d",&n,&m);
	for(int i = 1;i<=m;i++)
	{
        int op,x1,y1,x2,y2;
		scanf("%d%d%d%d%d",&op,&x1,&y1,&x2,&y2);
		if(op==1)
		{
			LL ans = 0;
			ans^=query(x1-1,y1-1);
			ans^=query(x1-1,y2);
			ans^=query(x2,y1-1);
			ans^=query(x2,y2);
            printf("%lld\n",ans);
		}
		else
		{
			LL v;
            scanf("%lld",&v);
			update(x1,y1,v);
			update(x1,y2+1,v);
			update(x2+1,y1,v);
			update(x2+1,y2+1,v);
		} 
	}
}


Description

Iahub does not like background stories, so he'll tell you exactly what this problem asks you for.

You are given a matrix a with n rows and n columns. Initially, all values of the matrix are zeros. Both rows and columns are 1-based, that is rows are numbered 1, 2, ..., n and columns are numbered 1, 2, ..., n. Let's denote an element on the i-th row and j-th column asai, j.

We will call a submatrix (x0, y0, x1, y1) such elements ai, j for which two inequalities hold: x0 ≤ i ≤ x1y0 ≤ j ≤ y1.

Write a program to perform two following operations:

  1. Query(x0y0x1y1): print the xor sum of the elements of the submatrix (x0, y0, x1, y1).
  2. Update(x0y0x1y1v): each element from submatrix (x0, y0, x1, y1) gets xor-ed by value v.

Input

The first line contains two integers: n (1 ≤ n ≤ 1000) and m (1 ≤ m ≤ 105). The number m represents the number of operations you need to perform. Each of the next m lines contains five or six integers, depending on operation type.

If the i-th operation from the input is a query, the first number from i-th line will be 1. It will be followed by four integers x0y0x1,y1. If the i-th operation is an update, the first number from the i-th line will be 2. It will be followed by five integers x0y0x1y1v.

It is guaranteed that for each update operation, the following inequality holds: 0 ≤ v < 262. It is guaranteed that for each operation, the following inequalities hold: 1 ≤ x0 ≤ x1 ≤ n1 ≤ y0 ≤ y1 ≤ n.

Output

For each query operation, output on a new line the result.

Sample Input

Input
3 5
2 1 1 2 2 1
2 1 3 2 3 2
2 3 1 3 3 3
1 2 2 3 3
1 2 2 3 2
Output
3
2

Hint

After the first 3 operations, the matrix will look like this:

1 1 2
1 1 2
3 3 3

The fourth operation asks us to compute 1 xor 2 xor 3 xor 3 = 3.

The fifth operation asks us to compute 1 xor 3 = 2.



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值