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

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

Source

 
  二维线段树,矩阵求和,经典题
  这道题用二维树状数组做更快,更省空间,代码更简洁。
  二维树状数组做法见右方链接:poj 1195:Mobile phones(二维树状数组,矩阵求和)
 
  题意
  一个矩阵,初始化为全0。有以下操作:
  1)将 (x,y) 元素加 a。x为行坐标,y为列坐标。
  2)求矩阵 [X,Y] 所有元素的和。该矩阵左上角为[l,b],右下角为[r,t]。
 
  思路
  这道题是一道经典的二维线段树的题,由于是区间求和,所以用二维树状数组也可以实现,而且更快,代码更少。
  二维线段树的实现方法有两种:树套树,四叉树。树套树常用一些。
  这道题的我是用树套树实现。
  二维线段树的每一个节点代表一个矩阵,在这道题里,节点存储的是这个矩阵的元素和,用一个二维数组int tree[][]存储所有不同矩阵的和。
  例:如果矩阵大小为4*4,则tree[1][2]代表,左上角坐标为[1,1],右下角坐标为[4,2]的一个矩阵。1代表1-4这个区间,即第一行到第四行;2代表1的左节点,1-2这个区间,即第一列到第二列。取交集就是这样一个矩阵。
  tree[1][2]的结果就是这个矩阵所有元素的和。
 
  附加一个 c源代码+20组测试数据 的压缩包下载地址:
 
  其加数,求和操作见代码
 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; #define MAXN 1100 int tree[MAXN*][MAXN*],s; void Add_y(int d,int dy,int L,int R,int y,int a) //加数列操作
{
tree[d][dy] += a; //相关矩阵全部加a if(L==R){
return ;
} int mid = (L+R)>>;
if(mid>=y)
Add_y(d,dy<<,L,mid,y,a);
else
Add_y(d,dy<<|,mid+,R,y,a);
} void Add_x(int d,int L,int R,int x,int y,int a) //加数行操作
{
Add_y(d,,,s,y,a); //每一个行块都要更新其对应的列块,这样才能将所有与(x,y)相关的矩阵的值更新 if(L==R){
return ;
} int mid = (L+R)>>;
if(mid>=x)
Add_x(d<<,L,mid,x,y,a);
else
Add_x(d<<|,mid+,R,x,y,a);
} int Sum_y(int d,int dy,int L,int R,int b,int t)
{
if(L==b && R==t) //找到要找的矩阵,输出这个矩阵对应的值
return tree[d][dy]; //没找到
int mid = (L+R)>>;
if(mid >= t)
return Sum_y(d,dy<<,L,mid,b,t);
else if(mid < b)
return Sum_y(d,dy<<|,mid+,R,b,t);
else
return Sum_y(d,dy<<,L,mid,b,mid) + Sum_y(d,dy<<|,mid+,R,mid+,t);
} int Sum_x(int d,int L,int R,int l,int r,int b,int t)
{
if(L==l && R==r){ //找到要找的行块,继续查找列块
return Sum_y(d,,,s,b,t);
} //没找到
int mid = (L+R)>>;
if(mid >= r)
return Sum_x(d<<,L,mid,l,r,b,t);
else if(mid < l)
return Sum_x(d<<|,mid+,R,l,r,b,t);
else
return Sum_x(d<<,L,mid,l,mid,b,t) + Sum_x(d<<|,mid+,R,mid+,r,b,t);
} int main()
{
int cmd,x,y,a,l,r,b,t; while(scanf("%d",&cmd)!=EOF){
switch(cmd){
case : //初始化矩阵
scanf("%d",&s);
memset(tree,,sizeof(tree));
break; case : //加数
scanf("%d%d%d",&x,&y,&a);
Add_x(,,s,x+,y+,a);
break; case : //求矩阵和
scanf("%d%d%d%d",&l,&b,&r,&t);
printf("%d\n",Sum_x(,,s,l+,r+,b+,t+));
break; case : //退出程序
return ;
default:
break;
}
}
return ;
}

Freecode : www.cnblogs.com/yym2013

poj 1195:Mobile phones(二维线段树,矩阵求和)的更多相关文章

  1. poj 1195 Mobile phones(二维树状数组)

    树状数组支持两种操作: Add(x, d)操作:   让a[x]增加d. Query(L,R): 计算 a[L]+a[L+1]……a[R]. 当要频繁的对数组元素进行修改,同时又要频繁的查询数组内任一 ...

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

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

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

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

  4. POJ 2155 Matrix【二维线段树】

    题目大意:给你一个全是0的N*N矩阵,每次有两种操作:1将矩阵中一个子矩阵置反,2.查询某个点是0还是1 思路:裸的二维线段树 #include<iostream>#include< ...

  5. poj 1195:Mobile phones(二维树状数组,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 De ...

  6. POJ1195 Mobile phones 【二维线段树】

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

  7. poj 2155 matrix 二维线段树 线段树套线段树

    题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...

  8. POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)

    题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...

  9. POJ 2155 二维线段树 经典的记录所有修改再统一遍历 单点查询

    本来是想找一个二维线段树涉及懒惰标记的,一看这个题,区间修改,单点查询,以为是懒惰标记,敲到一半发现这二维线段树就不适合懒惰标记,你更新了某段的某列,但其实其他段的相应列也要打标记,但因为区间不一样, ...

随机推荐

  1. HDU 2853 最大匹配&KM模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2853 这道题初看了没有思路,一直想的用网络流如何解决 参考了潘大神牌题解才懂的 最大匹配问题KM 还需要一些技巧 ...

  2. Vijos 1055 奶牛浴场

    Description 求一个不覆盖指定点的最大子矩阵,\(n,m \leqslant 3\times 10^5,S \leqslant 5\times 10^3\) . Sol 没有名字的算法都叫x ...

  3. ubuntu14 谷歌输入法

    sudo apt-get install ibus-googlepinyin 装完重启即可: (在右上角语言处右键,添加text entry)

  4. BurpSuite使用设置

    一.设置字体 二.设置字符集 三.设置浏览器代理 四.BurpSuite访问步骤 五.在Target中可以查看截获的数据包

  5. C++ 输出调试的一些技巧

    主要利用了宏和stderr... #define enable_debug #ifdef enable_debug FILL some macros/functions here #else /// ...

  6. IN和EXISTS的详解

    从效率来看: 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; T1数据量小而T2数据量非常大时,T1<& ...

  7. 【leetcode】Unique Binary Search Trees

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

  8. poj 2240(floyd)

    http://poj.org/problem?id=2240 题意:有些人会利用货币的不用汇率来进行套现,比如1美元换0.5英镑,而1英镑又可以换10法郎,而1法郎又可以换0.21的美元,那么经过货币 ...

  9. oracle11g 连接问题

    一.The Network Adapter could not establish the connection  状态: 失败 -测试失败: IO 错误: The Network Adapter c ...

  10. ios NSURLSession completeHandler默认调用quque

    注意 , [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSU ...