POJ 1195 二维树状数组
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 18489 | Accepted: 8558 |
Description
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
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
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
//树状数组模板
#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 二维树状数组的更多相关文章
- Mobile phones POJ - 1195 二维树状数组求和
Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows ...
- poj 2029 二维树状数组
思路:简单树状数组 #include<map> #include<set> #include<cmath> #include<queue> #inclu ...
- poj 3378 二维树状数组
思路:直接用long long 保存会WA.用下高精度加法就行了. #include<map> #include<set> #include<cmath> #inc ...
- poj 2155 (二维树状数组 区间修改 求某点值)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 33682 Accepted: 12194 Descript ...
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- (简单) POJ 1195 Mobile phones,二维树状数组。
Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...
- POJ 1195 Mobile phones【二维树状数组】
<题目链接> 题目大意: 一个由数字构成的大矩阵,开始是全0,能进行两种操作1) 对矩阵里的某个数加上一个整数(可正可负)2) 查询某个子矩阵里所有数字的和要求对每次查询,输出结果 解题分 ...
- POJ 1195 Mobile phones (二维树状数组)
Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...
- POJ 1195:Mobile phones 二维树状数组
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 16893 Accepted: 7789 De ...
随机推荐
- 驱动开发之 创建线程函数PsCreateSystemThread
PsCreateSystemThread 创建一个执行在内核模式的系统线程. 注意:创建线程必须用函数PsTerminateSystemThread强制线程结束.否则该线程是无法自动退出的. 函数原型 ...
- SQLserver删除某数据库中所有表 方法 二
方便删除数据库中所有的数据表,清空数据库,有些有约束,不能直接delete,需要先删除库中的约束,代码如下: --删除所有约束DECLARE c1 cursor for select 'alter t ...
- WINDOWS系统下四叶草CLOVER引导U盘制作
一.所需软件1.下载BootDiskUtility链接:http://pan.baidu.com/s/1bwBFyu 密码:glyk 2.下载Clover r3330 USB链接:http://pan ...
- mysql replace into用法与坑
需要主键一致 PRIMARY KEY (id) PRIMARY KEY (id, ts) 坑: https://blog.xupeng.me/2013/10/11/mysql-replace-into ...
- 某app客户端数字签名分析
最近测试app时发现某app对数据包做了签名,其直接后果就导致截获的数据包没法修改,因此对该app的数字签名了进行了一次分析.
- 17996 Daily Cool Run (dp)
时间限制:1000MS 内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题 语言: 不限定 Description Daily Cool Run is a popular gam ...
- <meta>元素
HTML <meta> 元素 meta标签描述了一些基本的元数据. <meta> 标签提供了元数据.元数据也不显示在页面上,但会被浏览器解析. META元素通常用于指定网页的描 ...
- iOS 常用控件 参数
1.StatusBar 20px 2.TableViewCell 44px 3.TabBar 49px 4.NavigationBar 44px 5.NaviBarIcon 20*20px 6.Tab ...
- WPF 颜色转换
从字符串到画刷: var converter = new System.Windows.Media.BrushConverter(); var brush = (Brush)converter.Con ...
- HDU-2159FATE(二维完全背包)
FATE Problem Description 最 近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完 ...