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

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]。
 
  思路
  同一维的树状数组求法差不多。
  分别说明二维树状数组加数和求和的做法:
  1)加数:在矩阵的某个坐标处加上某个数,则二维树状数组中的值也要改变。那么改变哪些位置的值呢?我们知道,一维的加数是不断改变在这个位置之上的所有位置的值,之上的位置是用lowbit()求出来的。例如要改变的位置是x,你要加的数是a,则需要把x不断加上lowbit(),在新的x的位置加上a,然后再将x加lowbit(),再在x上加a……直到x超过这个数组规定的大小。
  二维的也是类似,只不过在计算每一个x的时候,需要再计算一个y。例如要在(x,y)位置加上一个数a。先保持x不变,y不断加lowbit(),在对应的(x,y)的位置加a,直到y超过限界(如果矩阵是4*4的,则这个限界就是4,y<=4)。一轮过后,将x加lowbit(),y恢复一开始的值,再进行新的一轮,这样一轮一轮的直到x也超过限界(同样如果矩阵大小为4*4,则x<=4)。
  如此这般,假设矩阵大小为4*4,则Add的代码如下:
 void Add(int x,int y,int a)
{
int i=x;
while(i<=s){
int j=y;
while(j<=s){
c[i][j]+=a;
j+=lowbit(j);
}
i+=lowbit(i);
}
}

  2)求和:对左上角为(l,r),右下角为(b,t)的矩阵求和,即求该矩阵中所有元素的和。先求l到r的行矩阵的和,在求这个行矩阵和的时候,每一行要计算对应的b到t列的元素和。具体过程类似上述过程,将+lowbit()改为-lowbit()即可。限界为>=1。

  

  代码

 #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; #define MAXN 1100 int c[MAXN][MAXN],s; int lowbit(int x)
{
return x&-x;
} void Add(int x,int y,int a) //加数
{
int i=x;
while(i<=s){ //行
int j=y;
while(j<=s){ //列
c[i][j]+=a;
j+=lowbit(j);
}
i+=lowbit(i);
}
} int Sum(int l,int r,int b,int t) //求和
{
l--,b--;
int suml=,sumr=;
//求行矩阵和,l以上矩阵
while(l>=){
int i=b,j=t;
int sumb=,sumt=;
//求列矩阵和
while(i>=){
sumb+=c[l][i];
i-=lowbit(i);
}
while(j>=){
sumt+=c[l][j];
j-=lowbit(j);
}
suml+=sumt-sumb;
l-=lowbit(l);
}
//求行矩阵和,r以上矩阵
while(r>=){
int i=b,j=t;
int sumb=,sumt=;
//求列矩阵和
while(i>=){
sumb+=c[r][i];
i-=lowbit(i);
}
while(j>=){
sumt+=c[r][j];
j-=lowbit(j);
}
sumr+=sumt-sumb;
r-=lowbit(r);
}
return sumr-suml;
} int main()
{
int cmd,x,y,a,l,r,b,t; while(scanf("%d",&cmd)!=EOF){
switch(cmd){
case : //初始化矩阵
scanf("%d",&s);
memset(c,,sizeof(c));
break; case : //加数
scanf("%d%d%d",&x,&y,&a);
Add(x+,y+,a);
break; case : //求矩阵和
scanf("%d%d%d%d",&l,&b,&r,&t);
printf("%d\n",Sum(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 1195:Mobile phones 二维树状数组

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16893   Accepted: 7789 De ...

  3. 【poj1195】Mobile phones(二维树状数组)

    题目链接:http://poj.org/problem?id=1195 [题意] 给出一个全0的矩阵,然后一些操作 0 S:初始化矩阵,维数是S*S,值全为0,这个操作只有最开始出现一次 1 X Y ...

  4. POJ 2155 Matrix【二维树状数组+YY(区间计数)】

    题目链接:http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissio ...

  5. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  6. POJ 2155 Matrix (二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17224   Accepted: 6460 Descripti ...

  7. POJ 2155 Matrix 【二维树状数组】(二维单点查询经典题)

    <题目链接> 题目大意: 给出一个初始值全为0的矩阵,对其进行两个操作. 1.给出一个子矩阵的左上角和右上角坐标,这两个坐标所代表的矩阵内0变成1,1变成0. 2.查询某个坐标的点的值. ...

  8. POJ 2155 Matrix (二维树状数组)题解

    思路: 没想到二维树状数组和一维的比只差了一行,update单点更新,query求和 这里的函数用法和平时不一样,query直接算出来就是某点的值,怎么做到的呢? 我们在更新的时候不止更新一个点,而是 ...

  9. POJ 2155:Matrix 二维树状数组

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21757   Accepted: 8141 Descripti ...

  10. POJ 2155 Matrix(二维树状数组)

    与以往不同的是,这个树状数组是二维的,仅此而已 #include <iostream> #include <cstdio> #include <cstring> # ...

随机推荐

  1. MFC----任务管理器的制作

    首先建立一个MFC项目,因为进程有多个并且是动态的,所以可以看做链表,获得头结点&&依次向下遍历: 首先 我们使用CreateToolhelp32Snapshot提取出进程表,之后

  2. python 模块之间的变量共享

    才疏学浅,只知道两种方式: 1. 通过__builtin__实现: builtin1.py import __builtin__ __builtin__.some_global_var_among_m ...

  3. SqlBulkCopy 批量复制数据到数据表

    使用 SqlBulkCopy 类只能向 SQL Server 表写入数据.但是,数据源不限于 SQL Server:可以使用任何数据源,只要数据可加载到 DataTable 实例或可使用 IDataR ...

  4. bitnami-redmine 安装与插件使用

    bitnami-redmine 公司要进行敏捷开发管理,选择Redmine作为管理工具. 而Redmine本身的需要的环境比较麻烦,需要安装mysql,ruby,redmine,apach. Bitn ...

  5. OracleBulkCopy

    Oracle也有BulkCopy了,需要安装oracle 11g,并引用客户端下面的Oracle.DataAccess.dll 用法和SQLBulkCopy差不多 connStr 是 ORACLE 的 ...

  6. Pywinauto在Windows Twain Driver自动化测试中的应用研究

    摘  要: 以Python为基础,结合对Twain Driver测试工具的具体需求,将Pywinauto引入到Twain Driver的自动化测试中.介绍了Pywinauto的基本概念,通过测试用例说 ...

  7. centos6.5 tomcat开机启动

    可参考:centos6.5 nginx开机启动 /etc/init.d/下添加tomcatd文件,内容如下: #!/bin/sh # # chkconfig: - # # Licensed to th ...

  8. uml 推荐文章

    http://blog.csdn.net/zfrong/article/details/4086424 http://www.cnblogs.com/ywqu/archive/2009/12/14/1 ...

  9. win8开发wpf程序遇到的无语问题

    在设置wpf程序全屏后,点击某个listbox列表,发现程序下面出现了任务栏. 查找解决答案未果.仔细一想可能是win8系统的问题. 最后试着把listbox的滚动条去掉了,问题解决. 原因:当程序中 ...

  10. Android 启动白屏或者黑屏闪现解决

    1.设置Style //1.设置背景图Theme <style name="Theme.AppStartLoad" parent="android:Theme&qu ...