Mobile phones
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 18489   Accepted: 8558

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

Source

题意:
在一个二维平面内的所有点,共有三种操作,0 S表示把S*S区域内的点的值清零;X,Y,A表示将点(X,Y)的值加A;L,B,R,T表示求(L,B)~(R,T)之间的所有的点的值之和
代码:
 //树状数组模板
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int flag,X,Y,A,L,B,R,T,S;
int squ[][];
int lowbit(int x)
{
return x&(-x);
}
void add(int idx,int idy,int val)
{
for(int i=idx;i<=;i+=lowbit(i))
{
for(int j=idy;j<=;j+=lowbit(j))
{
squ[i][j]+=val;
}
}
}
int sum(int idx,int idy)
{
int s=;
for(int i=idx;i>;i-=lowbit(i))
{
for(int j=idy;j>;j-=lowbit(j))
{
s+=squ[i][j];
}
}
return s;
}
int ans(int x1,int y1,int x2,int y2)
{
return (sum(x2,y2)-sum(x1-,y2)-sum(x2,y1-)+sum(x1-,y1-));
}
int main()
{
while(scanf("%d",&flag)&&flag!=)
{
if(flag==)
{
scanf("%d",&S);
memset(squ,,sizeof(squ));
}
else if(flag==)
{
scanf("%d%d%d",&X,&Y,&A);
if(A>=)
add(X+,Y+,A);
else
{
int num=ans(X+,Y+,X+,Y+);
add(X+,Y+,num+A>?A:-num);
}
}
else if(flag==)
{
scanf("%d%d%d%d",&L,&B,&R,&T);
printf("%d\n",ans(L+,B+,R+,T+));
}
}
return ;
}

POJ 1195 二维树状数组的更多相关文章

  1. Mobile phones POJ - 1195 二维树状数组求和

    Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows ...

  2. poj 2029 二维树状数组

    思路:简单树状数组 #include<map> #include<set> #include<cmath> #include<queue> #inclu ...

  3. poj 3378 二维树状数组

    思路:直接用long long 保存会WA.用下高精度加法就行了. #include<map> #include<set> #include<cmath> #inc ...

  4. poj 2155 (二维树状数组 区间修改 求某点值)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33682   Accepted: 12194 Descript ...

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

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

  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【二维树状数组】

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

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

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

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

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

随机推荐

  1. ubuntu14使用qemu调试linux内核

    # 下载内核源代码编译内核 cd ~/LinuxKernel/ wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.18.6.tar.x ...

  2. 移动端-解决ios连续点击页面上移问题

    引入js即可 //解决ios双击页面上移问题//在项目中测试不紧input/button这些表单控件有这个问题,p,div等也有问题,于是乎就直接在body开刀了(function(){ var ag ...

  3. SQL数据库的基本语句

    1.修改字段类型语句: alter table 表名 alter column 列名  类型 例如: alter table D  alter column no char(15): 2.从其他地方插 ...

  4. Java调优经验谈

    对于调优这个事情来说,一般就是三个过程: 性能监控:问题没有发生,你并不知道你需要调优什么?此时需要一些系统.应用的监控工具来发现问题. 性能分析:问题已经发生,但是你并不知道问题到底出在哪里.此时就 ...

  5. css整理-01选择器和继承

    元素 元素形式: 替换,非替换 元素类型: 块级,行内 列表是特殊的块级元素,它会生成一个标记符 样式表 候选样式表: rel='alternative' @import导入样式表,必须在style的 ...

  6. Transform组件C#游戏开发快速入门

    Transform组件C#游戏开发快速入门大学霸 组件(Component)可以看作是一类属性的总称.而属性是指游戏对象上一切可设置.调节的选项,如图2-8所示.本文选自C#游戏开发快速入门大学霸   ...

  7. 手持终端打印POS机(安装移动销售开单订货会软件)无线传输到订货会后台销售管理系统

    当今的服装市场是品牌竞争时代,产品能否紧随潮流前线并迅速推出市场抢得先机,是品牌成功与否的关键.而订货会是每个鞋服企业新产品走向市场至关重要的开端,订货会如何演绎.成功与否,与品牌在竞争洪流中的命运息 ...

  8. Delphi7 客户端调用WebService(天气预报)

    客户程序:第一步:新建一个Application. 第二步:File----->New----->Other------>WebServices----->WSDL Impor ...

  9. Postman - HTTP接口测试工具

    Postman 是一个 Chrome 的 插件,它主要是用来模拟各种HTTP请求的(如:get/post/delete/put..等等),下面介绍下Postman的安装和使用方法: 一.安装Postm ...

  10. 最近发现docker感觉不错

    最近发现docker感觉不错,接下来开始学习docker方面的技术.lxc也可以学学. storm,kafka也要熟悉起来.