您的位置:首页 > 其它

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

2016-08-12 17:27 267 查看
Mobile phones

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

题目大意:有一个S * S的区域,里面有一些小格子,每个格子里有一座通信塔。通信塔可以为覆盖范围内的移动手机提供服务。当然,用户可以拿着手机移动,所以通信塔服务的手机数量是在动态变化的。现在给你一些指令和参数,输出每次查询的结果。指令如下
指令            参数           操作

0                  S                初始化一片S * S的区域,下标从0到S - 1,每个格子中通信塔服务的手机数量均为0

1                  X Y A         向格子(X,Y)添加A部手机,A可正可负

2                  L B R T      查询格子(Xi,Yi)里的手机数量之和,其中L <= Xi <= R,B <= Yi <= T

3                                     终止程序

解题思路:使用二维树状数组维护每个通信塔服务的手机数量,当指令为2是输出sum(R + 1,T + 1) - sum(L,T + 1) - sum(R + 1,B) + sum(L,B)即可,注意区域的坐标为0到S - 1,而树状数组不能有0号下标,所以调用add函数的时候x和y都要加1。

代码如下:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define EPS 1e-6
#define INF INT_MAX / 10
#define LL long long
#define MOD 100000000
#define PI acos(-1.0)
using namespace std;
const int maxs = 1111;
int bit[maxs][maxs];
int s;

int lowbit(int x)
{
return x & -x;
}

int sum(int posx,int posy)
{
int res = 0;
int x = posx,y = posy;
while(x > 0){
while(y > 0){
res += bit[x][y];
y -= y & -y;
}
x -= x & -x;
y = posy;
}
return res;
}

void add(int posx,int posy,int val)
{
int x = posx,y = posy;
while(x <= s){
while(y <= s){
bit[x][y] += val;
y += y & -y;
}
x += x & -x;
y = posy;
}
}

int main()
{
int x,y,a,b,l,r,t,opt;
while(scanf("%d",&opt) != EOF && opt != 3){
if(opt == 0){
scanf("%d",&s);
memset(bit,0,sizeof(bit));
}
else if(opt == 1){
scanf("%d %d %d",&x,&y,&a);
add(x + 1,y + 1,a);
}
else{
scanf("%d %d %d %d",&l,&b,&r,&t);
printf("%d\n",sum(r + 1,t + 1) - sum(l,t + 1) - sum(r + 1,b) + sum(l,b));
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POJ