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 免密码登录,修改密码及忘记密码操作

    ----免密码登陆 方式一 my.cnf增加[client]标签 [client] user="root" password="你的密码" 单对定义不同的客户端 ...

  2. C#中图片.BYTE[]和base64string的转换

    在C#中 图片到byte[]再到base64string的转换: Bitmap bmp = new Bitmap(filepath);                MemoryStream ms = ...

  3. React常用方法手记

    1.Reactjs 如何获取子组件的key值?请问antd中table自定义列render方法怎么获取当前第几列? https://segmentfault.com/q/101000000453235 ...

  4. H2 应用实例1

    说明:本例子开发工具为NetBeans,jdk 1.7 进行测试说明 H2安装说明如下 1. H2数据库必要文件下载地址为: http://www.h2database.com     (1) 下载截 ...

  5. OIer同样是音乐家

    烦闷的时候,shenben为大家准备了2首歌(不用耳机也能听哦) 只需把代码复制到dev-c++的编辑器上,轻按F11,然后聆听OIer的音乐…… 千本樱 曲谱 #include <cstdio ...

  6. 【BZOJ3671】[Noi2014]随机数生成器 暴力

    [BZOJ3535][Noi2014]随机数生成器 Description Input 第1行包含5个整数,依次为 x_0,a,b,c,d ,描述小H采用的随机数生成算法所需的随机种子.第2行包含三个 ...

  7. python的接口类的思考?

    1.java怎么实现多继承的功效:https://www.cnblogs.com/Berryxiong/p/6142735.html 2.python的接口类和抽象类:https://www.cnbl ...

  8. Android Studio第一次启动失败的解决办法

    Android Studio Android 开发环境 由于GFW的问题,安装后第一次启动会在显示Fetching android sdk component information对话框后,提示错误 ...

  9. zabbix haproxy 监控

    摘自: http://www.tuicool.com/articles/JrYNNrm 写的非常好,步步紧逼,环环相扣.直到成功! 文章首发站点:OpensGalaxy 这是一个HAProxy的zab ...

  10. 深入理解JVM - 垃圾收集器与内存分配策略 - 第三章

    引用计数算法——判断对象是否存活的算法 很多教科书判断对象是否存活的算法是这样的:给对象添加一个引用计数器,每当一个地方引用它时,计数器值就加1:当引用失效时,计数器值就减1:任何时刻计数器为0的对象 ...