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 ...
随机推荐
- Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组
E. George and Cards George is a cat, so he loves playing very much. Vitaly put n cards in a row in ...
- Nginx日志定时切割脚本
nginx的日志文件如果你不处理,将变得越来越大,我们可以写一个nginx日志切割脚本来自动切割日志文件. 第一步就是重命名日志文件,不用担心重命名后nginx找不到日志文件而丢失日志.在你未重新打开 ...
- [HTTP那些事]网络请求API
在Android上,原生API有两个,HttpUrlConnection和HttpClient,它们对封装Socket进行封装,让HTTP请求变得简单.这应该也算框架吧? 想象下,如果没有HttpUr ...
- 让一个div在不同的显示器中永远居中
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- [工作中的设计模式]备忘录模式memento
一.模式解析 备忘录对象是一个用来存储另外一个对象内部状态的快照的对象.备忘录模式的用意是在不破坏封装的条件下,将一个对象的状态捕捉(Capture)住,并外部化,存储起来,从而可以在将来合适的时候把 ...
- [xsd学习]复合元素
对于xsd,复合元素的定义有两种方式: 一.在元素内部直接声明,此种方法只能此元素使用 <xs:element name="employee"> <xs:comp ...
- Git 使用命令
$ git add . $ git commit -m "html files created 20160308 16:08" $ git push origin master 设 ...
- 九校联考 终&启
one term's ending... class:12 school:130...130...130... 至今没有看到九校的排名,如果九校排名正常的话,那yyhs的学生也太可怕了...估计要三百 ...
- linux TCP Wrappers
1. TCP Wrapper简介 (出处:http://www.softpanorama.org/Net/Network_security/TCP_wrappers/) (维基百科)TCP Wrapp ...
- Apache ActiveMQの版本更迭和Apache ActiveMQの故障转移
本文描述apache activemq 版本更迭的原因以及Apache ActiveMQのThe Failover Transport new features in 5.2.0 1.对信息的传输/ ...