Matrix
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 33682   Accepted: 12194

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<cstdio>
  3. #include<cstring>
  4. #include<string>
  5.  
  6. using namespace std;
  7. const int N=1e3+;
  8. int s[N][N];
  9. int n;
  10. int lowbit(int x)
  11. {
  12. return x&(-x);
  13. }
  14. void updata(int x,int y,int z)
  15. {
  16. for(int i=x;i<=n;i+=lowbit(i)){
  17. for(int j=y;j<=n;j+=lowbit(j)){
  18. s[i][j]+=z;
  19. }
  20. }
  21. }
  22.  
  23. int sum(int x,int y)
  24. {
  25. int res=;
  26. for(int i=x;i>;i-=lowbit(i)){
  27. for(int j=y;j>;j-=lowbit(j)){
  28. res+=s[i][j];
  29. }
  30. }
  31. return res;
  32. }
  33. int main()
  34. {
  35. int T;
  36. scanf("%d",&T);
  37. while(T--){
  38. memset(s,,sizeof(s));
  39. int m;
  40. scanf("%d %d",&n,&m);
  41. while(m--){
  42. char t[];
  43. scanf("%s",t);
  44. if(t[]=='C'){
  45. int x1,y1,x2,y2;
  46. scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
  47. updata(x1,y1,);
  48. updata(x2+,y2+,);
  49. updata(x2+,y1,-);
  50. updata(x1,y2+,-);
  51. }
  52. else{
  53. int x,y;
  54. scanf("%d %d",&x,&y);
  55. printf("%d\n",sum(x,y)%);
  56. }
  57. }
  58. if(T) printf("\n");
  59. }
  60.  
  61. return ;
  62. }

poj 2155 (二维树状数组 区间修改 求某点值)的更多相关文章

  1. 【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询

    题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不 ...

  2. 【bzoj3132】上帝造题的七分钟 二维树状数组区间修改区间查询

    题目描述 “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的操作. ...

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

  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. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  6. 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?

    开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...

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

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

  8. POJ 1195 二维树状数组

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 18489   Accepted: 8558 De ...

  9. poj 2029 二维树状数组

    思路:简单树状数组 #include<map> #include<set> #include<cmath> #include<queue> #inclu ...

随机推荐

  1. Java中的IO流(二)

    上一篇<Java中的IO流(一)>把学习IO流的字符流作了一下记录,本篇把字节流记录一下. 一,Java中的字节流 Java中的字节流的操作方式与字符流的操作方式大致相同,连方法名都是类似 ...

  2. Oracle作业5——多表查询、子查询

    一.基础练习: 1.查询和scott相同部门的员工姓名ename和雇用日期hiredate SELECT ENAME,HIREDATE FROM EMP WHERE DEPTNO=(SELECT DE ...

  3. 【模板】RMQ(计算区间最值)

    ①一维RMQ (1) dp[i,j] 表示从第i个数起连续2j个数中的(最大值min.最小值max.最大公约数gcd……),通过更改下列代码中的红色函数即可实现. (2) b数组放置所需查询的数列. ...

  4. .Net core 使用Swagger

    接触到项目的时候,用了很久的Swagger,发现Swagger真的非常好用,不但方便了调试Web Api,还生成了Api 文档,真是非常的好用啊. 然后我想搞懂到底如何使用Swagger,所以自己建了 ...

  5. MYSQL命令简要笔记

    mysqldump "C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe"    --host=localhost ...

  6. Linux-帮助的用法

    Linux帮助使用方法 内部命令:help COMMAND 或 man bash外部命令: (1) COMMAND --help   COMMAND -h --help和-h选项显示用法总结和参数列表 ...

  7. Python入门 —— 01简介

    Python 历史 python 的创始人为荷兰人吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为 ...

  8. 数据库5.7-jdbc版本8.0.12驱动连接

    现在版本的jdbc连接方式和原来不一样了, 假如你使用String driver = "com.mysql.jdbc.Driver"; 会抛出错误: Loading class ` ...

  9. PHP中计算字符串相似度的函数代码

    similar_text — 计算两个字符串的相似度 int similar_text ( string $first , string $second [, float &$percent ...

  10. 20190118-自定义实现replace方法

    1.自定义实现replace方法 Python replace() 方法把字符串中的 old(旧字符串) 替换成 neange(新字符串),如果指定第三个参数max,则替换不超过 max 次.考虑ol ...