【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description
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].
Input
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.
Output
There is a blank line between every two continuous test cases.
Sample Input
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
Sample Output
1
0
0
1
【题意】
n*n坐标图起初都为0,C:翻转左下和右上两个坐标围成的矩阵中所有点,Q:查询此点的0 1状态
【思路】
二维树状数组区间修改+单点查询模板题,先考虑一维。
一维单点查询就是前缀和,即query(x)。
区间修改先让s-n都加num,再让t+1-n减去num,即update(s, num),update(t+1, -num)。
二维的单点查询变成二维就好了query(x, y)。
区间修改update(x1, y1, num), update(x2+1, y1, -num), update(x1, y2+1, -num), update(x2+1, y2+1, num)。开始也是黑人问号,在纸上画几笔就看出来了。
这道题问得是0 1状态,那么只要统计翻转次数是偶次还是奇次就行了
【代码】
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = ;
int c[N][N], n;
int lowbit(int x)
{
return x&(-x);
}
void update(int x, int y, int m)
{
for(int i = x; i <= n; i += lowbit(i))
for(int j = y; j <= n; j += lowbit(j))
c[i][j] += m;
}
int query(int x, int y)
{
int sum = ;
for(int i = x; i > ; i -= lowbit(i))
for(int j = y; j > ; j-= lowbit(j))
sum += c[i][j];
return sum;
}
int main()
{
int t, m;
cin>>t;
while(t--)
{
memset(c, , sizeof c);
scanf("%d%d", &n, &m);
while(m--)
{
char c;
scanf(" %c", &c);
if(c == 'C')
{
int x1, x2, y1, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
update(x1, y1, );
update(x1, y2+, -);
update(x2+, y1, -);
update(x2+, y2+, );
}
else
{
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", query(x, y)&);
}
}
if(t) puts("");
}
return ;
}
【poj2155】Matrix(二维树状数组区间更新+单点查询)的更多相关文章
- POJ2155 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 an ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?
开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- POJ-2155 Matrix---二维树状数组+区域更新单点查询
题目链接: https://vjudge.net/problem/POJ-2155 题目大意: 给一个n*n的01矩阵,然后有两种操作(m次)C x1 y1 x2 y2是把这个小矩形内所有数字异或一遍 ...
- poj2155二维树状数组区间更新
垃圾poj又交不上题了,也不知道自己写的对不对 /* 给定一个矩阵,初始化为0:两种操作 第一种把一块子矩阵里的值翻转:0->1,1->0 第二种询问某个单元的值 直接累计单元格被覆盖的次 ...
- poj----2155 Matrix(二维树状数组第二类)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16950 Accepted: 6369 Descripti ...
- HDU 4031 Attack(线段树/树状数组区间更新单点查询+暴力)
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Sub ...
- NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)
Problem 1050: Just Go Time Limits: 3000 MS Memory Limits: 65536 KB 64-bit interger IO format: % ...
随机推荐
- Linux(8)- nginx+uWSGI+virtualenv+supervisor 发布web服务器
一.理论梳理 WSGI是web服务器的网关接口,它是一个规范,描述了web服务器(下图中的WEB server)如何与web应用程序(下图中的Application)通信,以及web应用程序如何链接在 ...
- Andrew Ng机器学习编程作业:Regularized Linear Regression and Bias/Variance
作业文件: machine-learning-ex5 1. 正则化线性回归 在本次练习的前半部分,我们将会正则化的线性回归模型来利用水库中水位的变化预测流出大坝的水量,后半部分我们对调试的学习算法进行 ...
- 解决[[NSFileManager defaultManager] contentsOfDirectoryAtPath 方法获取不到数据的bug
在说这个问题之前,必须先解释一下,我们在引入project的时候,xcode会给我们3个选项 1.Copy items if needed 主要是说明,是否要将文件复制到project所在文件夹. 假 ...
- 购物单问题—WPS使用excel
**** 180.90 88折 **** 10.25 65折 **** 56.14 9折 **** 104.65 ...
- SAP 后台job
SAP 如何定义后台job 有两种方 1是se38执行可执行程序后,菜单栏‘程序’--->'后台执行',输入输出设备,ENTER两次后,选择开始时间(立刻执行,或定义日期时间,也可周期执行..) ...
- ipv6被拒的解决方法
A,检测服务器是否支持ipv6连接 用mac 搭建IPv6环境测试,只能测试客户端到mac这段网络正常,但是mac到服务器还是IPv4的,所以没有办法测试服务器的IPv6是否正常.可以用手机打开2)步 ...
- python 系统相关操作
1.文件 open()代开文件或者创建文件 fout=open('oops.txt','wt') print('Oops, I created a file.',file=fout) fout.clo ...
- BCH码
http://baike.baidu.com/link?url=CfLtm9DigwWdup-9VJP99RG65NgaVOXfrnjT61ogP7au0QOrlypq72k67B0s1Ey-Q1yD ...
- Ubuntu安装及一些初始操作
目录 使用Universal-USB-Installer安装Ubuntu Ubuntu连接无线网络 Windows与Ubuntu双系统时间不一致解决办法 Ubuntu安装Sublime Text 3 ...
- POJ 2516 Minimum Cost (KM最优匹配)
题意:有N家家店,每家店都对K种货物有需求:同时有M家仓库,对K钟货物有供应.对于每种货物,每个仓库送至每家店都有自己的单位费用.求满足所有店所有货物的最小费用 分析:对于每一种货物,如果总需求大于总 ...