Description

Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix.

Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area.

 
  二维树状数组的裸题,不过要注意坐标要加一,因为从0开始。
 
代码如下:
  1. // ━━━━━━神兽出没━━━━━━
  2. //    ┏┓ ┏┓
  3. //   ┏┛┻━━━━━━━┛┻┓
  4. //   ┃ ┃
  5. //   ┃ ━ ┃
  6. // ████━████ ┃
  7. //   ┃ ┃
  8. //   ┃ ┻ ┃
  9. //   ┃ ┃
  10. //   ┗━┓ ┏━┛
  11. //    ┃ ┃
  12. //    ┃ ┃
  13. //    ┃ ┗━━━┓
  14. //    ┃ ┣┓
  15. //    ┃ ┏┛
  16. //    ┗┓┓┏━━━━━┳┓┏┛
  17. //     ┃┫┫ ┃┫┫
  18. //     ┗┻┛ ┗┻┛
  19. //
  20. // ━━━━━━感觉萌萌哒━━━━━━
  21.  
  22. // Author : WhyWhy
  23. // Created Time : 2015年07月17日 星期五 14时44分13秒
  24. // File Name : 1195.cpp
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <iostream>
  29. #include <algorithm>
  30. #include <vector>
  31. #include <queue>
  32. #include <set>
  33. #include <map>
  34. #include <string>
  35. #include <math.h>
  36. #include <stdlib.h>
  37. #include <time.h>
  38.  
  39. using namespace std;
  40.  
  41. const int MaxN=;
  42.  
  43. int C[MaxN][MaxN];
  44. int N;
  45.  
  46. inline int lowbit(int x)
  47. {
  48. return x&(-x);
  49. }
  50.  
  51. void add(int x,int y,int d)
  52. {
  53. int t;
  54.  
  55. while(x<=N)
  56. {
  57. t=y;
  58.  
  59. while(t<=N)
  60. {
  61. C[x][t]+=d;
  62. t+=lowbit(t);
  63. }
  64.  
  65. x+=lowbit(x);
  66. }
  67. }
  68.  
  69. int query(int x,int y)
  70. {
  71. int ret=;
  72. int t;
  73.  
  74. while(x>)
  75. {
  76. t=y;
  77.  
  78. while(t>)
  79. {
  80. ret+=C[x][t];
  81. t-=lowbit(t);
  82. }
  83.  
  84. x-=lowbit(x);
  85. }
  86.  
  87. return ret;
  88. }
  89.  
  90. int main()
  91. {
  92. //freopen("in.txt","r",stdin);
  93. //freopen("out.txt","w",stdout);
  94.  
  95. int a,b,c,d,e;
  96.  
  97. while()
  98. {
  99. scanf("%d",&a);
  100.  
  101. if(a==)
  102. {
  103. scanf("%d %d %d",&b,&c,&d);
  104. add(b+,c+,d);
  105. }
  106. else if(a==)
  107. {
  108. scanf("%d %d %d %d",&b,&c,&d,&e);
  109. printf("%d\n",query(d+,e+)-query(d+,c)-query(b,e+)+query(b,c));
  110. }
  111. else if(a==)
  112. {
  113. scanf("%d",&N);
  114. memset(C,,sizeof(C));
  115. }
  116. else
  117. break;
  118. }
  119.  
  120. return ;
  121. }

(简单) POJ 1195 Mobile phones,二维树状数组。的更多相关文章

  1. poj 1195 Mobile phones(二维树状数组)

    树状数组支持两种操作: Add(x, d)操作:   让a[x]增加d. Query(L,R): 计算 a[L]+a[L+1]……a[R]. 当要频繁的对数组元素进行修改,同时又要频繁的查询数组内任一 ...

  2. POJ 1195:Mobile phones 二维树状数组

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16893   Accepted: 7789 De ...

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

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

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

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

  5. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

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

  6. POJ 2155 Matrix (二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17224   Accepted: 6460 Descripti ...

  7. POJ 2155 Matrix 【二维树状数组】(二维单点查询经典题)

    <题目链接> 题目大意: 给出一个初始值全为0的矩阵,对其进行两个操作. 1.给出一个子矩阵的左上角和右上角坐标,这两个坐标所代表的矩阵内0变成1,1变成0. 2.查询某个坐标的点的值. ...

  8. POJ 2155 Matrix (二维树状数组)题解

    思路: 没想到二维树状数组和一维的比只差了一行,update单点更新,query求和 这里的函数用法和平时不一样,query直接算出来就是某点的值,怎么做到的呢? 我们在更新的时候不止更新一个点,而是 ...

  9. POJ 2155:Matrix 二维树状数组

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21757   Accepted: 8141 Descripti ...

  10. POJ 2155 Matrix(二维树状数组)

    与以往不同的是,这个树状数组是二维的,仅此而已 #include <iostream> #include <cstdio> #include <cstring> # ...

随机推荐

  1. 详细的SQL中datediff用法

    DATEDIFF 函数 [日期和时间] 功能返回两个日期之间的间隔. 语法DATEDIFF ( date-part, date-expression-1, date-expression-2 ) da ...

  2. js冒泡事件小解

    何为冒泡事件?简单来说事件就像一个水里的泡泡,先触发当前对象再触发其父元素,然后是父元素的父元素... eg: <div class="out" onclick= " ...

  3. 【floyd 多源最短路】 poj 1125

    #include <stdio.h> #include <iostream> #include <memory.h> using namespace std; ][ ...

  4. OpenGL———混合的基本知识

    混合是一种常用的技巧,通常可以用来实现半透明.但其实它也是十分灵活的,你可以通过不同的设置得到不同的混合结果,产生一些有趣或者奇怪的图象.混合是什么呢?混合就是把两种颜色混在一起.具体一点,就是把某一 ...

  5. [转]java构造方法的访问修饰符

    http://my.oschina.net/u/1464678/blog/210359 1.       类(class) 可见性修饰符: public—在所有类中可见,在其他包中可以用import导 ...

  6. 2016湖南省赛--A题--2016

    2016 [TOC] Description 给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量: 1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. Input ...

  7. SAX,DOM,JAXP,JDOM,DOM4J比较

    dom,sax,jdom,dom4j的技术特点: 1: DOMDOM 是用与平台和语言无关的方式表示 XML 文档的官方 W3C 标准.DOM 是以层次结构组织的节点或信息片断的集合.这个层次结构允许 ...

  8. 学习笔记——单例模式Singleton

    单例模式,很容易理解,就它一个. 比如网络请求服务类WebReq.它自己生成请求线程,并管理请求数据的返回,所以我们使用它进行网络请求时,不用每次都new一个,只需要使用一个实例就行了.WebReq实 ...

  9. mysql数据库主从备份

    近期实验室总是不给通知的就停电,导致我们在不停的恢复服务.在某一个断电的过程中,发现我们的项目管理工具redmine的硬盘挂掉了..因为是部署在虚拟机上的,也没做冗余,数据就丢了..于是反思,我们的m ...

  10. dage手法之 头部和banner ad tpl_header

    <div class="top2"> <?php if ($current_page_base == 'index' || $current_page_base ...