TZOJ 2722 Matrix(树状数组区间取反单点查询)
描述
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(树状数组区间取反单点查询)的更多相关文章
- 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?
开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
- POJ 2155 Matrix (树状数组 && 区间计数)
题意 : 给出一个N*N的矩阵, 矩阵只有可能包含0或1, 一开始则全部是0.对于矩阵可以进行两种操作, 第一种是输入 C x1 y1 x2 y2 表示, 对以(x1, y1)为左上角, 以(x2, ...
- POJ 2155 Matrix 【二维树状数组】(二维单点查询经典题)
<题目链接> 题目大意: 给出一个初始值全为0的矩阵,对其进行两个操作. 1.给出一个子矩阵的左上角和右上角坐标,这两个坐标所代表的矩阵内0变成1,1变成0. 2.查询某个坐标的点的值. ...
- ACM-ICPC 2018 徐州赛区网络预赛 G. Trace-树状数组-区间修改,单点查询
赛后和队友讨论了一波,感谢无敌的队友给我细心的讲题 先埋坑 #include<iostream> #include<string.h> #include<algorith ...
- 【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 树状数组区间加法&区间求和操作
树状数组区间加法&区间求和操作 一般的树状数组解决区间加&单点询问并不复杂 但是要解决区间求和... 我们假设原数组是\(\{a_i\}\),差分数组\(\{d_i=a_i-a_{i- ...
- hdu 1754 I Hate It(树状数组区间求最值)2007省赛集训队练习赛(6)_linle专场
题意: 输入一行数字,查询第i个数到第j个数之间的最大值.可以修改其中的某个数的值. 输入: 包含多组输入数据. 每组输入首行两个整数n,m.表示共有n个数,m次操作. 接下来一行包含n个整数. 接下 ...
随机推荐
- Java中,&&与&,||与|的区别
在java的逻辑运算符中,有这么四类:&&(短路与),&,|,||(短路或). &&和&都是表示与,区别是&&只要第一个条件不满足,后面 ...
- ZEOSDBO-7安装
zeosdbo是一套免费开源的Delphi数据库连接组件,可连接mssql.mysql.sybase.oracle.firebird.sqlite.postgresql等多种数据库.调用方法简单. 连 ...
- linux command>file 2>&1 & 命令详解
command>file >& & 命令的最后一个&表示把该命令以后台的job的形式运行 一个命令的执行伴随着三种输入输出 标准输入(stdin):默认指向键盘的输 ...
- PL/SQL编码规范的一些建议
由于业务的复杂多变,我们编写完的程序,在后期肯定要被修改,而且修改的人很可能不是自己.这种情况我们都遇到过. 而且,看别人的代码可能会觉得很痛苦:为什么他要这样写相关逻辑?为什么变量名称要这样定义?换 ...
- CentOS 7下源码安装zabbix服务
安装环境需要LAMP或者LNMP先搭建好 在此我使用上一篇搭建好的LNMP环境来安装zabbix 1.下载zabbix http://www.zabbix.com/download.php 2.安装及 ...
- Hibernate 再接触 基础配置 搭建Log4j环境 Junit日志环境等
<!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.aut ...
- Java复习 之多线程
线程是一个程序中的不同路径 例子1 只有一条路径 每一个分支都是一个线程 实际上在一个时刻内 电脑只能运行一个进程 但是因为cpu运算速度很快 将时间分出来了 所以我们感觉是同时运行 创建线程的两种方 ...
- 传输层——UDP报文头介绍
16位源端口 16位目的端口 16位总长度 16位校验和 数据 源端口:长度为16位,2个字节. 目的端口:长度为16位,2个字节. 总长度:长度为16位,2个字节,表示 UDP包头长度 和 数据长度 ...
- TensorFlow saved_model 模块
最近在学tensorflow serving 模块,一直对接口不了解,后面看到这个文章就豁然开朗了, 主要的困难在于 tf.saved_model.builder.SavedModelBuilde ...
- 微信小程序生命周期——小程序的生命周期及页面的生命周期。
最近在做微信小程序开发,也发现一些坑,分享一下自己踩过的坑. 生命周期是指一个小程序从创建到销毁的一系列过程. 在小程序中 ,通过App()来注册一个小程序 ,通过Page()来注册一个页面. 首先来 ...