POJ 2155 Matrix【二维树状数组+YY(区间计数)】
题目链接:http://poj.org/problem?id=2155
Matrix
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 32950 | Accepted: 11943 |
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
Source
题意概括:
有一个初始值为0的N*N的二维矩阵,有T次操作,每次操作有两种选择:
C : 修改以(x1, y1)为左上角(x2, y2)为右下角的矩阵的值(0和1互换)
Q:查询(x, y)的值为 0 或者 为 1;
解题思路:
涉及到多次区间修改和区间查询的优先考虑线段树和树状数组,这道题巧妙之处在于灵活运用树状数组的前缀和,把区间修改转换单点修改,一维需要标记两个点而二维需要标记四个点,一个点用于发挥效果,另外三个点用于消除效果,因为树状数组维护的是前缀和,而对于二维树状数组,查询的则是以(1,1)为左上角,(x,y)为右下角的矩阵的和。
第二就是我们可以借助修改次数的奇偶性来判断该点的值为 0 / 1;
例如:N = 3;C:x1 = 1, y1 = 1, x2 = 2, y2 = 2;
1 | 1 | |
(x,y) | ||
1 | 1 |
(因为我们只需要知道奇偶性,所以矩阵外的点加1即可消除效果)
AC code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define ll long long int;
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 1e3+; int mmp[MAXN][MAXN];
int N, T; int lowbit(int x)
{
return x&(-x);
} void add(int x, int y, int value)
{
for(int i = x; i <= N; i += lowbit(i))
for(int j = y; j <= N; j += lowbit(j))
mmp[i][j]+=value;
} int sum(int x, int y)
{
int res = ;
for(int i = x; i > ; i -= lowbit(i))
for(int j = y; j > ; j -= lowbit(j))
res+=mmp[i][j];
return res;
} void init()
{
for(int i = ; i <= N; i++)
for(int j = ; j <= N; j++)
mmp[i][j] = ;
} int main()
{
int T_case;
char com[];
int x, y, x1, x2, y1, y2;
scanf("%d", &T_case);
while(T_case--)
{
scanf("%d%d", &N, &T);
init();
while(T--)
{
scanf("%s", &com);
if(com[] == 'C')
{
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
add(x1, y1, );
add(x2+, y1, );
add(x1, y2+, );
add(x2+, y2+, );
}
else if(com[] == 'Q')
{
scanf("%d%d", &x, &y);
int res = ;
res = sum(x, y);
// printf("res: %d\n", res);
if(res%) printf("1\n");
else printf("0\n");
}
}
puts("");
}
return ;
}
POJ 2155 Matrix【二维树状数组+YY(区间计数)】的更多相关文章
- POJ 2155 Matrix(二维树状数组,绝对具体)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20599 Accepted: 7673 Descripti ...
- poj 2155 Matrix (二维树状数组)
题意:给你一个矩阵开始全是0,然后给你两种指令,第一种:C x1,y1,x2,y2 就是将左上角为x1,y1,右下角为x2,y2,的这个矩阵内的数字全部翻转,0变1,1变0 第二种:Q x1 y1,输 ...
- POJ 2155:Matrix 二维树状数组
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 21757 Accepted: 8141 Descripti ...
- POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)
题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- 【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 2029 (二维树状数组)题解
思路: 大力出奇迹,先用二维树状数组存,然后暴力枚举 算某个矩形区域的值的示意图如下,代码在下面慢慢找... 代码: #include<cstdio> #include<map> ...
- poj----2155 Matrix(二维树状数组第二类)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16950 Accepted: 6369 Descripti ...
- POJ2155【二维树状数组,区间修改,点查询?】【又被输入输出坑】
这题反反复复,到现在才过. 这道题就是树状数组的逆用,用于修改区间内容,查询点的值. 如果单纯就这个奇偶数来判的话,似乎这个思路比较好理解. 看了一下国家集训队论文(囧),<关于0与1在信息学奥 ...
随机推荐
- Reading a IMU Without Kalman: The Complementary Filter
目标是将惯性测量元件(IMU)之中陀螺仪.加速计的数据结合使用.Kalman filter太复杂,在微机上倾向用一种更简单的方法:Complementary filter 姿态估计(获得3个角度,俯仰 ...
- rewrite 功能
一, rewrite 地址重写与地址转发 区别: 1,地址转发后客户端浏览器地址栏中的地址时不会改变的;而地址重写的话客户端浏览器地址栏会改变为服务器确定的地址 2, 在一次地址转发过程中,只产生一次 ...
- (转)Cobbler无人值守批量安装Linux系统
本文目录: 1.1 pxe安装系统 1.2 cobbler基本介绍 1.3 安装和配置cobbler 1.3.1 安装cobbler 1.3.2 配置dhcp和tftp 1.4 cobbler从本地光 ...
- Beam概念学习系列之Pipeline Runners
不多说,直接上干货! https://beam.apache.org/get-started/beam-overview/ 在 Beam 管道上运行引擎会根据你选择的分布式处理引擎,其中兼容的 API ...
- lua-遍历集合-ipairs和pairs的区别
--ipairs和pairs的区别arr = {1,3,[5]=5,name="kaikai",age=12, 89}--arr[4]= 23--ipairs--ipairs仅仅遍 ...
- js获取当前日期,格式为YYYY-MM-DD
//获取当前时间,格式YYYY-MM-DD function getNowFormatDate() { var date = new Date(); var seperator1 = "-& ...
- DW网页制作,数学,数据库管理
数学(函数关系的建立) 函数关系:确定性现象之间的关系常常表现为函数关系,即一种现象的数量确定以后,另一种现象的数量也随之完全确定,表现为一种严格的函数关系. 如:记为y=f(x),其中x称为自变量, ...
- 项目管理系统 TAIGA 部署
题记 使用了 MantisBT 一段时间,觉得功能太少,只局限在错误跟踪,而且操作体验比较差,界面很糟糕,很早就想将其换掉. 偶然发现一个很不错的新选择:Taiga,于是就试着将其部署下来,发现绝对是 ...
- ZROJ#397. 【18提高7】模仿游戏(爆搜)
题意 题目链接 Sol 考试的时候调了1.5h没调出来我真是菜爆了... 读完题目后不难发现,每次约束的条件相当于是\(b[((x[i] + i) % N + (i / N) % N) % N] = ...
- 图解JavaScript中的原型链
转自:http://www.jianshu.com/p/a81692ad5b5d typeof obj 和 obj instanceof Type 在JavaScript中,我们经常用typeof o ...