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
    解题思路:
    二维树状数组,直接用一维板子改成二维就行了,要注意的是询问区间的时候两个区间的差值并不能直接减,而且还要考虑边界的问题。
    所以最后两个区间的差值由 (x2,y2)-(x1-1,y2)-(x2,y1-1)+(x1-1,x2-1);
    数组是从0开始的,坐标必须加一
    实现代码:
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. #define lowbit(x) ((x)&(-(x)))
  6. const int N = ;
  7. int m;
  8. int c[N][N];
  9. inline void update(int x,int y,int date){
  10. for(int i=x;i<=m;i+=lowbit(i))
  11. for(int j=y;j<=m;j+=lowbit(j))
  12. c[i][j] += date;
  13. }
  14.  
  15. inline int sum(int x,int y){
  16. int ans = ;
  17. for(int i=x;i>;i-=lowbit(i))
  18. for(int j=y;j>;j-=lowbit(j))
  19. ans += c[i][j];
  20. return ans;
  21. }
  22. int main()
  23. {
  24. int n,k,x,y,d,x1,x2,y1,y2;
  25. while(scanf("%d%d",&n,&m)!=EOF){
  26. memset(c,,sizeof(c));
  27. while(scanf("%d",&k)){
  28. if(k==){
  29. scanf("%d%d%d",&x,&y,&d);
  30. x++;y++;
  31. update(x,y,d);
  32. //cout<<sum(x,y)<<endl;
  33. }
  34. else if(k==){
  35. scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
  36. x1++;y1++;x2++;y2++;
  37. //cout<<sum(x2,y2)<<endl;
  38. printf("%d\n",sum(x2,y2)-sum(x1-,y2)-sum(x2,y1-)+sum(x1-,y1-));
  39. }
  40. else
  41. break;
  42. }
  43. }
  44. return ;
  45. }

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 【二维线段树】

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14291   Accepted: 6644 De ...

随机推荐

  1. Mysql windows版本的安装

    一.mysql官网下载 下载安装包 MySQL Community Server (GPL)--> 选用zip版本的 二.安装 解压mysql的安装包. 将bin目录配置到环境变量中.(即环境变 ...

  2. 常见camera测试卡

    常见camera测试卡     版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/luckywang1103/article/details/87166 ...

  3. Luogu3162 CQOI2012 组装 贪心

    传送门 如果提供每一种零件的生产车间固定了,那么总时间\(t\)与组装车间的位置\(x\)的关系就是 \(t = \sum (x-a_i)^2 = nx^2-2\sum a_ix + \sum a_i ...

  4. 【php增删改查实例】第十七节 - 用户登录(1)

    新建一个login文件,里面存放的就是用户登录的模块. <html> <head> <meta charset="utf-8"> <sty ...

  5. Jlink使用技巧之虚拟串口功能

    前言 串口调试是单片机开发过程必不可少的一个功能,一般是使用一个UART-TTL的串口模块来实现串口的功能,其实下载调试使用的Jlink仿真器也可以实现串口调试的功能,本篇文章将介绍如何使用Jlink ...

  6. Express4.x API (三):Response (译)

    Express4.x API 译文 系列文章 Express4.x API (一):application (译) -- 完成 Express4.x API (二):request (译) -- 完成 ...

  7. Nagios监控系统部署(源码)

    1. 概述2. 部署Nagios2.1 创建Nagios用户组2.2 下载Nagios和Nagios-plugin源码2.3 编译安装3. 部署Nagios-plugin3.1 编译安装nagios- ...

  8. LVS负载均衡-基础知识梳理

    一. 集群的概念 服务器集群简称集群是一种服务器系统,它通过一组松散集成的服务器软件和/或硬件连接起来高度紧密地协作完成计算工作.在某种意义上,他们可以被看作是一台服务器.集群系统中的单个服务器通常称 ...

  9. linux下expect环境安装以及简单脚本测试

    expect是交互性很强的脚本语言,可以帮助运维人员实现批量管理成千上百台服务器操作,是一款很实用的批量部署工具!expect依赖于tcl,而linux系统里一般不自带安装tcl,所以需要手动安装 下 ...

  10. MySQL针对Swap分区的运维注意点

    Linux有很多很好的内存.IO调度机制,但是并不会适用于所有场景.对于运维人员来说,Linux比较让人头疼的一个地方是:它不会因为MySQL很重要就避免将分配给MySQL的地址空间映射到swap上. ...