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 ...
随机推荐
- 用Access作为后台数据库支撑。
/// <summary> /// 读取Excel文档 /// </summary> /// <param name="Path">文件名称&l ...
- Android中常用的五种数据存储方式
第一种: 使用SharedPreferences存储数据 适用范围: 保存少量的数据,且这些数据的格式非常简单:字符串型.基本类型的值.比如应用程序的各种配置信息(如是否打开音效.是否使用震动效果.小 ...
- windows mysql 自动备份的几种方法
转自:http://www.cnblogs.com/liongis/archive/2013/03/12/2956573.html 1.复制date文件夹备份===================== ...
- python 继承
继承一个类 如果已经定义了Person类,需要定义新的Student和Teacher类时,可以直接从Person类继承: class Person(object): def __init__(self ...
- ps命令使用 进程查看
ps命令是Process Status的缩写 用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信息,就可以使用top命 ...
- Myeclipse中创建Maven工程的时候没有 webapp-javaee6
1. http://mvnrepository.com/artifact/org.codehaus.mojo.archetypes/webapp-javaee6/1.5 中有描述
- 餐厅外卖app第三天
n听
- 看Ue4角色代码——跳跃与实现二段跳
看了一下终于发现了跳跃的关键代码 bool UCharacterMovementComponent::DoJump(bool bReplayingMoves) { if ( CharacterOwne ...
- iris数据集
iris以鸢尾花的特征作为数据来源,数据集包含150个数据集,分为3类,每类50个数据,每个数据包含4个属性,是在数据挖掘.数据分类中非常常用的测试集.训练集. 链接地址
- java基础-基本数据类型
浏览以下内容前,请点击并阅读 声明 java有八种基本数据类型,其中包括: byte,字节型,是8位带符号的整数,即其范围在-128和127之间(包括) short,短整型,是16位带符号的整数,范围 ...