Mobile phones
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 17176   Accepted: 7920

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"cstdio"
#include"cstring"
using namespace std;
const int MAXN=+;
int bit[MAXN][MAXN];
int lowbit(int i)
{
return i&(-i);
}
void add(int x,int y,int a)
{
for(int i=x;i<MAXN;i+=lowbit(i))
for(int j=y;j<MAXN;j+=lowbit(j))
bit[i][j]+=a;
}
long long sum(int x,int y)
{
long long s=;
for(int i=x;i>;i-=lowbit(i))
for(int j=y;j>;j-=lowbit(j))
s+=bit[i][j];
return s;
}
long long getSum(int x1,int y1,int x2,int y2)
{
return sum(x2,y2)+sum(x1-,y1-)-sum(x2,y1-)-sum(x1-,y2);
}
int main()
{
int Init;
while(scanf("%d",&Init)!=EOF)
{
int n;
if(Init==)
{
scanf("%d",&n);
memset(bit,,sizeof(bit));
}
else if(Init==)
{
int x,y,a;
scanf("%d%d%d",&x,&y,&a);
x++,y++;
add(x,y,a);
}
else if(Init==)
{
int L,B,R,T;
scanf("%d%d%d%d",&L,&B,&R,&T);
L++,B++,R++,T++;
printf("%I64d\n",getSum(L,B,R,T));
}
else
{
break;
}
} return ;
}

POJ1195(二维树状数组)的更多相关文章

  1. poj1195二维树状数组模板

    二维树状数组和一维的也差不多,改一下add和query函数即可:即按行修改,行内单点修改即可 /* 二维树状数组,询问一个二维区间内的数之和 */ #include<iostream> # ...

  2. 二维树状数组poj1195

    题目链接:https://vjudge.net/problem/POJ-1195 题意:一开始输入0和一个s,0代表开始,s代表这是一个s*s的图,接下来会输入1或2,1代表进行单点修改,后面会接3个 ...

  3. poj1195(二维树状数组)

    题目链接:https://vjudge.net/problem/POJ-1195 题意:有s*s的矩阵,初始化为全0,有两种操作,单点修改(x,y)的值,区间查询(x,y)的值(l<=x< ...

  4. POJ-1195 Mobile phones---裸的二维树状数组(注意下标从1,1开始)

    题目链接: https://vjudge.net/problem/POJ-1195 题目大意: 直接维护二维树状数组 注意横纵坐标全部需要加1,因为树状数组从(1,1)开始 #include<c ...

  5. 二维树状数组(水题) POJ1195

    前段时间遇到线段树过不了,树状数组却过了的题.(其实线段树过得了的) 回忆了下树状数组. 主要原理,还是二进制位数,每一项的和表示其为它的前((最后一位1及其后)的二进制数)和,可从二进制图来看.(用 ...

  6. 【POJ1195】【二维树状数组】Mobile phones

    Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...

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

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

  8. 二维树状数组 BZOJ 1452 [JSOI2009]Count

    题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...

  9. HDU1559 最大子矩阵 (二维树状数组)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)  ...

随机推荐

  1. mysql 相关博客地址

    MySQL概念学习与逐步上手操作系列(一套完整)   https://www.cnblogs.com/zlslch/category/962962.html

  2. ASP.NET MVC 4 技术讲解

    ASP.NET MVC 相关的社群与讨论区 Routing 与 ASP.NET MVC 生命周期 Model相关技术 Controller相关技术 View数据呈现相关技术 Area区域相关技术 AS ...

  3. GS踢玩家下线功能

    GS踢玩家下线功能 //key:userId, val:nChannelId (当前在线用户) std::map<int, int> m_mapOnLineUserByUid; ///&l ...

  4. Elasticsearch基本语法

    match和match_phrase区别 match: 索引中只要有任意一个匹配拆分后词就可以出现在结果中,只是匹配度越高的排越前面 match_phrase: 索引中必须同时匹配拆分后词就可以出现在 ...

  5. DAICO模式到底是什么?

    自从V神提出DAICO以来,已经有项目围绕DAICO模式落地,但是DAICO到底是什么呢? 所谓DAIC0,其实就是DAO+IC0. DAO是DistributedAutonomous Organiz ...

  6. <raspberry pi > 用树莓派来听落网电台

    树莓派放在抽屉里吃灰有半年多了,去年玩了1个月后就没怎么开整了,上个月没工作,刚好有点闲暇,就把树莓派翻出来折腾,刚好碰到落网改版了,想起以前在树莓派论坛看到有网友拿树莓派来听豆瓣电台,代码那时我都下 ...

  7. SlopeOne推荐算法

           Slope One 算法 是一种基于评分的预测算法, 本质上也是一种基于项目的算法.与一般的基于项目的算法不同, 该算法不计算项目之间的相似度, 而是用一种简单的线性回归模型进行预测(可 ...

  8. Zookeeper四字命令

    ZooKeeper 支持某些特定的四字命令(The Four Letter Words)与其进行交互.它们大多是查询命令,用来获取 ZooKeeper 服务的当前状态及相关信息.用户在客户端可以通过 ...

  9. JAVA源码分析------锁(1)

    http://870604904.iteye.com/blog/2258604 第一次写博客,也就是记录一些自己对于JAVA的一些理解,不足之处,请大家指出,一起探讨. 这篇博文我打算说一下JAVA中 ...

  10. 《机器学习实战》学习笔记第七章 —— AdaBoost元算法

    主要内容: 一.提升方法与AdaBoost算法的简介 二.AdaBoost算法 三.代码解释 一.提升方法与AdaBoost算法的简介 1.提升方法:从弱学习算法出发,反复学习,得到一系列弱分类器(又 ...