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. 2.实现Express中间件

    Express提供的大部分功能都是通过中间件函数完成,这些中间件函数在Node.js收到 请求的时点 和 发送响应的时点 执行 connect模块提供了中间件框剪 方便在全局或路径级别或为单个路由插入 ...

  2. zend framework2 入门实例代码album模型

    下载album模型 一.目录结构说明 - zf_project - config    - autoload      global.php    -- 数据库在这里配置      local.php ...

  3. linux 编程环境搭建过程记录

    1, 安装centos 7 最小版  过程略 ...... 2, 安装桌面安装yum groupinstall "GNOME Desktop" 更新系统运行级别ln -sf /li ...

  4. JavaWeb出现404一个很隐蔽的原因

  5. idea 排除编译文件,恢复编译

  6. sizeof和strlen的区别

    一.sizeof    sizeof(...)是运算符,而不是一个函数.    sizeof操作符的结果类型是size_t,在头文件中typedef为unsigned int,其值在编译时即计算好了, ...

  7. 页面加载时执行JQ代码

    $(function () { //jq加载时执行的这里面是 $("#ss").append("<strong>这是新加的</strong>&qu ...

  8. C#之设置无边框后如何移动窗体(转)

    转载:http://www.cnblogs.com/techmango/archive/2012/03/31/2427523.html 第一种,利用windows的消息机制来实现: 首先﹐.定义鼠標左 ...

  9. mysql查询当前正在使用数据库

    1.select database(): 2.status: 3.show tables:

  10. socket端口重复占用问题

    1.一个服务端进程在主动释放端口后(调用close)端口状态为TIME_WAIT,这时再去监听同样的端口,不论是否设置SO_REUSEADDR,都能监听成功,也能接收到客户端的连接,但是无法收到数据. ...