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. 

Input

The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter
integers according to the following table. 




The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and
0 <= Y <= 3. 



Table size: 1 * 1 <= S * S <= 1024 * 1024 

Cell value V at any time: 0 <= V <= 32767 

Update amount: -32768 <= A <= 32767 

No of instructions in input: 3 <= U <= 60002 

Maximum number of phones in the whole table: M= 2^30 

Output

Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.

Sample Input

  1. 0 4
  2. 1 1 2 3
  3. 2 0 0 2 2
  4. 1 1 1 2
  5. 1 1 2 -1
  6. 2 1 1 2 3
  7. 3

Sample Output

  1. 3
  2. 4

二维树状数组求和与改动,从一维能够扩展到二维。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <set>
  6. #include <stack>
  7. #include <cctype>
  8. #include <algorithm>
  9. #define lson o<<1, l, m
  10. #define rson o<<1|1, m+1, r
  11. using namespace std;
  12. typedef long long LL;
  13. const int mod = 99999997;
  14. const int MAX = 1000000000;
  15. const int maxn = 100005;
  16. int ca, s, op;
  17. int c[1100][1100];
  18. void add(int i, int j, int v) {
  19. while(i <= s) {
  20. int y = j;
  21. while(y <= s) {
  22. c[i][y] += v;
  23. y += y&-y;
  24. }
  25. i += i&-i;
  26. }
  27. }
  28. int query(int i ,int j) {
  29. int ans = 0;
  30. while(i > 0) {
  31. int y = j;
  32. while(y > 0) {
  33. ans += c[i][y];
  34. y -= y&-y;
  35. }
  36. i -= i&-i;
  37. }
  38. return ans;
  39. }
  40. int main()
  41. {
  42. cin >> ca >> s;
  43. while(1) {
  44. scanf("%d", &op);
  45. if(op == 1) {
  46. int a, b, v;
  47. scanf("%d%d%d", &a, &b, &v);
  48. add(a+1, b+1, v);
  49. } else if(op == 2) {
  50. int x1, x2, y1, y2;
  51. scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
  52. int ans = query(x2+1, y2+1) + query(x1, y1) - query(x2+1, y1) - query(x1, y2+1);
  53. printf("%d\n", ans);
  54. } else break;
  55. }
  56. return 0;
  57. }



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. window下golang使用gRPC入门案例&net core客户端

    gRPC是google开源高性能分布式RPC框架,支持http/2 双向数据流传输及Protobuff,可以在任何环境下运行. 它可以有效地将数据中心内和跨数据中心的服务与可插拔支持进行负载均衡,跟踪 ...

  2. HTML-loading动画1

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. est6 -- Object.is()、Object.assign()、Object.defineProperty()、Symbol、Proxy

    Object.is()用来比较两个值是否严格相等.它与严格比较运算符(===)的行为基本一致,不同之处只有两个:一是+0不等于-0,二是NaN等于自身. + === - //true NaN === ...

  4. POI2004

    11th Polish Olympiad in Informatics(POI2004) <br > 填坑计划第二弹......把这个没填完的坑搬过来啦~ 上次勉强填完NEERC的坑... ...

  5. 某考试 T2 bomb

    轰炸(bomb)[题目描述]有n座城市,城市之间建立了m条有向的地下通道.你需要发起若干轮轰炸,每轮可以轰炸任意多个城市.但每次轰炸的城市中,不能存在两个不同的城市i,j满足可以通过地道从城市i到达城 ...

  6. Nessus虚拟机的几个问题解决办法

    1.使用ppp的校园网或者家庭宽带无法通过桥接上网. 这时要把这俩网卡变成NAT模式就行. 2.国外下载插件包(或者过慢). 我这里贡献个高速链接.base64,懂得自然懂. c3NyOi8vTkRj ...

  7. Ext 上传文件

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title> ...

  8. android studio C/C++ jni 编写以及调试方法

    原文路径: http://blog.sina.com.cn/s/blog_ad64b8200102vnxl.html 目录 开发环境 2 编写hello_jni程序 2 运行结果 10 调试程序 10 ...

  9. OpenGL step to step(1)

    在窗体上绘制一个矩形,just a demo #include <GLUT/GLUT.h> void init() { glClearColor(0.0,0.0,0.0,0.0); glS ...

  10. Access自定义函数(人民币大写)

    人民币大写函数:整数不超过13位. Public Function 人民币大写(A) As String Dim aa As String Dim bb As String Dim cc As Str ...