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

0 4
1 1 2 3
2 0 0 2 2
1 1 1 2
1 1 2 -1
2 1 1 2 3
3

Sample Output

3
4
解题思路:这是一道二维树状数组入门题---单点修改、单点(区间)查询,其思路和一维树状数组非常相似,多加了一个维度而已。下面我们来看看怎么实现这两个基本操作:
将一维数组A[]扩展到二维数组A[][],二维树状数组C[][]来维护矩阵前缀和。
设原始二维数组A[][]={a11,a12,a13,a14,a15,
a21,a22,a23,a24,a25,
a31,a32,a33,a34,a35,
a41,a42,a43,a44,a45,
a51,a52,a53,a54,a55};
那么二维树状数组表示如下:
C[1][]=a1,C[1][]=a1+a1,C[1][]=a1,C[1][]=a1+a1+a1+a1,C[1][]=a15
这是数组A[][]第一行的一维树状数组;
C[][1]=a1+a1,C[][]=a1+a+a+a,C[][]=a+a,C[][]=a+a+a+a+a+a+a+a,C[][]=a+a5
这是数组A[][]第一行和第二行相加后得到的树状数组;
C[3][]=a3,C[3][]=a3+a3,C[3][]=a3,C[3][]=a3+a3+a3+a3,C[3][]=a35
这是数组A[][]第三行的一维树状数组;
C[][]=a+a+a+a,C[][]=a+a+a+a+a+a+a+a,C[][]=a+a+a+a...
这是数组A[][]前4行相加后得到的树状数组;
C[5][]=a5,C[5][]=a5+a5,C[5][]=a5,C[5][]=a5+a5+a5+a5,C[5][]=a55
这是数组A[][]第5行的一维树状数组。
仔细观察以上式子可以发现,二维树状数组C[x][y]的值其实是分别在x、y上的一维树状数组向下、向右(x上+lowbit(x)跳跃(>n停止),y上+lowbit(y)跳跃(>n停止))进行求和,这就是矩阵中坐标点值的单点修改。对于区间查询,同样分别在x、y上的一维树状数组从下往上,从右往左进行累加(y上-lowbit(y)跳跃(<=0停止),x上-lowbit(x)跳跃(<=0停止)),这样就得到了(1,1)到(x,y)矩阵中所有元素的和。
回到本题,题意为给出一些命令进行一些操作:
0 S 初始化一个全0的S*S矩阵,这个命令只会在第一次给出一次;
1 X Y A 给坐标点(X,Y)的值加上A;
2 L B R T 询问(L,B)到(R,T)构成的矩阵中所有元素的总和;
3 结束对矩阵的操作,程序终止。
典型的二维BIT,套一下模板即可,但需要注意一点:给出命令中的坐标都是默认从下标0开始的,为避免陷入死循环和计算错误,在更新和询问操作上统一对每一个坐标点(横、纵坐标)加1。
怎么统计坐标点(L,B)到(R,T)矩阵内所有值呢?给出下面的矩阵:
1 2 3 4 5
1 0 0 0 0 0
2 0 0 0 0 0
3 0 0 0
4 0 0 0
5 0 0 0 0
从图上可得计算公式:[R,T]-[L-1,T]-[R,B-1]+[L-1,B-1](多减去了一个左上角的矩阵,还要把它加回来),这样就得到了点(L,B)到(R,T)矩阵中所有元素的和。
AC代码:
 #include<cstdio>
#include<string.h>
const int maxn=;
int op,s,x,y,a,l,b,r,t,C[maxn][maxn];
void add(int x,int y,int val){//单点修改
for(int i=x;i<=s;i+=i&-i)
for(int j=y;j<=s;j+=j&-j)
C[i][j]+=val;
}
int query(int x,int y){//前缀和查询
int ans=;
for(int i=x;i>;i-=i&-i)
for(int j=y;j>;j-=j&-j)
ans+=C[i][j];
return ans;
}
int main(){
while(~scanf("%d%d",&op,&s)){
memset(C,,sizeof(C));
while(~scanf("%d",&op)&&op!=){
if(op==){
scanf("%d%d%d",&x,&y,&a);
x++,y++;add(x,y,a);//单点修改
}
else{
scanf("%d%d%d%d",&l,&b,&r,&t);l++,b++,r++,t++;
printf("%d\n",query(r,t)-query(l-,t)-query(r,b-)+query(l-,b-));//区间查询,求矩形中所有元素的和
}
}
}
return ;
}

