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

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 <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; #define MY_MAX 1100
int C[MY_MAX][MY_MAX];
int s; int lowbit(int x)
{
return x&(-x);
} void Add(int y,int x,int a)
{
while(y<=s)
{
int temp=x;
while(temp<=s)
{
C[y][temp]+=a;
temp +=lowbit(temp);
}
y +=lowbit(y);
}
} int QuerySum(int y,int x)
{
int nSum=0;
while(y>0)
{
int temp=x;
while(temp>0)
{
nSum += C[y][temp];
temp -= lowbit(temp);
}
y -=lowbit(y);
}
return nSum;
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout);
int oper;
int x,y,a,le,b,r,t; while(true)
{
scanf("%d",&oper);
if(oper==0)
{
scanf("%d",&s);
memset(C,0,sizeof(C));
}
else if(oper==1)
{
scanf("%d%d%d",&x,&y,&a);
Add(x+1,y+1,a);
}
else if(oper==2)
{
scanf("%d%d%d%d",&le,&b,&r,&t);
printf("%d\n",QuerySum(r+1,t+1) + QuerySum(le,b)- QuerySum(le,t+1) - QuerySum(r+1,b));
}
else if(oper==3)
{
break;
}
}
//system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

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. 【poj1195】Mobile phones(二维树状数组)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. HTML 5 <em> <strong> <dfn> <code> <samp> <kbd> <var> <cite> 标签

    <em> 呈现为被强调的文本. <strong> 定义重要的文本. <dfn> 定义一个定义项目. <code> 定义计算机代码文本. <samp ...

  2. 查漏补缺之Go的Strings, bytes, runes和字符

    字节遍历,字符遍历 https://play.golang.org/p/DeZcCN9aHXo package main import ( "fmt" "unicode/ ...

  3. freeswitch install

    https://freeswitch.org/confluence/display/FREESWITCH/CentOS+7+and+RHEL+7

  4. uni app中使用自定义图标库

    项目中难免会用到自定义图标,那在uni app中应该怎么使用呢? 首先, 将图标目录放在static资源目录下: 在main.js中引入就可以全局使用了 import '@/static/icon-o ...

  5. English_Rhymes_Phonics_resource

    English_Rhymes_Phonics_resource 1. 英语启蒙早有用吗?_英语启蒙 2. 26个英文字母背后的故事_英语启蒙 3. Phonics Song 4. 学Phonics前先 ...

  6. RTL级低功耗设计

    重点:门控时钟和操作数隔离 1.并行与流水的选择 并行与流水都是用面积换速度,但是有时可以降低功耗 并行处理常用于数字信号处理部分,采用并行处理,也已降低系统工作频率从而降低功耗 用两个乘法器取代原设 ...

  7. ch8 faux列

    在一个布局中,假设有导航元素和内容元素,切给他们都分别应用了背景,理想情况下,背景应该拉长到整个布局的最大高度,从而形成列的效果,但是实际上,因为导航元素没有扩展到最大高度,所以它们的背景不会拉长,如 ...

  8. 导弹拦截p1020(LIS问题)

    题目描述(题目链接:https://www.luogu.org/problem/P1020) 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够 ...

  9. 「CF5E」Bindian Signalizing

    传送门 Luogu 解题思路 很显然的一点,任何一条可能成为路径的圆弧都不可能经过最高的点,除非这条路径全是最高点. 所以我们先把最大值抠掉,把剩下的按原来的顺序排好. 从前往后.从后往前扫两次,用单 ...

  10. 078、Java数组之数组的引用传递

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...