POJ 1195:Mobile phones 二维树状数组
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 16893 | Accepted: 7789 |
Description
number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with
the row and the column of the matrix.
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
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
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 <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; #define MY_MAX 1100
int C[MY_MAX][MY_MAX];
int s; int lowbit(int x)
{
return x&(-x);
} void Add(int y,int x,int a)
{
while(y<=s)
{
int temp=x;
while(temp<=s)
{
C[y][temp]+=a;
temp +=lowbit(temp);
}
y +=lowbit(y);
}
} int QuerySum(int y,int x)
{
int nSum=0;
while(y>0)
{
int temp=x;
while(temp>0)
{
nSum += C[y][temp];
temp -= lowbit(temp);
}
y -=lowbit(y);
}
return nSum;
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout);
int oper;
int x,y,a,le,b,r,t; while(true)
{
scanf("%d",&oper);
if(oper==0)
{
scanf("%d",&s);
memset(C,0,sizeof(C));
}
else if(oper==1)
{
scanf("%d%d%d",&x,&y,&a);
Add(x+1,y+1,a);
}
else if(oper==2)
{
scanf("%d%d%d%d",&le,&b,&r,&t);
printf("%d\n",QuerySum(r+1,t+1) + QuerySum(le,b)- QuerySum(le,t+1) - QuerySum(r+1,b));
}
else if(oper==3)
{
break;
}
}
//system("pause");
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 1195:Mobile phones 二维树状数组的更多相关文章
- poj 1195 Mobile phones(二维树状数组)
树状数组支持两种操作: Add(x, d)操作: 让a[x]增加d. Query(L,R): 计算 a[L]+a[L+1]……a[R]. 当要频繁的对数组元素进行修改,同时又要频繁的查询数组内任一 ...
- 【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> # ...
随机推荐
- LTE 网元功能
E-NodeB : 无线资源管理,无线承载控制.无线接入控制.连接移动性控制.UE的上下行动态资源分配 IP头压缩及用户数据流加密 UE连接期间选择MME 路由用户面数据至S-GW 寻呼消息的组织和发 ...
- CSS - 去除图片img底侧空白缝隙
1. 图片底部有空隙 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 5款微信小程序开发工具使用报告,微信官方开发工具还有待提升
微信小程序已经内测有一段时间了,笔者本着好奇加学习的心态写了几个小demo,虽然在MINA框架上并没有遇到太多的坑,但官方开发工具实在不敢恭维. api提示不全,要一个个查api啊,写代码超级慢啊 很 ...
- 获取navigationController中的控制器
@implementation UIViewController (UIViewControllerExt) - (void)popViewController:(NSString *)control ...
- iOS10打电话、发短信、发邮件等小功能
注意:iOS10.0以后,使用openURL会有延迟,需要使用 openURL: options: completionHandler: 一.概要 本文中主要就是介绍在iOS中实现打电话.发短信.发邮 ...
- SLT学习——leafes tree扩展 【文艺平衡树】
这是一个全新的数据结构 md,别看这篇文章了,这篇已经废了. 百折不饶,再交一次,更新复杂度证明 这里是HYF,蒟蒻一只,最近因某些原因开始学数据结构了,然后就写了这篇题解. 下面给大家介绍一个全新的 ...
- 2016 黑客必备的Android应用都有哪些?
免责声明:本站所发布的此份清单仅供学习之用.我们不支持读者利用其中的任何工具进行任何不道德的恶意攻击行为. 根据业界的一系列评测以及亲身经验,我们整理出了这份最佳Android黑客应用清单.除了对应用 ...
- ADV-297 快速排序 java
问题描述 用递归来实现快速排序(quick sort)算法.快速排序算法的基本思路是:假设要对一个数组a进行排序,且a[0] = x.首先对数组中的元素进行调整,使x放在正确的位置上.同时,所有比x小 ...
- maven构建项目失败----99%
删除工作空间信息,重新导入,问题解决, 该问题,可能是安装Spring IDE 导致的问题,eclipse兼容性问题
- os期末复习
登记之后会发生两个变化:读者数增加(v操作).座位数减少(p操作) 注销之后会发生的变化:读者数减少(p操作).座位数增加(v操作) 必须要清楚释放的是甚麽,以及申请的是甚麽资源(在具体的题目当中) ...