POJ 1195 Mobile phones (二维树状数组)
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 <cstring>
#include <cstdio>
#include <cmath>
#include <set>
#include <stack>
#include <cctype>
#include <algorithm>
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL;
const int mod = 99999997;
const int MAX = 1000000000;
const int maxn = 100005;
int ca, s, op;
int c[1100][1100];
void add(int i, int j, int v) {
while(i <= s) {
int y = j;
while(y <= s) {
c[i][y] += v;
y += y&-y;
}
i += i&-i;
}
}
int query(int i ,int j) {
int ans = 0;
while(i > 0) {
int y = j;
while(y > 0) {
ans += c[i][y];
y -= y&-y;
}
i -= i&-i;
}
return ans;
}
int main()
{
cin >> ca >> s;
while(1) {
scanf("%d", &op);
if(op == 1) {
int a, b, v;
scanf("%d%d%d", &a, &b, &v);
add(a+1, b+1, v);
} else if(op == 2) {
int x1, x2, y1, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
int ans = query(x2+1, y2+1) + query(x1, y1) - query(x2+1, y1) - query(x1, y2+1);
printf("%d\n", ans);
} else break;
}
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]. 当要频繁的对数组元素进行修改,同时又要频繁的查询数组内任一 ...
- 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> # ...
随机推荐
- 【CF1023A】Single Wildcard Pattern Matching(模拟)
题意:给定两个串s与t,其中s可能有至多一个通配符*可以被当做任意长度与内容的串,问t能否与s匹配 n,m<=2e5 思路: #include<cstdio> #include< ...
- 【CF1016A】Death Note(签到)
题意:无限页的书,每页可以写m个名字,给你一个长度为n的序列,序列为你每天要写的名字数,输出你每天要翻的页数. n<=2e5,m,a[i]<=1e9 思路: #include<cst ...
- css选择器浅谈
css选择器有很多,种类的话总结起来有5种.即: id选择器,class选择器,elements选择器,级联选择器,相邻选择器. 前三个没什么好说的,分别是id,class和标签的选择,注意选中对象的 ...
- 从反汇编看待C++ new
首先来看最简单的new操作 int main() { int *temp = new int; delete temp; } 反汇编结果:调用了operator new 00311C9E push 4 ...
- PHP 几个常用的正则表达式
记录几个PHP中比较常用的正则. , $max_len = 20){ if (empty($username)) { return false; } $match = '/^(?![0-9]+$)[\ ...
- vue 权限控制按钮3种样式、内容、以及跳转事件
最近碰到一个因为要根据权限来给一个按钮变成不同功能, 简单写出3个按钮然后用v-if也能实现这个功能,但是在加载页面时,如果延迟过高则会把按钮按照DOM顺序加载出来,这是个很不好的效果 思索了下,把三 ...
- (4)ASP.NET HttpRequest 类
HttpRequest 类的主要作用是读取客户端在 Web 请求期间发送的 HTTP 值. https://msdn.microsoft.com/zh-cn/library/system.web.ht ...
- (38)C#IIS
IIS7.5标识介绍(转) http://www.cnblogs.com/zgqys1980/p/3862815.html 应用程序池的标识是运行应用程序池的工作进程所使用的服务帐户名称.默认情况下, ...
- tomcat7.0.55配置单向和双向HTTPS连接(二)
上一篇文章:tomcat7.0.55配置单向和双向HTTPS连接 只是简要的配置了一下HTTPS,还有许多问题没有解决,本篇来解决这些文件 首先按照这篇文章:Widows下利用OpenSSL生成证书来 ...
- JDK1.8和Spring 3.2.0 的坑
上午 org.apache.catalina.core.StandardContext listenerStart严重: Exception sending context initialized e ...