题解报告:poj 1195 Mobile phones(二维BIT裸题)的更多相关文章

  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: 14489   Accepted: 6735 De ...

  3. poj 1195:Mobile phones(二维线段树,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 De ...

  4. (简单) POJ 1195 Mobile phones,二维树状数组。

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

  5. POJ 1195 Mobile phones(二维树状数组)

                                                                  Mobile phones Time Limit: 5000MS   Mem ...

  6. POJ 1195 Mobile phones (二维树状数组)

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

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

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

  8. POJ 1195 Mobile phones【二维树状数组】

    <题目链接> 题目大意: 一个由数字构成的大矩阵,开始是全0,能进行两种操作1) 对矩阵里的某个数加上一个整数(可正可负)2) 查询某个子矩阵里所有数字的和要求对每次查询,输出结果 解题分 ...

  9. POJ 1195 Mobile phones【 二维树状数组 】

    题意:基础的二维数组,注意 0 + lowbit(0)会陷入无限循环----- 之前做一道一维的一直tle,就是因为这个-------------------------- #include<i ...

随机推荐

  1. 【网络】TCP的流量控制

    一.利用滑动窗口实现流量控制 流量控制是让发送方的发生速率不要太快,要让接收方来得及接收. 发送方的发送窗口不能超过接收方给出的接收窗口的数值,TCP的窗口单位是字节,不是报文段. TCP为每一个连接 ...

  2. RGB中的颜色的设置

    用来表示一个 RGB 颜色值. 语法 RGB(red, green, blue) RGB 函数的语法含有以下这些命名参数: 部分 描述 red 必要参数:Variant (Integer).数值范围从 ...

  3. php进一法取整、四舍五入取整、忽略小数等的取整数方法大全

    PHP取整数函数常用的四种方法,下面收集了四个函数:经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已--主要是:ceil,floor,round,intval PHP取整数函数常用 ...

  4. openstack kolla多节点容器化环境安装

    好久没写随笔了,6月份趁着在公司没有太忙的事儿,把公司的服务器进行了虚拟化,采用的openstack当前的容器化方案kolla. 整体安装完的感受时,小白感觉自己是个大牛!哈哈,开玩笑,由于以前是开发 ...

  5. Unity即将到来的2D工具

    孙广东  2015.7.5 看了一下对功能介绍的视频,确实功能强大. 可是须要FQ在youtube上观看,所以就下载下来了.能够浏览一下: http://www.iqiyi.com/playlist2 ...

  6. vs2013发布网站提示 “未能将文件**复制到**”

    原因:年久失修,原来在项目中的一些文件给删掉或移除了 解决方法:打开.csproj文件(记事本打开),把提示的文件给删除掉.

  7. POJ 3368 Frequent values RMQ ST算法/线段树

                                                         Frequent values Time Limit: 2000MS   Memory Lim ...

  8. HDU1074 Doing Homework —— 状压DP

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Time Limit: 2000/1000 MS (J ...

  9. [noip模拟赛]小U的女装

    https://www.zybuluo.com/ysner/note/1329304 题面 有一张\(n\)点\(m\)边的.不一定联通的无向图. 如果选了一条边,就不能选其两个端点. 现在同时选点和 ...

  10. Gerrit配置--用户配置

    环境: Gerrit Server:172.16.206.133 Client:172.16.206.129 1.在Gerrit服务器上创建用户 Gerrit服务器使用的是HTTP认证类型,并用htt ...