附一篇经典翻译,学习 树状数组  http://www.hawstein.com/posts/binary-indexed-trees.html

  1. /**
  2. * @author johnsondu
  3. * @time 2015-8-22
  4. * @type 2D Binary Index Tree
  5. * @strategy 如果翻转的是(x1,y1), (x2,y2)区域。则相当于
  6. * 翻转(0, 0)~(x2, y2), 然后再翻转(0,0)~(x1-1, y2)
  7. * (0, 0)~(x2, y1-1), (0, 0)~(x1-1, y1-1);
  8. * 由于翻转两次等于没有翻转。
  9.  
  10. 此处类比容斥原理
  11. * @url http://poj.org/problem?id=2155
  12. */
  13.  
  14. #include <iostream>
  15. #include <cstdio>
  16. #include <cmath>
  17. #include <algorithm>
  18. #include <cstring>
  19. #include <string>
  20. #include <stack>
  21. #include <queue>
  22. #include <map>
  23. #include <vector>
  24.  
  25. using namespace std;
  26.  
  27. const int N = 1005;
  28. int c[N][N], n, q;
  29.  
  30. int lowbit(int x)
  31. {
  32. return (x & (-x));
  33. }
  34.  
  35. void update(int x, int y)
  36. {
  37. for(int i = x; i <= n; i += lowbit(i))
  38. for(int j = y; j <= n; j += lowbit(j))
  39. c[i][j] ++;
  40. }
  41.  
  42. int query(int x, int y)
  43. {
  44. int sum = 0;
  45. for(int i = x; i > 0; i -= lowbit(i))
  46. for(int j = y; j > 0; j -= lowbit(j))
  47. sum += c[i][j];
  48. return sum;
  49. }
  50.  
  51. int main()
  52. {
  53. int tcase;
  54. int x1, y1, x2, y2;
  55. scanf("%d", &tcase) ;
  56. while(tcase --) {
  57. memset(c, 0, sizeof(c));
  58. scanf("%d%d", &n, &q);
  59. for(int i = 0; i < q; i ++)
  60. {
  61. char command[5];
  62. scanf("%s", command);
  63. if(command[0] == 'C')
  64. {
  65. scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  66. x1 ++; y1 ++; x2 ++; y2 ++;
  67. update(x2, y2);
  68. update(x1 - 1, y1 - 1);
  69. update(x1 - 1, y2);
  70. update(x2, y1 - 1);
  71. }
  72. else
  73. {
  74. scanf("%d%d", &x1, &y1);
  75. printf("%d\n", query(x1, y1) & 1);
  76. }
  77. }
  78. printf("\n");
  79.  
  80. }
  81.  
  82. return 0;
  83. }

【树状数组】POJ 2155 Matrix的更多相关文章

  1. 线段树/树状数组 POJ 2182 Lost Cows

    题目传送门 题意:n头牛,1~n的id给它们乱序编号,已知每头牛前面有多少头牛的编号是比它小的,求原来乱序的编号 分析:从后往前考虑,最后一头牛a[i] = 0,那么它的编号为第a[i] + 1编号: ...

  2. 树状数组 || POJ 2352 Stars

    Astronomers often examine star maps where stars are represented by points on a plane and each star h ...

  3. 树状数组 || POJ 3321 Apple Tree

    一道dfs序+树状数组的题 因为并没有get到dfs序以及对树状数组也不熟练卡了很久orz dfs序: in和out是时间戳 dfs序可以将树转化成为一个序列,满足区间 -> 子树 然后就可以用 ...

  4. LCA+树状数组 POJ 2763 Housewife Wind

    题目传送门 题意:两种操作,问u到v的距离,并且u走到了v:把第i条边距离改成w 分析:根据DFS访问顺序,将树处理成链状的,那么回边处理成负权值,那么LCA加上BIT能够知道u到v的距离,BIT存储 ...

  5. 树状数组 POJ 2481 Cows

    题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...

  6. POJ 2155 Matrix【二维树状数组+YY(区间计数)】

    题目链接:http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissio ...

  7. poj 2155 Matrix (树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16797   Accepted: 6312 Descripti ...

  8. POJ 2155 Matrix(树状数组+容斥原理)

    [题目链接] http://poj.org/problem?id=2155 [题目大意] 要求维护两个操作,矩阵翻转和单点查询 [题解] 树状数组可以处理前缀和问题,前缀之间进行容斥即可得到答案. [ ...

  9. POJ 2155 Matrix(二维树状数组,绝对具体)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20599   Accepted: 7673 Descripti ...

随机推荐

  1. JavaScript--编程题

    某班的成绩出来了,现在老师要把班级的成绩打印出来. 效果图: XXXX年XX月X日 星期X--班级总分为:81 格式要求: 1.显示打印的日期. 格式为类似“XXXX年XX月XX日 星期X” 的当前的 ...

  2. Offer收割_5

    训练 投入 欲望.  ---贾森博尔特 第一题:二分枚举答案,check时候模拟一下即可. 时间复杂度: O(n*logn). 第二题: 描述 小Hi在虚拟世界中有一只小宠物小P.小P有K种属性,每种 ...

  3. Hadoop Hive概念学习系列之hive与依赖环境的交互(二十一)

    hive与环境的交互,算是一个小知识点,但掌握不菲! 如何在hive里,也达到这样呢? 不需要这样啦,因为,hive是建立在hadoop之上,启动hive,相当于,就是,hadoop jar ** h ...

  4. Java NIO 聊天室实例

    最近写了个Java NIO聊天室聊天的程序,NIO学习起来比较困难的,我的代码能给大家起到一个抛砖引玉的作用! 服务端: package test.javanio; /** * @author * @ ...

  5. Android studio 中R.menu的创建

    对于Android开发中的menu没有声明的情况: 首先,将鼠标定位到红色的menu上面, 然后,Alt+enter组合键,建立文件menu, 然后将以下代码复制进去: <item androi ...

  6. Ajax——跨域访问

    同源 基本概念:同源策略是浏览器的一种安全策略,所谓同源是指,域名,协议,端口完全相同. //同一域名下,允许通讯 http://www.a.com/a.js http://www.a.com/b.j ...

  7. JS——回调函数

    1.回调函数作为参数的形式进行使用 2.回调函数一定程度上达到了解耦效果(模块化.功能化) <script> console.log(op(1, 2, add)); console.log ...

  8. Android Studio 快捷键整理

    Alt+回车 导入包,自动修正 Ctrl+N   查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L  格式化代码Ctrl+Alt+O 优化导入的类和包Alt+Insert 生成代码(如ge ...

  9. 预处理、const、static、sizeof

    1.预处理和宏定义 #define xxxx #ifdef xxxx ; #elseif xxxx; #endif 2.c++求随机数 rand(),rand()会返回一随机数值, 范围在0至RAND ...

  10. CAD增加一个有形的线型(网页版)

    主要用到函数说明: _DMxDrawX::AddTextStyle1 向数据库中增加一个文字样式.详细说明如下: 参数 说明 BSTR pszName 文字样式名称 BSTR pszFileName ...