POJ1195 Mobile phones 【二维线段树】
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 14291 | Accepted: 6644 |
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
附二维树状数组解法:http://blog.csdn.net/chang_mu/article/details/37739053
题意:点更新。区域查询。
题解:主要的二维线段树。(今天看了非常久才看懂...线段树扩展到二维果然没有树状数组方便,可是一旦想通,会发现思路事实上并不复杂)。
#include <stdio.h>
#define maxn 1026
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1 int tree[maxn * 3][maxn * 3], size; void getSize(){ scanf("%d", &size); } void updateY(int rootX, int pos, int val, int l, int r, int rt)
{
tree[rootX][rt] += val;
if(l == r) return; int mid = (l + r) >> 1;
if(pos <= mid) updateY(rootX, pos, val, lson);
else updateY(rootX, pos, val, rson);
} void updateX(int x, int y, int val, int l, int r, int rt)
{
//更新的任务都交给updateY
updateY(rt, y, val, 0, size - 1, 1);
if(l == r) return; int mid = (l + r) >> 1;
if(x <= mid) updateX(x, y, val, lson);
else updateX(x, y, val, rson);
} void update()
{
int x, y, val;
scanf("%d%d%d", &x, &y, &val);
updateX(x, y, val, 0, size - 1, 1);
} int queryY(int rootX, int y1, int y2, int l, int r, int rt)
{
if(l == y1 && r == y2) return tree[rootX][rt]; int mid = (l + r) >> 1;
if(y2 <= mid) return queryY(rootX, y1, y2, lson);
else if(y1 > mid) return queryY(rootX, y1, y2, rson);
else{
return queryY(rootX, y1, mid, lson) + queryY(rootX, mid + 1, y2, rson);
}
} int queryX(int x1, int x2, int y1, int y2, int l, int r, int rt)
{
if(x1 == l && x2 == r) return queryY(rt, y1, y2, 0, size - 1, 1); int mid = (l + r) >> 1;
if(x2 <= mid) return queryX(x1, x2, y1, y2, lson);
else if(x1 > mid) queryX(x1, x2, y1, y2, rson);
else{
return queryX(x1, mid, y1, y2, lson) + queryX(mid + 1, x2, y1, y2, rson);
}
} void query()
{
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2); printf("%d\n", queryX(x1, x2, y1, y2, 0, size - 1, 1));
} void (*funArr[])() = {
getSize, update, query
}; int main()
{
int num;
while(scanf("%d", &num), num != 3)
(*funArr[num])();
return 0;
}
POJ1195 Mobile phones 【二维线段树】的更多相关文章
- poj 1195:Mobile phones(二维线段树,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14391 Accepted: 6685 De ...
- POJ1195 二维线段树
Mobile phones POJ - 1195 Suppose that the fourth generation mobile phone base stations in the Tamper ...
- UVA 11297 线段树套线段树(二维线段树)
题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要 不同的处理方式,非叶子形成的 ...
- POJ2155 Matrix二维线段树经典题
题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...
- HDU 1823 Luck and Love(二维线段树)
之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...
- poj 2155:Matrix(二维线段树,矩阵取反,好题)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17880 Accepted: 6709 Descripti ...
- POJ 2155 Matrix (二维线段树)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17226 Accepted: 6461 Descripti ...
- HDU 4819 Mosaic (二维线段树)
Mosaic Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total S ...
- HDU 4819 Mosaic --二维线段树(树套树)
题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的 ...
随机推荐
- VS2010/2012Web负载测试
原文地址:http://www.cnblogs.com/aarond/archive/2013/04/18/performance.html VS自带的Web负载测试真的很大程度上能和专业的loadr ...
- Hihocoder 1634 Puzzle Game(2017 ACM-ICPC 北京区域赛 H题,枚举 + 最大子矩阵变形)
题目链接 2017 Beijing Problem H 题意 给定一个$n * m$的矩阵,现在可以把矩阵中的任意一个数换成$p$,求替换之后最大子矩阵的最小值. 首先想一想暴力的方法,枚举矩阵中 ...
- linux命令和工具
环境搭建 lnmp环境搭建 命令 uname -a 查看linux版本 lsof -i:80 查看端口被那个程序占用 lsof -p pid号 查看引用的文件 netstat -apn|grep 80 ...
- NOI2016 高中OI生涯的最后一站
你乘坐的航班XXX已经抵达终点站——四川绵阳. “呼——”机舱外的天空灰沉沉的,不禁有些压抑与紧张. 一出机场,就看见南山中学的牌子,黄色衣服的志愿者们,还有热情的老师们. 感觉刚才的情绪又一扫而空了 ...
- ARM常用汇编指令介绍
b 跳转指令(跳转范围为32Mb) bl 带返回地址的跳转,指令自动将下一条指令的地址复制到R14寄存器,然后跳转到指定地址去执行,执行完后返回到下一条指令处执行 pc 寄存器R1 ...
- 设计模式之桥接模式(php实现)
github地址:https://github.com/ZQCard/design_pattern /** * 桥接模式 * 优点: * 1.分离抽象接口及其实现部分.提高了比继承更好的解决方案. * ...
- 排序效率小PK
上个小demo看看 import com.google.common.primitives.Chars; import java.util.*; /** * java中数组转List使用Arrays. ...
- elasticsearch新加入节点不能识别问题
向ES集群中新加入节点,配置文件也没有什么问题,但是就是加不进去,这时候就需要检查一下防火墙是否开启.关闭即可
- 2017.4.19 慕课网-通过自动回复机器人学习mybatis
开发前的分析 1.技能前提 JSP JSTL EL JS/JQUERY Servlet JavaBean JDBC(后期再用mybatis,这样体会更深) MYSQL 2.需求分析和模块划分 (1)基 ...
- git学习资料及心得
1. 阮一峰的(简单易懂,实用性佳) http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html http://www.ruanyifeng ...