Mobile phones
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 18608   Accepted: 8621

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
【分析】第一次写树状数组,在网上学了会,发现很神奇,此题是二维的。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
const int mod=1e9+;
int n,m;
ll tree[N][N];
int lowbit(int x)
{
return x&(-x);
}
void add(int x,int y,ll num){
for(int i=x;i<=n;i+=lowbit(i)){
for(int j=y;j<=n;j+=lowbit(j)){
tree[i][j]+=num;
}
}
}
ll Sum(int x,int y){
ll sum=;
for(int i=x;i>;i-=lowbit(i)){
for(int j=y;j>;j-=lowbit(j)){
sum+=tree[i][j];
}
}
return sum;
}
int main() {
int x,y,l,b,r,t;
ll num;
while(){
scanf("%d",&m);
if(m==){
scanf("%d",&n);
met(tree,);
}
else if(m==){
scanf("%d%d%lld",&x,&y,&num);
add(x+,y+,num);
}
else if(m==){
scanf("%d%d%d%d",&l,&b,&r,&t);
l++;b++;t++;r++;
ll ans=Sum(r,t)-Sum(r,b-)-Sum(l-,t)+Sum(l-,b-);
printf("%lld\n",ans);
}
else break;
}
return ;
}

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. highcharts的.net本地导出环境安装记录

    由于项目中highcharts需要内网使用,需要本地搭建导出的环境.下面简述下步骤: 1.下载开源的.net导出文件:https://github.com/imclem/Highcharts-expo ...

  2. 基于K2 BPM平台,中原地产实现了从2个人到5万多人的跨越

    演讲人:吴付文 中原地产CIO 点击这里查看中原地产怎么使用BPM实现业绩的飞跃式发展.

  3. Android ViewPager 里有子ViewPager的事件冲突

    在Android应用中有时候要用到类似网易新闻左右滑动页面且页面里又有左右滑动的图片功能,我不知道网易是怎么实现的,本人的做法是外面的BaseFragmentActivity布局就是TabViewPa ...

  4. MATLAB axes

    本帖由MATLAB技术论坛(http://www.matlabsky.com)原创,更多精彩内容参见http://www.matlabsky.com axes ★★★★★ 功能 创建坐标系图形对象 语 ...

  5. struts2常见配置

    <struts> <!--开发模式设置:该属性设置Struts2应用是否使用开发模式.如果设置该属性为true,则可以在应用出错时显示更多.更友好的出错提示.该属性只接受true和f ...

  6. iOS 开发之推力动画效果

    步骤: 1.使用single view application 创建新的项目 2.在.h文件中需要遵守两个协议<UICollisionBehaviorDelegate,UIGestureReco ...

  7. Svn win7系统下状态图标不显示-转载

    Svn win7系统下状态图标不显示 Svn版本 tortoisesvn-1.8.8.25755-x64-svn-1.8.10.msi 2.不显示图标状态如图1,期望结果显示图标状态如图2 图1 图2 ...

  8. linux常用命令:1文件处理命令

    文件处理命令 1.命令格式 命令格式:命令 [-选项]  [参数] 例:ls  -la /etc 说明:1)个别命令使用不遵循此格式 2)档有多个选项时,可以写在一起 3)简化选项与完整选项 2.目录 ...

  9. ubuntu系统安装mongodb

    安装mongodb sudo apt-get install mongodb ... ... 设置客户端连接 打开文件/etc/mongodb.conf  注意权限 修改 bind_ip=127.0. ...

  10. 【LeetCode OJ】Word Ladder I

    Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in t ...