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.

  1. 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)\).

  2. 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. 1
  2. 2 10
  3. C 2 1 2 2
  4. Q 2 2
  5. C 2 1 2 1
  6. Q 1 1
  7. C 1 1 2 1
  8. C 1 2 1 2
  9. C 1 1 2 2
  10. Q 1 1
  11. C 1 1 2 1
  12. Q 2 1

Sample Output

  1. 1
  2. 0
  3. 0
  4. 1

Source

POJ Monthly,Lou Tiancheng

Solution

题意简述:一个\(N \times N\)的\(01\)矩阵,和几种动态操作,包括对子矩阵\((x,y,xx,yy)\)的所有元素异或,查询某一点\((x,y)\)的元素值。

二维树状数组裸题。

异或的操作不难修改。

二维树状数组与一维树状数组不同的是:

  • 一维树状数组维护的是一条链,而二维树状数组维护的却是一片区域。
  • 一维树状数组更新和查找只有一重循环,而二维树状数组需要两重循环。
  • 二维树状数组比一维树状数组多一维度。

经过以上分析,\(AC\)代码就不难得出了。

Code

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <cctype>
  8. using namespace std;
  9. inline int gi()
  10. {
  11. int f = 1, x = 0; char c = getchar();
  12. while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar();}
  13. while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar();}
  14. return f * x;
  15. }
  16. int n, m, c[1003][1003], t;
  17. bool fl = false;
  18. inline void add(int x, int y)//二维树状数组的修改操作
  19. {
  20. for (int i = x; i <= n; i = i + (i & (-i)))
  21. {
  22. for (int j = y; j <= n; j = j + (j & (-j)))//注意两重循环
  23. {
  24. ++c[i][j];
  25. }
  26. }
  27. }
  28. inline int getans(int x, int y)//二维树状数组的查询操作
  29. {
  30. int ans = 0;
  31. for (int i = x; i; i = i - (i & (-i)))
  32. {
  33. for (int j = y; j; j = j - (j & (-j)))
  34. {
  35. ans = ans + c[i][j];//加上答案
  36. }
  37. }
  38. return ans;
  39. }
  40. int main()
  41. {
  42. int t = gi();
  43. while (t--)
  44. {
  45. if (!fl) fl = true;
  46. else puts("");
  47. n = gi(), m = gi();
  48. memset(c, 0, sizeof(c));
  49. while (m--)
  50. {
  51. char c;
  52. cin >> c;
  53. if (c == 'C')
  54. {
  55. int x = gi(), y = gi(), xx = gi(), yy = gi();
  56. add(x, y), add(xx + 1, y), add(x, yy + 1), add(xx + 1, yy + 1);//进行插入操作
  57. }
  58. else
  59. {
  60. int x = gi(), y = gi();
  61. printf("%d\n", getans(x, y) % 2);//输出最终答案
  62. }
  63. }
  64. }
  65. return 0;//结束
  66. }

题解【POJ2155】Matrix的更多相关文章

  1. [LeetCode 题解] Spiral Matrix

    前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...

  2. POJ2155 Matrix 【二维线段树】

    题目链接 POJ2155 题解 二维线段树水题,蒟蒻本想拿来养生一下 数据结构真的是有毒啊,, TM这题卡常 动态开点线段树会TLE[也不知道为什么] 直接开个二维数组反倒能过 #include< ...

  3. POJ2155 Matrix 【二维树状数组】+【段更新点查询】

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17766   Accepted: 6674 Descripti ...

  4. [poj2155]Matrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25004   Accepted: 9261 Descripti ...

  5. [POJ2155]Matrix(二维树状数组)

    题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...

  6. POJ2155:Matrix(二维树状数组,经典)

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

  7. C++题解:Matrix Power Series ——矩阵套矩阵的矩阵加速

    Matrix Power Series r时间限制: 1 Sec 内存限制: 512 MB 题目描述 给定矩阵A,求矩阵S=A^1+A^2+--+A^k,输出矩阵,S矩阵中每个元都要模m. 数据范围: ...

  8. poj----2155 Matrix(二维树状数组第二类)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16950   Accepted: 6369 Descripti ...

  9. 【题解】Matrix BZOJ 4128 矩阵求逆 离散对数 大步小步算法

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4128 大水题一道 使用大步小步算法,把数字的运算换成矩阵的运算就好了 矩阵求逆?这么基础的线 ...

  10. 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 ...

随机推荐

  1. 嵊州D5T2 折纸 folding

    折纸 folding [问题描述] 在非常紧张的 NOIP 考试中,有人喜欢啃指甲,有人喜欢转铅笔,有人喜欢撕 纸条,……而小 x 喜欢迷折纸. 现有一个 W * H 的矩形纸张,监考老师想知道,小 ...

  2. gulp常用插件之gulp-size使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 gulp-size这是一款显示项目的大小插件. 更多使用文档请点击访问gulp-size工具官网. 安装 一键安装不多解释 npm install ...

  3. web做题记录

    2020.1.19 南邮ctf 签到题 题目:key在哪里? 在火狐浏览器中右键选择打开查看源代码,在源代码可以看到如下 因为是第一次做这个题,不知道提交啥,我先提交了“admiaanaaaaaaaa ...

  4. IDEA 找不到包或者找不到符号的一些解决办法

    有时使用IDE导入项目后,启动时会发生找不到包或者找不到符号的情况,下面有一些处理方法 1.右键项目Maven→Reimport 2.IDEA窗口左上角File→Invalidate and Rest ...

  5. Qt Installer Framework翻译(8)

    好了,到这里翻译就结束了.各位可以下载源码,结合examples示例,使用repogen和binarycreator好好实操一下,就能掌握基础用法了.祝各位使用顺利. 官方文档网址:https://d ...

  6. C 库函数 - modf()

    C 库函数 - modf() C 标准库 - <math.h> 描述 C 库函数 double modf(double x, double *integer) 返回值为小数部分(小数点后的 ...

  7. Go-结构体,结构体指针和方法

    https://cloud.tencent.com/developer/article/1482382 4.1.结构体 结构体:讲一个或多个变量组合到一起形成新的类型,这个类型就是结构体,结构体是值类 ...

  8. 链表问题----删除倒数第K个节点

    在单链表和双链表中删除倒数第K个节点 分别实现两个函数,一个可以删除单链表中的倒数第K个节点,一个可以删除双链表中的倒数第k 个节点,要求时间复杂度是 O(N),空间复杂度是 O(1). [解析] 基 ...

  9. Graph Regularized Feature Selection with Data Reconstruction

    Abstract • 从图正则数据重构方面处理无监督特征选择: • 模型的思想是所选特征不仅通过图正则保留了原始数据的局部结构,也通过线性组合重构了每个数据点: • 所以重构误差成为判断所选特征质量的 ...

  10. RN开发-Flex

    1.容器属性        (1). display : flex | inline-flex (块级伸缩容器 | 行内级伸缩容器)        (2). flex-direction : row ...