1. /*
  2. 二维的树状数组:
  3. 更新某个元素时:
  4. NO.1:c[n1],c[n2],c[n3],....,c[nm];
  5. 当中n1 = i,n(i+1) = ni+lowbit(ni);
  6. nm+lowbit(nm)的值应该大于元素个数N。
  7. NO.2:sum(k)=c[n1]+c[n2]+...+c[nm];
  8. 当中nm=k,n(i-1)=ni-lowbit(ni);
  9. n1-lowbit(n1)的值应该小于0
  10. ----------------------------------------------------------------------
  11. 用二维的c[i][j]存储长条的和。。。、
  12. ---------------------------------------
  13. 元素的值加一个数:
  14. void update(int x, int y, int a)
  15. {
  16. for(int i=x; i<=s; i+=lowbit(i))
  17. {
  18. for(int j=y; j<=s; j+=lowbit(j))
  19. c[i][j] += a;
  20. }
  21. }
  22. ---------------------------------------------------
  23. int sum(int x, int y)矩阵0到x和0到y的元素和
  24. {
  25. int ret = 0;
  26. for(int i=x; i>0; i-=lowbit(i))
  27. {
  28. for(int j=y; j>0; j-=lowbit(j))
  29. ret += c[i][j];
  30. }
  31. return ret;
  32. }
  33. ------------------------------------------------
  34. */
  35. #include <iostream>
  36. #include <cstdio>
  37. #include <cstring>
  38. #define INF 0x3f3f3f3f
  39.  
  40. using namespace std;
  41.  
  42. int op,s;
  43. int x,y,a;
  44. int l,b,r,t;
  45. int c[1050][1050];
  46.  
  47. int lowbit(int x)
  48. {
  49. return x & (-x);
  50. }
  51.  
  52. void update(int x, int y, int a)
  53. {
  54. for(int i=x; i<=s; i+=lowbit(i))
  55. {
  56. for(int j=y; j<=s; j+=lowbit(j))
  57. c[i][j] += a;
  58. }
  59. }
  60.  
  61. int sum(int x, int y)
  62. {
  63. int ret = 0;
  64. for(int i=x; i>0; i-=lowbit(i))
  65. {
  66. for(int j=y; j>0; j-=lowbit(j))
  67. ret += c[i][j];
  68. }
  69. return ret;
  70. }
  71.  
  72. int main()
  73. {
  74. //freopen("input.txt","r",stdin);
  75. while(1)
  76. {
  77. scanf("%d",&op);
  78. if(op == 0)
  79. {
  80. scanf("%d",&s);
  81. memset(c, 0, sizeof(c));
  82. }
  83. if(op == 1)
  84. {
  85. scanf("%d%d%d",&x,&y,&a);
  86. update(x+1, y+1, a);
  87. }
  88. if(op == 2)
  89. {
  90. scanf("%d%d%d%d",&l, &b,&r,&t);
  91. l++;
  92. b++;
  93. r++;
  94. t++;
  95. printf("%d\n",sum(r, t) - sum(r, b-1) - sum(l-1, t) + sum(l-1, b-1) );
  96. }
  97. if(op == 3)
  98. return 0;
  99. }
  100. }

----------------------------------

快。。。。。

【树转数组】poj1195的更多相关文章

  1. 二维树状数组poj1195

    题目链接:https://vjudge.net/problem/POJ-1195 题意:一开始输入0和一个s,0代表开始,s代表这是一个s*s的图,接下来会输入1或2,1代表进行单点修改,后面会接3个 ...

  2. poj1195(二维树状数组)

    题目链接:https://vjudge.net/problem/POJ-1195 题意:有s*s的矩阵,初始化为全0,有两种操作,单点修改(x,y)的值,区间查询(x,y)的值(l<=x< ...

  3. POJ-1195 Mobile phones---裸的二维树状数组(注意下标从1,1开始)

    题目链接: https://vjudge.net/problem/POJ-1195 题目大意: 直接维护二维树状数组 注意横纵坐标全部需要加1,因为树状数组从(1,1)开始 #include<c ...

  4. 二维树状数组(水题) POJ1195

    前段时间遇到线段树过不了,树状数组却过了的题.(其实线段树过得了的) 回忆了下树状数组. 主要原理,还是二进制位数,每一项的和表示其为它的前((最后一位1及其后)的二进制数)和,可从二进制图来看.(用 ...

  5. 【POJ1195】【二维树状数组】Mobile phones

    Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...

  6. poj1195二维树状数组模板

    二维树状数组和一维的也差不多,改一下add和query函数即可:即按行修改,行内单点修改即可 /* 二维树状数组,询问一个二维区间内的数之和 */ #include<iostream> # ...

  7. poj-1195(二维树状数组)

    题目链接:传送门 题意:给出操作,按照操作进行. 思路:将树状数组设置为二维的就行了. 注意: (1)每次求出的面积是S(x2,y2)-S(x1-1,y2)-S(x2,y1-1)+S(x1-1,y1- ...

  8. 【poj1195】Mobile phones(二维树状数组)

    题目链接:http://poj.org/problem?id=1195 [题意] 给出一个全0的矩阵,然后一些操作 0 S:初始化矩阵,维数是S*S,值全为0,这个操作只有最开始出现一次 1 X Y ...

  9. POJ1195(二维树状数组)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 17176   Accepted: 7920 De ...

随机推荐

  1. Emmet超详细教程

    Emmet超详细教程 一.总结 一句话总结:用的时候照着用,能提高效率. 1.快捷键如何使用? 需要敲代码的时候把快捷键放到旁边即可.照着敲. 二.Emmet超详细教程 Emmet的前身是大名鼎鼎的Z ...

  2. thinkphp 3.2,引入第三方类库的粗略记录

    首先用第三方类库我是放到vendor里面的 根目录\ThinkPHP\Library\Vendor\Wxphp 例如创建了一个Wxphp文件夹,里面有个php文件叫做     zll.php    文 ...

  3. [Recompose] Transform Props using Recompose --mapProps

    Learn how to use the 'mapProps' higher-order component to modify an existing component’s API (its pr ...

  4. C#生成6位随机验证码

    private string VerifyCode() { Random random = new Random(); , ).ToString(); }

  5. [Linux] Use find to search for filename patterns

    Learn how to use find to identify filenames matching specified patterns. We'll use find to identify ...

  6. [Compose] 12. Two rules about Funtors

    We learn the formal definition of a functor and look at the laws they obey. Any Functor should follo ...

  7. Java的面向AOP编程

    一. 引言 AOP(Aspect-Oriented Programming,面向切面的编程),是一种新型的编程范式,主张关注软件流程中的一个切面,将相同功能的代码整合打包在一起,减少系统的耦合性,增强 ...

  8. Android 输入框弹出样式

    在androidMainfest.xml文件里 在Activity中设置 [A]stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置 [B]stateU ...

  9. [GraphQL] Add an Interface to a GraphQL Schema

    As we start building out more complex GraphQL schemas, certain fields start to repeat across differe ...

  10. NOIP模拟 赌博游戏 - 概率dp

    题意: 最近西雅图的高中校园里流行这样一个游戏. 我们有一个骰子,这个骰子有M个面,分别写着1..M,并且是个公平的骰子,换句话说,一次投掷时每个面朝上的概率是相同的. 游戏的组织者使用这个骰子进行N ...