描述

Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1 <= i, j <= N).

We can change the matrix in the following
way. Given a rectangle whose upper-left corner is (x1, y1) and
lower-right corner is (x2, y2), we change all the elements in the
rectangle by using "not" operation (if it is a '0' then change it into
'1' otherwise change it into '0'). To maintain the information of the
matrix, you are asked to write a program to receive and execute two
kinds of instructions.

1. C x1 y1 x2 y2 (1 <= x1 <= x2
<= n, 1 <= y1 <= y2 <= n) changes the matrix by using the
rectangle whose upper-left corner is (x1, y1) and lower-right corner is
(x2, y2).
2. Q x y (1 <= x, y <= n) querys A[x, y].

输入

The
first line of the input is an integer X (X <= 10) representing the
number of test cases. The following X blocks each represents a test
case.

The first line of each block contains two numbers N and T
(2 <= N <= 1000, 1 <= T <= 50000) representing the size of
the matrix and the number of the instructions. The following T lines
each represents an instruction having the format "Q x y" or "C x1 y1 x2
y2", which has been described above.

输出

For each querying output one line, which has an integer representing A[x, y].

There is a blank line between every two continuous test cases.

样例输入

1
2 10
C 2 1 2 2
Q 2 2
C 2 1 2 1
Q 1 1
C 1 1 2 1
C 1 2 1 2
C 1 1 2 2
Q 1 1
C 1 1 2 1
Q 2 1

样例输出

1
0
0
1

题意

初始n*n的矩阵全为0

Q个操作

1.[X1,Y1]-[X2,Y2]中取反操作

2.查询[X1,Y1]的值

题解

1.区间更新分成4块,([X1,Y1]-[n,n])([X2,X2]-[n,n])([X2+1,Y1]-[n,n])([X1,Y2+1]-[n,n]),每个区间都+1操作,只保证[X1,Y1]-[X2,Y2]+1,其余+2或者+4

2.单点查询[X1,Y1]的值,只需要查询[X1,Y1]的值%2即可

代码

 #include<bits/stdc++.h>
using namespace std; const int N=;
int n; struct BIT2{
int sum[N][N];
void init(){memset(sum,,sizeof(sum));}
int lowbit(int x){return x&(-x);}
void update(int x,int y,int w)
{
for(int i=x;i<=n;i+=lowbit(i))
for(int j=y;j<=n;j+=lowbit(j))
sum[i][j]+=w;
}
int query(int x,int y)
{
int ans=;
for(int i=x;i>;i-=lowbit(i))
for(int j=y;j>;j-=lowbit(j))
ans+=sum[i][j];
return ans;
}
}T; int main()
{
int t,q,o;
scanf("%d",&t);
while(t--)
{
if(o++)printf("\n");
T.init();
scanf("%d%d",&n,&q);
for(int i=;i<q;i++)
{
char op[];
int x1,y1,x2,y2;
scanf("%s",op);
if(op[]=='C')
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
T.update(x1,y1,);
T.update(x2+,y1,);
T.update(x1,y2+,);
T.update(x2+,y2+,);
}
else
scanf("%d%d",&x1,&y1),printf("%d\n",T.query(x1,y1)%);
}
}
}

TZOJ 2722 Matrix(树状数组区间取反单点查询)的更多相关文章

  1. 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?

    开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...

  2. POJ 2155 Matrix (树状数组 && 区间计数)

    题意 : 给出一个N*N的矩阵, 矩阵只有可能包含0或1, 一开始则全部是0.对于矩阵可以进行两种操作, 第一种是输入 C x1 y1 x2 y2 表示, 对以(x1, y1)为左上角, 以(x2, ...

  3. POJ 2155 Matrix 【二维树状数组】(二维单点查询经典题)

    <题目链接> 题目大意: 给出一个初始值全为0的矩阵,对其进行两个操作. 1.给出一个子矩阵的左上角和右上角坐标,这两个坐标所代表的矩阵内0变成1,1变成0. 2.查询某个坐标的点的值. ...

  4. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace-树状数组-区间修改,单点查询

    赛后和队友讨论了一波,感谢无敌的队友给我细心的讲题 先埋坑 #include<iostream> #include<string.h> #include<algorith ...

  5. 【poj2155】Matrix(二维树状数组区间更新+单点查询)

    Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...

  6. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  7. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  8. 树状数组区间加法&区间求和操作

    树状数组区间加法&区间求和操作 一般的树状数组解决区间加&单点询问并不复杂 但是要解决区间求和... 我们假设原数组是\(\{a_i\}\),差分数组\(\{d_i=a_i-a_{i- ...

  9. hdu 1754 I Hate It(树状数组区间求最值)2007省赛集训队练习赛(6)_linle专场

    题意: 输入一行数字,查询第i个数到第j个数之间的最大值.可以修改其中的某个数的值. 输入: 包含多组输入数据. 每组输入首行两个整数n,m.表示共有n个数,m次操作. 接下来一行包含n个整数. 接下 ...

随机推荐

  1. Hadoop 3.0 安装

    1.      下载Hadoop 3.0 http://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/hadoop-3.0.0/hadoop-3. ...

  2. vue下载和上传excle数据文件,解析excel文件数据并存在数据库中

    下载: VUE: window.open("xxxx/downloadOldTaskDataFile.do_", "_blank"); JAVA: /** * ...

  3. day16-小数据池

    一,什么是代码块 Python程序是由代码块构造的.块是一个python程序的文本,他是作为一个单元执行的. 代码块:一个模块,一个函数,一个类,一个文件等都是一个代码块. 而作为交互方式输入的每个命 ...

  4. 函数参数,const 引用 和 非 const引用是不同的函数。

    举个例子, void f(const int &x) 和 void f(int &x) 是不同的函数. 函数的返回值不能作为区分

  5. rocketmq 4.2.0 版本 控制台本地搭建(史上最简单教程)

    就像发现新大陆一般,瞎折腾,搞出来了..并没有网上说的一大串....(本人公司的项目从未使用过springboot....) rocketmq  控制台,官方使用springboot 做后端,前端使用 ...

  6. KMP算法next数组求解

    关于KMP算法,许多教材用的是递推式求解,虽然代码简洁,但是有些不好理解,这里我介绍一种迭代求next数组的方法 KMP算法关键部分就是滑动模式串,我们可以每次滑动一个单位,直到出现可能匹配的情况,此 ...

  7. javascript 中的 innerHTML 是什么意思

    innerHTML在JS是双向功能:获取对象的内容 或 向对象插入内容:如:<div id="aa">这是内容</div> ,我们可以通过 document ...

  8. E_FAIL (0x80004005) MachineWrap

    下载VirtualBox-4.3.12-93733-Win.exe,下载地址:http://download.virtualbox.org/virtualbox/4.3.12/

  9. 在CentOS7中利用yum命令安装mysql

    在CentOS7中利用yum命令安装mysql 原创 2016年08月31日 10:42:33 标签: mysql / centos 4832 一.说明 我们是在VMware虚拟机上安装的mysql, ...

  10. 火兰hillstone与fortigate之ipsec v.p.n连接实践

    文章目录 参考文档: http://ismailaktas.com.tr/hillstone-to-fortigate-ipsec-vpn/ https://wenku.baidu.com/view/ ...