D. Iahub and Xors
 

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 as ai, j.

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

Write a program to perform two following operations:

  1. Query(x0, y0, x1, y1): print the xor sum of the elements of the submatrix (x0, y0, x1, y1).
  2. Update(x0, y0, x1, y1, v): 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 x0, y0, x1, y1. If thei-th operation is an update, the first number from the i-th line will be 2. It will be followed by five integers x0, y0, x1, y1, v.

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 ≤ n, 1 ≤ y0 ≤ y1 ≤ n.

Output

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

Examples
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
Note

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.

题意:

  给你一个n×n的矩阵;初始时所有位置的值都为0;

  m次操作:两种操作,一种是将某个矩形区域的值异或v,另一种是求一个矩形区域的异或和。

题解:

  想办法维护(1,1)(x,y)的异或和,那么矩形区域的异或和就可以根据四个位置的异或和求出。由于异或操作比较特殊,在一个区域进行异或操作,那么这个区域的某个点的(1,1)(x,y)的异或和要么要异或上v,要么异或上0。这样,我们就可以利用二维树状数组,只进行单点更新就完成更新矩形区域的操作,另外,由于区域是否被更新跟奇偶性有关,因此跟据奇偶性来维护4种情况的树状数组。

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
const long long INF = 1e18+1LL;
const double Pi = acos(-1.0);
const int N = 1e3+, M = 1e5+, mod = 1e9+, inf = 2e9; int n,m,mp[N][N];
LL C[][N][N]; int get(int x,int y) {
int res = ;
if(x&) res += ;
if(y&) res += ;
return res;
}
LL ask(int x,int y) {
LL s = ;
int wh = get(x,y);
for(int i = x; i; i-= i & (-i))
for(int j = y; j; j -= j & (-j)) s^=C[wh][i][j];
return s;
}
void update(int x,int y,LL v) {
int wh = get(x,y);
for(int i = x; i < N; i+= i&(-i))
for(int j = y; j < N; j += j&(-j)) C[wh][i][j] ^= v;
}
int main() {
scanf("%d%d",&n,&m);
for(int i = ; i <= m; ++i) {
int op,x1,x2,y1,y2;
LL v;
scanf("%d%d%d%d%d",&op,&x1,&y1,&x2,&y2);
if(op == ) {
LL a = ask(x2,y2);
LL b = , c = , d = ;
if(y1 > ) b = ask(x2,y1-);
if(x1 > ) c = ask(x1-,y2);
if(x1 > && y1 > ) d = ask(x1-,y1-);
printf("%I64d\n",a^b^d^c);
} else {
scanf("%I64d",&v);
update(x2+,y2+,v);
update(x2+,y1,v);
update(x1,y2+,v);
update(x1,y1,v);
}
}
return ;
}

Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*的更多相关文章

  1. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

    题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...

  2. Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)

    http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...

  3. Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)

    题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...

  4. Codeforces Round 261 Div.2 D Pashmak and Parmida's problem --树状数组

    题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求有多少对这样的(i,j). 解法:分别从左到右,由右到 ...

  5. Codeforces Round #590 (Div. 3)【D题:26棵树状数组维护字符出现次数】

    A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...

  6. VK Cup 2016 - Round 1 (Div. 2 Edition) B. Bear and Displayed Friends 树状数组

    B. Bear and Displayed Friends 题目连接: http://www.codeforces.com/contest/658/problem/B Description Lima ...

  7. Codeforces Round #371 (Div. 1) D. Animals and Puzzle 二维倍增

    D. Animals and Puzzle 题目连接: http://codeforces.com/contest/713/problem/D Description Owl Sonya gave a ...

  8. Codeforces 707 E. Garlands (二维树状数组)

    题目链接:http://codeforces.com/problemset/problem/707/E 给你nxm的网格,有k条链,每条链上有len个节点,每个节点有一个值. 有q个操作,操作ask问 ...

  9. Codeforces Round #198 (Div. 2) E. Iahub and Permutations —— 容斥原理

    题目链接:http://codeforces.com/contest/340/problem/E E. Iahub and Permutations time limit per test 1 sec ...

随机推荐

  1. POJ 3752

    http://poj.org/problem?id=3752 这是一道我觉得还蛮有意思的题目,不难,是个水题,但我也TLE了几次,感到很奇怪,这么简单的循环还TLE,最后一想,肯定是有几个例子我是没有 ...

  2. Django CRM __contains与__icontains区别

    http://www.yihaomen.com/article/python/199.htm operators = { 'exact': '= %s', 'iexact': 'LIKE %s', ' ...

  3. windows hosts

    2015年6月29日 11:10:56 星期一 windows 的 hosts 文件生效机制 以最前边的为准, 重复的硬解析只有第一次出现的地方生效

  4. 请不要用SECONDS_BEHIND_MASTER来衡量MYSQL主备的延迟时间【转】

    本文来自:http://www.woqutech.com/?p=1116 MySQL 本身通过 show slave status 提供了 Seconds_Behind_Master ,用于衡量主备之 ...

  5. js验证手机号

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  6. tomcat URL乱码问题

    用get传参时,显示乱码 在tomcat里的server.xml中添加一下即可. <Connector port="8080" protocol="HTTP/1.1 ...

  7. 通过configSource提高web.config配置灵活性

    很多时候我们会有这样的情况,开发环境和测试环境中的配置文件是不一样的,最明显的就是数据库连接串,这样,每次我们发布一个测试版本,都要手动去修改一下配置文件,是不是很麻烦的说.其实利用web.confi ...

  8. Mysql 基础2

    创建数据库:     create  database/*条件*/+ text3/*数据库名称*/ 创建数据库  步骤:查询   创建查询  查询编辑器 (写代码) 删除数据库: drop datab ...

  9. SSM 加载配置文件

    配置文件中 <bean id="address" class="org.springframework.beans.factory.config.Propertie ...

  10. UI课堂笔记

    2016.7.18 + (UIColor *)blackColor; + (UIColor *)darkGrayColor;   深灰色 + (UIColor *)lightGrayColor;  浅 ...