题解【POJ2155】Matrix
Description
Given an \(N \times 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 \leq i, j \leq 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.
C x1 y1 x2 y2
\((1 \leq x1 \leq x2 \leq n, 1 \leq y1 \leq y2 \leq n)\) changes the matrix by using the rectangle whose upper-left corner is \((x1, y1)\) and lower-right corner is \((x2, y2)\).Q x y
\((1 \leq x, y \leq n)\) querys \(A[x, y]\).
Input
The first line of the input is an integer \(X (X \leq 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 \leq N \leq 1000, 1 \leq T \leq 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
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.
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
POJ Monthly,Lou Tiancheng
Solution
题意简述:一个\(N \times N\)的\(01\)矩阵,和几种动态操作,包括对子矩阵\((x,y,xx,yy)\)的所有元素异或,查询某一点\((x,y)\)的元素值。
二维树状数组裸题。
异或的操作不难修改。
二维树状数组与一维树状数组不同的是:
- 一维树状数组维护的是一条链,而二维树状数组维护的却是一片区域。
- 一维树状数组更新和查找只有一重循环,而二维树状数组需要两重循环。
- 二维树状数组比一维树状数组多一维度。
经过以上分析,\(AC\)代码就不难得出了。
Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
using namespace std;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar();}
return f * x;
}
int n, m, c[1003][1003], t;
bool fl = false;
inline void add(int x, int y)//二维树状数组的修改操作
{
for (int i = x; i <= n; i = i + (i & (-i)))
{
for (int j = y; j <= n; j = j + (j & (-j)))//注意两重循环
{
++c[i][j];
}
}
}
inline int getans(int x, int y)//二维树状数组的查询操作
{
int ans = 0;
for (int i = x; i; i = i - (i & (-i)))
{
for (int j = y; j; j = j - (j & (-j)))
{
ans = ans + c[i][j];//加上答案
}
}
return ans;
}
int main()
{
int t = gi();
while (t--)
{
if (!fl) fl = true;
else puts("");
n = gi(), m = gi();
memset(c, 0, sizeof(c));
while (m--)
{
char c;
cin >> c;
if (c == 'C')
{
int x = gi(), y = gi(), xx = gi(), yy = gi();
add(x, y), add(xx + 1, y), add(x, yy + 1), add(xx + 1, yy + 1);//进行插入操作
}
else
{
int x = gi(), y = gi();
printf("%d\n", getans(x, y) % 2);//输出最终答案
}
}
}
return 0;//结束
}
题解【POJ2155】Matrix的更多相关文章
- [LeetCode 题解] Spiral Matrix
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...
- POJ2155 Matrix 【二维线段树】
题目链接 POJ2155 题解 二维线段树水题,蒟蒻本想拿来养生一下 数据结构真的是有毒啊,, TM这题卡常 动态开点线段树会TLE[也不知道为什么] 直接开个二维数组反倒能过 #include< ...
- POJ2155 Matrix 【二维树状数组】+【段更新点查询】
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17766 Accepted: 6674 Descripti ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
- POJ2155:Matrix(二维树状数组,经典)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- C++题解:Matrix Power Series ——矩阵套矩阵的矩阵加速
Matrix Power Series r时间限制: 1 Sec 内存限制: 512 MB 题目描述 给定矩阵A,求矩阵S=A^1+A^2+--+A^k,输出矩阵,S矩阵中每个元都要模m. 数据范围: ...
- poj----2155 Matrix(二维树状数组第二类)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16950 Accepted: 6369 Descripti ...
- 【题解】Matrix BZOJ 4128 矩阵求逆 离散对数 大步小步算法
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4128 大水题一道 使用大步小步算法,把数字的运算换成矩阵的运算就好了 矩阵求逆?这么基础的线 ...
- 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 ...
随机推荐
- 剑指offer-面试题14-剪绳子-贪婪算法
/* 题目: 给定一个长度为n的绳子,把绳子剪为m段,(n>1,m>1) 求各段绳子乘积的最大值. */ /* 思路: 贪婪算法. 当绳子的长度大于5时,尽可能多的剪长度为3的绳子:当剩下 ...
- linux vi编辑器光标跳到文件末尾
1.:0,:1,gg都可以到第一行2.shift+g到末行3.或者:$=检查总行数比如24,:24到第24行
- 数据结构与算法之非比较排序【Java】
比较排序与非比较排序的对比 常见的快速排序.归并排序.堆排序.冒泡排序等属于比较排序.在排序的最终结果里,元素之间的次序依赖于它们之间的比较.每个数都必须和其他数进行比较,才能确定自己的位置.在冒泡排 ...
- C语言面试题 02.03. 删除中间节点
实现一种算法,删除单向链表中间的某个节点(除了第一个和最后一个节点,不一定是中间节点),假定你只能访问该节点. 示例: 输入:单向链表a->b->c->d->e->f中的 ...
- Win10下安装tensorflow详细过程
首先声明几点: 安装tensorflow是基于Python的,并且需要从Anaconda仓库中下载. 所以我们的步骤是:先下载Anaconda,再在Anaconda中安装一个Python,(你的电脑里 ...
- (三)运用Python模块和包
1 引言 为了能够在Python项目中高效地运用Python模块和包,我们需要进一步地来了解它们是如何在Python项目中进行定义.使用和工作的. 2 Python模块和包 Python模块和包的基本 ...
- Ioc依赖注入:Unity4.0.1 在项目中的应用 (MVC和API)
使用Unity的好处网上有很多,百度一下即可 这里引用了一篇关于面向接口编程的好处的文章作为引申:https://blog.csdn.net/Cyy19970527/article/details/8 ...
- .NetCore学习笔记:二、基于Dapper的泛型Repository
为减少代码量,这里实现一个基于Dapper的泛型Repository. 这里需要引用Dapper.dll和Dapper.Contrib.dll. 接口定义: /// <summary> / ...
- Java中基本数据类型byte的溢出问题
Java中基本数据类型byte的溢出问题 问题源于:https://www.cnblogs.com/HuoHua2020/p/12326631.html 定义两个byte类型的数据,将其之和赋值给一个 ...
- c++ STL vector初步学习
/*vector(向量):是一种顺序容器,,动态数组,事实上和数组差不多,但它比数组更优越.一般来说数组不能动态拓展,因此在程序运行的时候不是浪费内存,就是造成越界.而vector正好弥补了这个缺陷, ...