POJ1195(二维树状数组)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 17176 | Accepted: 7920 |
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

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
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
二维树状数组模板题。
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=+;
int bit[MAXN][MAXN];
int lowbit(int i)
{
return i&(-i);
}
void add(int x,int y,int a)
{
for(int i=x;i<MAXN;i+=lowbit(i))
for(int j=y;j<MAXN;j+=lowbit(j))
bit[i][j]+=a;
}
long long sum(int x,int y)
{
long long s=;
for(int i=x;i>;i-=lowbit(i))
for(int j=y;j>;j-=lowbit(j))
s+=bit[i][j];
return s;
}
long long getSum(int x1,int y1,int x2,int y2)
{
return sum(x2,y2)+sum(x1-,y1-)-sum(x2,y1-)-sum(x1-,y2);
}
int main()
{
int Init;
while(scanf("%d",&Init)!=EOF)
{
int n;
if(Init==)
{
scanf("%d",&n);
memset(bit,,sizeof(bit));
}
else if(Init==)
{
int x,y,a;
scanf("%d%d%d",&x,&y,&a);
x++,y++;
add(x,y,a);
}
else if(Init==)
{
int L,B,R,T;
scanf("%d%d%d%d",&L,&B,&R,&T);
L++,B++,R++,T++;
printf("%I64d\n",getSum(L,B,R,T));
}
else
{
break;
}
} return ;
}
POJ1195(二维树状数组)的更多相关文章
- poj1195二维树状数组模板
二维树状数组和一维的也差不多,改一下add和query函数即可:即按行修改,行内单点修改即可 /* 二维树状数组,询问一个二维区间内的数之和 */ #include<iostream> # ...
- 二维树状数组poj1195
题目链接:https://vjudge.net/problem/POJ-1195 题意:一开始输入0和一个s,0代表开始,s代表这是一个s*s的图,接下来会输入1或2,1代表进行单点修改,后面会接3个 ...
- poj1195(二维树状数组)
题目链接:https://vjudge.net/problem/POJ-1195 题意:有s*s的矩阵,初始化为全0,有两种操作,单点修改(x,y)的值,区间查询(x,y)的值(l<=x< ...
- POJ-1195 Mobile phones---裸的二维树状数组(注意下标从1,1开始)
题目链接: https://vjudge.net/problem/POJ-1195 题目大意: 直接维护二维树状数组 注意横纵坐标全部需要加1,因为树状数组从(1,1)开始 #include<c ...
- 二维树状数组(水题) POJ1195
前段时间遇到线段树过不了,树状数组却过了的题.(其实线段树过得了的) 回忆了下树状数组. 主要原理,还是二进制位数,每一项的和表示其为它的前((最后一位1及其后)的二进制数)和,可从二进制图来看.(用 ...
- 【POJ1195】【二维树状数组】Mobile phones
Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...
- 【poj1195】Mobile phones(二维树状数组)
题目链接:http://poj.org/problem?id=1195 [题意] 给出一个全0的矩阵,然后一些操作 0 S:初始化矩阵,维数是S*S,值全为0,这个操作只有最开始出现一次 1 X Y ...
- 二维树状数组 BZOJ 1452 [JSOI2009]Count
题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...
- HDU1559 最大子矩阵 (二维树状数组)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) ...
随机推荐
- 【Unity3D】【NGUI】Atlas的动态创建
NGUI版本号:3.6.5 1.參见SZUIAtlasMakerRuntimeTest设置对应的值以上值须要提前设置好 2.没有检查是否atlas可以正确创建,自己可以改,增加返回值 3.代码都是在N ...
- Drupal 主题的表现形式
1.template.php /** * Implements hook_theme(). */ function yourtheme_theme($existing, $type, $theme ...
- H2 database 操作操作内存表
本例开发工具为 NetBeans,使用b2前提安装jdk. 第一步:在官网下载驱动包 :http://www.h2database.com ,本例版本为: h2-1.4.192.jar 第二步:安装开 ...
- mysq'l系列之10.mysql优化&权限控制
网站打开慢如何排查 1.打开网页, 用谷歌浏览器F12, 查看network: 哪个加载时间长就优化哪个 2.如果是数据库问题 2.1 查看大体情况 # top # uptime //load av ...
- Method invoke 方法
这个问题要看明白源码才能解决
- 查看SqlServer安装的log文件
SqlServer安装时产生的log被保存在这个目录下: "%programfiles%\Microsoft SQL Server\[SQL_VERSION]\Setup Bootstrap ...
- C ~ 指针的运算
一 :取地址运算“&”与取内容运算“*”: 单目运算“&”是取操作对象的地址 , “*”是取指针指向的对象的内容 , 两者互为逆运算 int x , *p ; p = &x ; ...
- 关于JMS和MQ
2.1 什么是JMS? JMS是java的消息服务,JMS的客户端之间可以通过JMS服务进行异步的消息传输. 2.2 什么是消息模型 ○ Point-to-Point(P2P) --- 点对点 ○ P ...
- Buffer的数据存取
缓冲区 存放要读取的数据 缓冲区 和 通道 配合使用 一个用于特定基本数据类行的容器.有java.nio包定义的,所有缓冲区都是抽象类Buffer的子类. Java NIO中的Buffer主要用于与N ...
- input标签添加上disable属性在移动端字体颜色不兼容的解决办法。
input[disabled],input:disabled,input.disabled{ color: #999; -webkit-text-fill-color:#999; -webkit-op ...