poj 1195:Mobile phones(二维树状数组,矩阵求和)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 14489 | Accepted: 6735 |
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
Source

void Add(int x,int y,int a)
{
int i=x;
while(i<=s){
int j=y;
while(j<=s){
c[i][j]+=a;
j+=lowbit(j);
}
i+=lowbit(i);
}
}
2)求和:对左上角为(l,r),右下角为(b,t)的矩阵求和,即求该矩阵中所有元素的和。先求l到r的行矩阵的和,在求这个行矩阵和的时候,每一行要计算对应的b到t列的元素和。具体过程类似上述过程,将+lowbit()改为-lowbit()即可。限界为>=1。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; #define MAXN 1100 int c[MAXN][MAXN],s; int lowbit(int x)
{
return x&-x;
} void Add(int x,int y,int a) //加数
{
int i=x;
while(i<=s){ //行
int j=y;
while(j<=s){ //列
c[i][j]+=a;
j+=lowbit(j);
}
i+=lowbit(i);
}
} int Sum(int l,int r,int b,int t) //求和
{
l--,b--;
int suml=,sumr=;
//求行矩阵和,l以上矩阵
while(l>=){
int i=b,j=t;
int sumb=,sumt=;
//求列矩阵和
while(i>=){
sumb+=c[l][i];
i-=lowbit(i);
}
while(j>=){
sumt+=c[l][j];
j-=lowbit(j);
}
suml+=sumt-sumb;
l-=lowbit(l);
}
//求行矩阵和,r以上矩阵
while(r>=){
int i=b,j=t;
int sumb=,sumt=;
//求列矩阵和
while(i>=){
sumb+=c[r][i];
i-=lowbit(i);
}
while(j>=){
sumt+=c[r][j];
j-=lowbit(j);
}
sumr+=sumt-sumb;
r-=lowbit(r);
}
return sumr-suml;
} int main()
{
int cmd,x,y,a,l,r,b,t; while(scanf("%d",&cmd)!=EOF){
switch(cmd){
case : //初始化矩阵
scanf("%d",&s);
memset(c,,sizeof(c));
break; case : //加数
scanf("%d%d%d",&x,&y,&a);
Add(x+,y+,a);
break; case : //求矩阵和
scanf("%d%d%d%d",&l,&b,&r,&t);
printf("%d\n",Sum(l+,r+,b+,t+));
break; case : //退出程序
return ;
default:
break;
}
}
return ;
}
Freecode : www.cnblogs.com/yym2013
poj 1195:Mobile phones(二维树状数组,矩阵求和)的更多相关文章
- poj 1195 Mobile phones(二维树状数组)
树状数组支持两种操作: Add(x, d)操作: 让a[x]增加d. Query(L,R): 计算 a[L]+a[L+1]……a[R]. 当要频繁的对数组元素进行修改,同时又要频繁的查询数组内任一 ...
- POJ 1195:Mobile phones 二维树状数组
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 16893 Accepted: 7789 De ...
- 【poj1195】Mobile phones(二维树状数组)
题目链接:http://poj.org/problem?id=1195 [题意] 给出一个全0的矩阵,然后一些操作 0 S:初始化矩阵,维数是S*S,值全为0,这个操作只有最开始出现一次 1 X Y ...
- POJ 2155 Matrix【二维树状数组+YY(区间计数)】
题目链接:http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissio ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- POJ 2155 Matrix (二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17224 Accepted: 6460 Descripti ...
- POJ 2155 Matrix 【二维树状数组】(二维单点查询经典题)
<题目链接> 题目大意: 给出一个初始值全为0的矩阵,对其进行两个操作. 1.给出一个子矩阵的左上角和右上角坐标,这两个坐标所代表的矩阵内0变成1,1变成0. 2.查询某个坐标的点的值. ...
- POJ 2155 Matrix (二维树状数组)题解
思路: 没想到二维树状数组和一维的比只差了一行,update单点更新,query求和 这里的函数用法和平时不一样,query直接算出来就是某点的值,怎么做到的呢? 我们在更新的时候不止更新一个点,而是 ...
- POJ 2155:Matrix 二维树状数组
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 21757 Accepted: 8141 Descripti ...
- POJ 2155 Matrix(二维树状数组)
与以往不同的是,这个树状数组是二维的,仅此而已 #include <iostream> #include <cstdio> #include <cstring> # ...
随机推荐
- linux kernel i2c底层代码跟踪
其实跟上次跟的平台总线有关 在arch/arm/mach-mx6/board-mx6q_sabresd.c 文件中 static void __init mx6_sabresd_board_init( ...
- 绝对实用 NAT + VLAN +ACL管理企业网络
在企业中,要实现所有的员工都能与互联网进行通信,每个人各使用一个公网地址是很不现实的.一般,企业有1个或几个公网地址,而企业有几十.几百个员工.要想让所有的员工使用这仅有的几个公网地址与互联网通信该怎 ...
- SQL 数据库初学笔记一
做web刚好用得上SQL和php,图书馆借来书,来一个一晚上速成 <SQL必知必会>笔记 通用的语法,相关分类执行程序(DBMS): Apache Open Office Base Ado ...
- HTML之文本框关键字显示
文本框默认显示 "请输入关键字",当鼠标点击输入框的时候, "请输入关键字"这几个字消失,移出文本框又显示出来 <!DOCTYPE html> &l ...
- Python自动化之常用模块
1 time和datetime模块 #_*_coding:utf-8_*_ __author__ = 'Alex Li' import time # print(time.clock()) #返回处理 ...
- __getattr__ 与动态属性
直接上代码 >>> class Test(object): ... def __getattr__(self,attr_name): ... setattr(self, attr_n ...
- django xadmin 插件(1)
1. 插件的作用可以是全局的,也可以是只针对某个模型的.通过其 init_request控制是否加载此插件, demo如下: class SCPCardOverviewPlugin(BaseAdmin ...
- MyEclipse 优化
1.取消自动validation 有一堆,什么xml.jsp.jsf.js等等, 我们没有必要全部都去自动校验一下,只是需要的时候才会手工校验一下! 取消方法: windows-->perfer ...
- phpcms访问顶级栏目,自动跳到第一个子栏目
在顶级栏目的category页放入如下代码: <?php if($child){ $child_arrary=explode(',',$arrchildid); $to_url=$CATEGOR ...
- NPOI教程
NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. NPOI 官方网站:http://npoi.codeplex.com/( ...