POJ 1195 Mobile phones(树状数组)
来源:互联网 发布:达芬奇调色 知乎 编辑:程序博客网 时间:2024/06/05 18:34
Mobile phones
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
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.
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
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 41 1 2 32 0 0 2 2 1 1 1 21 1 2 -12 1 1 2 3 3
Sample Output
34
题意:有N*N矩阵,有三个操作,0是初始化矩阵,1是更新区间元素,2是询问,输出给定矩阵内的元素和。
心得:很裸的二维树状数组,简直就是模板题啊~~~不过,细节还是很容易错的。开始时,自己写的,没注意如何表示给定矩形的和,一直调不出来,后来画了个图,终于搞出来了,唉,这抽象思维啊。。。好好努力,下回仔细点。。。
分析:二维树状数组,其实跟一维的原理是一样的,就是把一维坐标,换成了二维坐标,更新与求和的时候用两层循环。再一个就是求区域的和,要注意怎么表示区域和,画个图,你会懂得^_^~~~详见代码
AC代码:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <cstdlib>#include <ctime>#define INF 0x7fffffffusing namespace std;const int maxx = 1200;int c[maxx][maxx];int s;int lowbit(int x){ return x & (-x);}long long sum(int x,int y){ long long ret = 0; for(int i=x; i>0; i-=lowbit(i)) for(int j=y; j>0; j-=lowbit(j)) ret += c[i][j]; return ret;}void add(int x,int y,int d){ for(int i=x; i<=maxx; i+=lowbit(i)) for(int j=y; j<=maxx; j+=lowbit(j)) c[i][j] += d;}int main(){ int f,x,y,a,l,b,r,t; while(scanf("%d",&f)!=EOF) { if(f == 0) //操作为0时,初始化矩阵 { scanf("%d",&s); memset(c,0,sizeof(c)); } else if(f == 1) //操作为1时,更新整个区间范围 { scanf("%d%d%d",&x,&y,&a); add(x+1,y+1,a); //注意x,y有可能为0,加入x+1,y+1 } else if(f == 2) //询问,输出矩阵内的元素和 { scanf("%d%d%d%d",&l,&b,&r,&t); printf("%ld\n",sum(r+1,t+1)-sum(r+1,b)-sum(l,t+1)+sum(l,b)); //注意如何输出矩形内的所有元素之和 } else break; } return 0;}
0 0
- poj 1195 Mobile phones(树状数组)
- poj - 1195 - Mobile phones(树状数组)
- POJ 1195 Mobile phones(树状数组)
- POJ 1195 Mobile phones(树状数组)
- poj 1195 Mobile phones(树状数组)
- POJ 1195 Mobile phones 二维树状数组
- POJ 1195 Mobile phones【二维树状数组】
- POJ 1195 Mobile phones 二维树状数组
- poj 1195 Mobile phones 二维树状数组
- POJ 1195 Mobile phones 二维树状数组
- poj 1195 Mobile phones +二维树状数组
- POJ 1195 Mobile phones 二维树状数组
- Mobile Phones poj 1195--二维树状数组
- POJ 1195 Mobile phones【二维树状数组】
- POJ 1195 Mobile phones(二维树状数组)
- poj 1195 - Mobile phones(树状数组)
- POJ 1195 Mobile phones(二维树状数组)
- [树状数组]POJ 1195 Mobile phones
- hud_bone Collector
- HDU 3488 费用流 圈
- 十大致癌食物黑名单公布 葵花子竟然排第一(图)
- 树链剖分-点的分治(链的点的个数为k的点对数)
- Android 网络通信框架Volley简介(Google IO 2013)
- POJ 1195 Mobile phones(树状数组)
- hdu 2089 不要62
- 多校联合训练赛 第三场--B - The battle near the swamp
- Ubuntu12.04 侧边栏不显示图标-解决方案
- HDU 3033 I love sneakers! (DP 01背包+完全背包)
- 存储结构和事务
- TW_第一次内容简介
- WEB服务器、应用程序服务器、HTTP服务器区别
- Wikioi 1017 乘积最大