Mobile phones
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 14288   Accepted: 6642

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-1编号。每一个小方格都包括一个收发站。每一个方格内的手机的移动电话数量能够不断改变,由于手机用户在各个方格之间移动,也实用户开机或者关机。一旦某个方格里面开机的移动电话数量发生了变化,该方格里的收发站就会向总部发送一条信息说明这个改变量。

总部要你写一个程序,用来管理从各个收发站收到的信息。老板可能随时会问:某个给定矩形区域内有多少部开机的移动电话啊?你的程序必需要能随时回答老板的问题。

【输入输出数据】

从标准输入读入整数,向标准输出写入你对老板的回答。

输入数据的格式例如以下:每一个输入独立成一行。一个输入包含一个指示数和一些參数,见下表:

指示数

參数

意义

0

S

初始指令。整个区域由S*S个小方格组成。这个指令仅仅会在一開始出现一次。

1

X Y A

方格(X,Y)内的开机移动电话量添加了A。A可能是正数也可能是负数

2

L B R T

询问在矩形区域(L,B)—(R,T)内有多少部开机的移动电话。矩形区域(L,B)—(R,T)包含全部的格子(X。Y)满足L<=X<=R,B<=Y<=T.

3

终止程序。

这个指令仅仅会在最后出现一次。

全部的数据总是在给定范围内,你不须要差错。

特别的,假设A是负数,你能够觉得该操作不会让该格子的开机移动电话数变成负数。格子是从0開始编号的,比方一个4*4的区域,全部的格子(X,Y)应该表示为:0<=X<=3,0<=Y<=3。

对于除了2之外的指示,你的程序不应该输出不论什么东西。

假设指示是2。那么你的程序应该向标准输出写入一个整数。

【数据限制】

区域大小

S * S

1 * 1 <= S * S <= 1024 * 1024

每一个格子的值

V

0 <= V <= 32767

添加/降低量

A

-32768 <= A <= 32767

指令总数

U

3 <= U <= 60002

全部格子的和

M

M= 2^30

第一次在OJ题中用函数指针数组。感觉挺简洁的。

#include <stdio.h>

int tree[1030][1030], size;

int lowBit(int n)
{
return n & (-n);
} void getSize()
{
scanf("%d", &size);
} void update()
{
int x, y, val;
scanf("%d%d%d", &x, &y, &val);
++x; ++y; int temp;
while(x <= size){
temp = y;
while(temp <= size){
tree[x][temp] += val;
temp += lowBit(temp);
}
x += lowBit(x);
}
} int getSum(int x, int y)
{
int sum = 0, temp;
while(x > 0){
temp = y;
while(temp > 0){
sum += tree[x][temp];
temp -= lowBit(temp);
}
x -= lowBit(x);
}
return sum;
} void query()
{
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
++x1; ++y1; ++x2; ++y2; int sum = getSum(x2, y2) - getSum(x2, y1 - 1) -
getSum(x1 - 1, y2) + getSum(x1 - 1, y1 - 1);
printf("%d\n", sum);
} void (*funArr[])() = {
getSize, update, query
}; //函数指针数组 int main()
{
int com;
while(scanf("%d", &com), com != 3)
(*funArr[com])();
return 0;
}

POJ1195 Mobile phones 【二维树状数组】的更多相关文章

  1. 【poj1195】Mobile phones(二维树状数组)

    题目链接:http://poj.org/problem?id=1195 [题意] 给出一个全0的矩阵,然后一些操作 0 S:初始化矩阵,维数是S*S,值全为0,这个操作只有最开始出现一次 1 X Y ...

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

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

  3. poj 1195 Mobile phones(二维树状数组)

    树状数组支持两种操作: Add(x, d)操作:   让a[x]增加d. Query(L,R): 计算 a[L]+a[L+1]……a[R]. 当要频繁的对数组元素进行修改,同时又要频繁的查询数组内任一 ...

  4. 【POJ1195】【二维树状数组】Mobile phones

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

  5. Mobile phones_二维树状数组

    [题意]给你一个矩阵(初始化为0)和一些操作,1 x y a表示在arr[x][y]加上a,2 l b r t 表示求左上角为(l,b),右下角为(r,t)的矩阵的和. [思路]帮助更好理解树状数组. ...

  6. poj1195Mobile phones(二维树状数组)

    http://poj.org/problem?id=1195 模版题 i写成k了 找了一个多小时没找出来.. #include <iostream> #include<cstring ...

  7. poj_1195Mobile phones,二维树状数组

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...

  8. poj 1195:Mobile phones(二维树状数组,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 De ...

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

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

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

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

随机推荐

  1. LCIS 最长上升公共子序列问题

    首先点名一个串叫 L1,另一个叫L2. 明显的是一个DP,那么我们来探讨下如何求得答案. 朴素的算法 首先我们定义状态$dp[ i ][ j ]$表示L1中前i个与L2中前j个的最长公共上升子序列. ...

  2. 小程序之如何设置图片以及image组件的属性

    1. 设置图片,小程序支持两种引用图片方法,一种是本地引用,一种是网络资源引用. 但是引用本地图片的的时候不能用wxml样式去引用本地的图片,不会报错,也没效果.就是在wxss页面中不能引用本地的图片 ...

  3. c++_加法变乘法

    加法变乘法 我们都知道:1+2+3+ ... + 49 = 1225现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015 比如:1+2+3+...+10*11+12+...+27*28+29+ ...

  4. 第二章:systemverilog声明的位置

    1.package 定义及从package中导入定义(***) verilog中,对于变量.线网.task.function的声明必须在module和endmodule之间.如果task被多个modu ...

  5. CentOS6.8下安装Docker

    原文章链接https://www.cnblogs.com/baolong/p/5743420.html. 由于在自己安装的虚拟机上打开linux终端命令行输入uname -a 以及cat /etc/r ...

  6. python_字符串类型

    1.在python中用单引号' ',双引号'' '',三引号'''  ''' 标注字符串类型. >>> name = "Alex Li" #双引号 >> ...

  7. Spring Boot集成百度Ueditor

    遇到的问题: 1.将ueditor加入/static目录下,能正常显示,但是出现“请求后台配置项http错误,上传功能将不能正常使用!”(解决在下面,都是自定义上传的,如果需要官方的示例,我也无能为力 ...

  8. 【HIHOCODER 1478】 水陆距离(BFS)

    描述 给定一个N x M的01矩阵,其中1表示陆地,0表示水域.对于每一个位置,求出它距离最近的水域的距离是多少. 矩阵中每个位置与它上下左右相邻的格子距离为1. 输入 第一行包含两个整数,N和M. ...

  9. python接口自动化-multipart/form-data上传图片

    前言 在提交表单操作的时候,经常会遇到图片上传的操作,图片上传是一个单独的接口,本篇以禅道为例,介绍如何上传图片 上传接口 1.以禅道上提交bug为例,在选择图片时,点确定按钮,就是上传图片了 2.用 ...

  10. java常见问题集锦

    Eclipse 编译错误 Access restriction:The type *** is not accessible due to restriction on... 解决方案 Eclipse ...