Matrix

Time Limit: 3000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6. #define max 1005
  7. int c[max][max];
  8. int n;
  9.  
  10. int lowbit(int x)
  11. {
  12. return x&-x;
  13. }
  14.  
  15. void update(int x,int y,int val)
  16. {
  17. for(int i=x; i<=n; i+=lowbit(i))
  18. {
  19. for(int j=y; j<=n; j+=lowbit(j))
  20. {
  21. c[i][j]+=val;
  22. }
  23. }
  24. }
  25.  
  26. int get_sum(int x,int y)
  27. {
  28. int s=;
  29. for(int i=x; i>; i-=lowbit(i))
  30. {
  31. for(int j=y; j>; j-=lowbit(j))
  32. {
  33. s+=c[i][j];
  34. }
  35. }
  36. return s;
  37. }
  38.  
  39. int main()
  40. {
  41. /// freopen("in.txt","r",stdin);
  42. ///freopen("out.txt","w",stdout);
  43. int cc,t,x1,x2,y1,y2,a,b;
  44. char ch;
  45. scanf("%d",&cc);
  46. for(int i=;i<=cc;i++)
  47. {
  48. memset(c,,sizeof(c));
  49. scanf("%d%d",&n,&t);
  50. getchar();
  51. for(int j=;j<t;j++) ///下标不可以从0开始 会死循环
  52. {
  53. scanf("%c",&ch);
  54. if(ch=='C')
  55. {
  56. scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
  57. getchar();
  58. update(x1,y1,);
  59. update(x1,y2+,);
  60. update(x2+,y1,);
  61. update(x2+,y2+,);
  62. }
  63. else
  64. {
  65. scanf("%d%d",&a,&b);
  66. getchar();
  67. printf("%d\n",get_sum(a,b)%);
  68. }
  69. }
  70. printf("\n");
  71. }
  72. return ;
  73. }

* 矩阵的00在左上角

http://blog.csdn.net/zxy_snow/article/details/6264135   图示

poj 2155 区间更新 单点查询的更多相关文章

  1. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  2. codevs 1081 线段树练习 2 区间更新 单点查询 无lazy

    题目描述 Description 给你N个数,有两种操作 1:给区间[a,b]的所有数都增加X 2:询问第i个数是什么? 输入描述 Input Description 第一行一个正整数n,接下来n行n ...

  3. CDOJ 1059 秋实大哥与小朋友 STL(set)+离散化+BIT区间更新单点查询

    链接: A - 秋实大哥与小朋友 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%lld & %llu Subm ...

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

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

  5. NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)

    Problem 1050: Just Go Time Limits:  3000 MS   Memory Limits:  65536 KB 64-bit interger IO format:  % ...

  6. HDU 5861 Road 线段树区间更新单点查询

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5861 Road Time Limit: 12000/6000 MS (Java/Othe ...

  7. ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】

    任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...

  8. hdu1556 树状数组区间更新单点查询板子

    就是裸的区间更新: 相对于直观的线段树的区间更新,树状数组的区间更新原理不太相同:由于数组中的一个结点控制的是一块区间,当遇到更新[l,r]时,先将所有能控制到 l 的结点给更新了,这样一来就是一下子 ...

  9. hdu3966 树链剖分点权模板+线段树区间更新/树状数组区间更新单点查询

    点权树的模板题,另外发现树状数组也是可以区间更新的.. 注意在对链进行操作时方向不要搞错 线段树版本 #include<bits/stdc++.h> using namespace std ...

随机推荐

  1. Fedora : multilib version problems found

    摘自:https://smjrifle.net/fedora-fix-multilib-version-problems/ This error was due to duplicate packag ...

  2. web前端学习笔记:文本属性

    今天的web前端笔记主要讲述文本属性,希望能帮助到正在学习web前端开发的初学者们,废话不多说了,一起来看看文本属性的相关内容吧. 文本属性 文本缩进 将Web页面上的一个段落第一行缩进,这是一种最常 ...

  3. 担心后端代码泄露?用delphi做后端,模板扣出来,随时可以变化。

    担心后端代码泄露?用delphi做后端,模板扣出来,随时可以变化. 本项目不是intraweb, unigui等类似的拖拉项目,只是一个简单 的模板引擎,理论上可以结合任何后端. 要就下载源码,作者保 ...

  4. insert执行错误,怎么样获取具体的错误原因

    1.开启debug 2.去runtime里面去找最后执行的SQL

  5. [Robot Framework] 动态等待,提供默认的等待时间,等待时间可传可不传

    默认10s

  6. 论坛:排序 >>case..when..then ..end的妙用

    a.主题列表按 最后更新时间 进行排序 数据库SQL语句中没有if..else的判断语句,但是oracle中有decode()函数可以实现这种判断语句,但是还可以用case..when..then . ...

  7. go语言字符串练习

    package main import "fmt" import s"strings" var p = fmt.Println func main() { p( ...

  8. 用php把access数据库导入到mysql

    <?php header("content-Type: text/html; charset=utf-8"); /// ///把access数据库转换成mysql的SQL语句 ...

  9. linux中命令突然不能用

    先用:echo $PATH 发现path丢失:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 于是用临时环境变量 export ...

  10. 2019.01.22 zoj3583 Simple Path(并查集+枚举)

    传送门 题意简述:给出一张图问不在从sss到ttt所有简单路径上的点数. 思路: 枚举删去每个点然后把整张图用并查集处理一下,同时不跟sss和ttt在同一个连通块的点就是满足要求的点(被删去的不算). ...