http://codeforces.com/contest/341/problem/D
每次给一个矩阵,将其异或上一个数,或者查询这个矩阵的异或和。
根据二维树状数组的矩阵加减矩阵求和,结合异或的奇偶性质。
//poj1195
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
const double pi=acos(-1.0);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
int n,m;
const int maxn=1000+5;
int s;
struct tree
{
ll num[2][2][maxn][maxn];
inline int lowbit(int x)
{
return x&-x;
}
void add(int x,int y,ll a)
{
int i,j;
for (i=x; i<=s; i+=lowbit(i))
for (j=y; j<=s; j+=lowbit(j))
num[x&1][y&1][i][j]^=a;
}
ll query(int x,int y)
{
ll ans=0,i,j;
for (i=x; i>0; i-=lowbit(i))
for (j=y; j>0; j-=lowbit(j))
ans^=num[x&1][y&1][i][j];
return ans;
}
};
tree tp;
int main()
{
int op;
int l,b,r,t;
ll val;
scanf("%d%d",&n,&m);
s=n;
for (int i=1; i<=m; i++)
{
scanf("%d",&op);
if (op==2)
{
scanf("%d%d%d%d",&l,&b,&r,&t);
scanf("%lld",&val);
tp.add(l,b,val);
tp.add(r+1,b,val);
tp.add(l,t+1,val);
tp.add(r+1,t+1,val);
}
else
{
scanf("%d%d%d%d",&l,&b,&r,&t);
ll ret=tp.query(r,t);
ret^=tp.query(l-1,t);
ret^=tp.query(r,b-1);
ret^=tp.query(l-1,b-1);
printf("%lld\n",ret);
}
}
return 0;
}
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define _INF 0x3f3f3f3f
#define Mod 9999991
#define lowbit(x) (x&(-x))
using namespace std;
const int N = 1005;
typedef long long ll;
ll a[N][N];
ll ax[N][N];
ll ay[N][N];
ll axy[N][N];
void Updata(ll a[N][N],int x,int y,int n,int m,ll data)
{
for(; x <= n; x += lowbit(x))
{
for(int t = y; t <= m; t += lowbit(t))
{
a[x][t] ^= data;
}
}
}
ll Query(ll a[N][N],int x,int y)
{
ll sum = 0;
while(x > 0)
{
for(int t = y; t > 0; t -= lowbit(t))
{
sum ^= a[x][t];
}
x -= lowbit(x);
}
return sum;
}
ll get(ll x)
{
return x&1;
}
ll sum(int x,int y)
{
ll r1=Query(a,x,y)*get((x+1)*(y+1)) ;
r1^=get(y+1)*Query(ax,x,y);
r1^=get(x+1)*Query(ay,x,y);
r1^= Query(axy,x,y);
return r1;
}
void Add(int x1,int y1,int x2,int y2,int n,int m,ll data)
{
Updata(a,x1,y1,n,m,data);
Updata(a,x2+1,y1,n,m,data);
Updata(a,x1,y2+1,n,m,data);
Updata(a,x2+1,y2+1,n,m,data);
Updata(ax,x1,y1,n,m,get(x1)*data);
Updata(ax,x2+1,y1,n,m,get(x2+1)*data);
Updata(ax,x1,y2+1,n,m,get(x1)*data);
Updata(ax,x2+1,y2+1,n,m,get(x2+1)*data);
Updata(ay,x1,y1,n,m,get(y1)*data);
Updata(ay,x2+1,y1,n,m,get(y1)*data);
Updata(ay,x1,y2+1,n,m,get(y2+1)*data);
Updata(ay,x2+1,y2+1,n,m,get(y2+1)*data);
Updata(axy,x1,y1,n,m,get(x1*y1)*data);
Updata(axy,x2+1,y1,n,m,get((x2+1)*y1)*data);
Updata(axy,x1,y2+1,n,m,get(x1*(y2+1))*data);
Updata(axy,x2+1,y2+1,n,m,get((x2+1)*(y2+1))*data);
}
// Add(x1,y1,x2,y2,n,m,data);
// printf("%d\n",sum(x2,y2) - sum(x1-1,y2) - sum(x2,y1-1) + sum(x1-1,y1-1));
int main()
{
int op;
int l,b,r,t;
ll val;
int n,m;
scanf("%d%d",&n,&m);
for (int i=1; i<=m; i++)
{
scanf("%d",&op);
if (op==2)
{
scanf("%d%d%d%d",&l,&b,&r,&t);
scanf("%lld",&val);
/* tp.add(l,b,val);
tp.add(r+1,b,val);
tp.add(l,t+1,val);
tp.add(r+1,t+1,val);*/
Add(l,b,r,t,n,n,val);
}
else
{
scanf("%d%d%d%d",&l,&b,&r,&t);
ll ret=sum(r,t);
ret^=sum(l-1,t);
ret^=sum(r,b-1);
ret^=sum(l-1,b-1);
printf("%lld\n",ret);
}
}
return 0;
}