Matrix

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 17226   Accepted: 6461

Description

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

Input

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.

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
 
题意:给出矩阵左上角和右下角坐标,矩阵里的元素 1变0 ,0 变1,然后给出询问,问某个点是多少
思路:二维树状数组
  1. //2017-10-25
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <iostream>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. const int N = ;
  10.  
  11. int bt[N][N], n, q;
  12.  
  13. int lowbit(int x){
  14. return x&(-x);
  15. }
  16.  
  17. void add(int x, int y, int v){
  18. while(x <= n){
  19. int j = y;
  20. while(j <= n){
  21. bt[x][j] += v;
  22. j += lowbit(j);
  23. }
  24. x += lowbit(x);
  25. }
  26. }
  27.  
  28. int sum(int x, int y){
  29. int sm = ;
  30. while(x > ){
  31. int j = y;
  32. while(j > ){
  33. sm += bt[x][j];
  34. j -= lowbit(j);
  35. }
  36. x -= lowbit(x);
  37. }
  38. return sm;
  39. }
  40.  
  41. int main()
  42. {
  43. int T;
  44. cin>>T;
  45. while(T--){
  46. scanf("%d%d", &n, &q);
  47. memset(bt, , sizeof(bt));
  48. char op;
  49. int x, y, x1, y1;
  50. while(q--){
  51. getchar();
  52. scanf("%c%d%d", &op, &x, &y);
  53. if(op == 'C'){
  54. scanf("%d%d", &x1, &y1);
  55. add(x, y, );
  56. add(x, y1+, -);
  57. add(x1+, y, -);
  58. add(x1+, y1+, );
  59. }else{
  60. printf("%d\n", sum(x, y)%);
  61. }
  62. }if(T)printf("\n");
  63. }
  64.  
  65. return ;
  66. }

POJ2155(二维树状数组)的更多相关文章

  1. poj2155二维树状数组

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

  2. poj2155二维树状数组区间更新

    垃圾poj又交不上题了,也不知道自己写的对不对 /* 给定一个矩阵,初始化为0:两种操作 第一种把一块子矩阵里的值翻转:0->1,1->0 第二种询问某个单元的值 直接累计单元格被覆盖的次 ...

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

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

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

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

  5. 【POJ2155】【二维树状数组】Matrix

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

  6. poj2155一个二维树状数组

                                                                                                         ...

  7. 【poj2155】Matrix(二维树状数组区间更新+单点查询)

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

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

  9. POJ2155/LNSYOJ113 Matrix【二维树状数组+差分】【做题报告】

    这道题是一个二维树状数组,思路十分神奇,其实还是挺水的 题目描述 给定一个N∗NN∗N的矩阵AA,其中矩阵中的元素只有0或者1,其中A[i,j]A[i,j]表示矩阵的第i行和第j列(1≤i,j≤N)( ...

随机推荐

  1. Senparc.Weixin SDK v5.0 升级公告

    经过五年半的持续维护,Senparc.Weixin SDK 逐步丰满和完善,在升级的过程中,我们为基础库(Senparc.Weixin.dll)加入了许多通用的功能,例如加密/解密算法.通用缓存方法等 ...

  2. FineCMS 5.0.10 多个 漏洞详细分析过程

    0x01 前言 已经一个月没有写文章了,最近发生了很多事情,水文一篇.今天的这个CMS是FineCMS,版本是5.0.10版本的几个漏洞分析,从修补漏洞前和修补后的两方面去分析. 文中的evai是特意 ...

  3. [Postman]请求(6)

    您可以从以下位置创建并保存请求: 工作区构建视图 新按钮 启动屏幕 使用新按钮 在标题工具栏中,单击“ 新建”按钮. 出现“新建”屏幕. 在SAVE REQUEST屏幕中: 输入您的请求的标题和说明. ...

  4. PHP环境搭建时缺少php7apache2_4.dll怎么办

    PHP环境搭建时缺少php7apache2_4.dll怎么办 下载的文件有问题! 1.在PHP官网点击Download下载时不管选择哪个版本的都有两个类型  如果需要 php7apache2_4.dl ...

  5. java中根据key获取resource下properties资源文件中对应的参数

    properties资源文件是放在resource目录下的: 新建工具类: package com.demo.utils; import java.io.InputStream; import jav ...

  6. java中Base64的加密工具封装

    Base64加密作为最简单普遍的加密方式(其实只能称为编码方式),应用场景众多比如秘钥,安全证书,也应用在其他的加密方式中或与其他加密方式进行嵌套使用 可以通过引用sun.misc来使用,也可以自己手 ...

  7. 工欲善其事,必先利其器-Python编辑器选择(2)

    前言:工欲善其事.必先利其器 一款顺手的好的编辑器可以让程序员写代码更得心应手,效率也会更高,但是编辑器本身没有好坏,只有使用者使用起来是否顺手而已,这里简单给大家介绍几款常用的可以编辑Python的 ...

  8. [原创]K8Cscan插件之存活主机扫描

    [原创]K8 Cscan 大型内网渗透自定义扫描器 https://www.cnblogs.com/k8gege/p/10519321.html Cscan简介:何为自定义扫描器?其实也是插件化,但C ...

  9. maven的标准pom.xml详解

    maven是构建和管理理项目的利器,pom.xml 是其核心.一个标准的pom.xml该怎么写?其中的标签又有什么意义?话不多说,请看代码: <?xml version="1.0&qu ...

  10. MySQL数据库事务详解

    微信公众号[程序员江湖] 作者黄小斜,斜杠青年,某985硕士,阿里 Java 研发工程师,于 2018 年秋招拿到 BAT 头条.网易.滴滴等 8 个大厂 offer,目前致力于分享这几年的学习经验. ...