Mobile phones
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 14291   Accepted: 6644

Description

Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The
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

The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter
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

Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard 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 【二维线段树】的更多相关文章

  1. poj 1195:Mobile phones(二维线段树,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 De ...

  2. POJ1195 二维线段树

    Mobile phones POJ - 1195 Suppose that the fourth generation mobile phone base stations in the Tamper ...

  3. UVA 11297 线段树套线段树(二维线段树)

    题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要  不同的处理方式,非叶子形成的 ...

  4. POJ2155 Matrix二维线段树经典题

    题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...

  5. HDU 1823 Luck and Love(二维线段树)

    之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...

  6. poj 2155:Matrix(二维线段树,矩阵取反,好题)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17880   Accepted: 6709 Descripti ...

  7. POJ 2155 Matrix (二维线段树)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17226   Accepted: 6461 Descripti ...

  8. HDU 4819 Mosaic (二维线段树)

    Mosaic Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total S ...

  9. HDU 4819 Mosaic --二维线段树(树套树)

    题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的 ...

随机推荐

  1. POJ 2528.Mayor's posters-线段树(成段替换、离散数据、简单hash)

    POJ2528.Mayor's posters 这道题真的是线段数的经典的题目,因为数据很大,直接建树的话肯定不可以,所以需要将数据处理一下,没有接触离散化的时候感觉离散化这个东西相当高级,其实在不知 ...

  2. hdu6038

    hdu6038 分析 求函数 \(f\) 的构成方案,\(f\) 确定下来后,\(f\) 和 \(b\) 的值也是一一对应的了( \(f(i)=b_{f(a_i)}\) ),观察 \(a\) 数组,代 ...

  3. #424 Div2 E

    #424 Div2 E 题意 给出一个 n 个数的数列,从前往后取数,如果第一个数是当前数列的最小值,则取出,否则将它放到数列尾端,问使数列为空需要多少步操作. 分析 用数据结构去模拟. 线段树维护区 ...

  4. Xamarin XAML语言教程Progress属性设置进度条进度

    Xamarin XAML语言教程Progress属性设置进度条进度 在图12.19~12.21中我们看到的是没有实现加载的进度条,即进度条的当前进度为0,如果开发者想要修改当前进度,可以使用两种方式: ...

  5. C++指针、引用知多少?

    上午搞了一个小程序,test半天都没有得到想要的结构,原来是递归的时候没有注意的循环的问题,结果直接死循环了.催了...看来当程序出现问题的时候,首先要整理的是算法思路是否有问题,其次是算法的实现,是 ...

  6. Oracle数据库任何用户密码都能以sysdba角色登入

    * 本文相关环境:Windows 10,64位操作系统:Oracle 11gR2:toad for Oracle12.1 最近在学习Oracle数据库,使用Toad for Oracle来查看数据库的 ...

  7. 1· linux命令:查看依赖的共享库 - ldd命令

    今天使用qt链接mysql的时候,发现提示driver not load 而出现此错误一般是没有mysql驱动的原因,但是qt5.4已经提供了mysql驱动的 查看plugins/sqldrivers ...

  8. ocmock

  9. Android自定义View(二)

    前言 魅族手机的闹钟应用中有个倒计时,这个控件还是蛮有趣的.左边是魅族闹钟,右边是我们最终实现的效果,虽然有些细节还需优化,不过基本上已经达到了想要的效果,我们先来就来看看如何实现吧. 分析 确定宽高 ...

  10. 自动生成logo的网址

    1.https://www.logaster.com/logo/