Mobile phones
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
题意: 在一个矩阵中, 更新矩阵的值和统计子矩阵的和.
解题思路:
1. 从百度文库里面学会了, 树状数组是神马一回事.
树状数组使用分析:
(1). 用途1:给定一维序列, 动态的在一些位置上修改数字, 加上/减去/乘上一个数, 然后动态
地提出问题, 问题形式有求出一段数字的和之类.
用途2:给定二维序列的矩阵, 一样是动态地修改某些位置, 然后求出子矩阵的和之类.
(2). 借用百科的图
设一个数组C[i]表示图片上面的2,4,6,...,16的节点.
C1 = A1
C2 = A1 + A2
C3 = A3
C4 = A1 + A2 + A3 + A4
C5 = A5
C6 = A5 + A6
C7 = A7
C8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8
...
C16 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 + A9 + A10 + A11 + A12 + A13 + A14 + A15 + A16
其实可以看成C[16] = C[8]+C[12]+C[14]+A[15]+A[16].
C[n] = A[n-2^k+1]+...+A[n];
k为n二进制时末尾0的个数.
一个快捷的求法: n & (n异或(n-1)) 或则 n & (-n);
另外加上一条常用加法: sum[j]-sum[i] = A[j]+A[j-1]+...+A[i];
2. 二维树状数组:一个由数字构成的大矩阵,能进行两种操作
(1) 对矩阵里的某个数加上一个整数(可正可负)
(2) 查询某个子矩阵里所有数字的和
要求对每次查询,输出结果
以样例为一个例子: 1 1 2 3 全部坐标+1 ==> 2 2 3 4
x1 = 2, y1 = 2, x2 = 3, y2 = 4
红色部分是求的, 因为用树状数组求的是最大一个椭圆, 而且要减去黄色部分, 黑色是
重复减了2次的部分.
result = getSum(x2, y2)-getSum(x1-1, y2)-getSum(x2, y1-1)+getSum(x1-1,y1-1);
值得我继续研究.
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 1050
int n;
int c[MAX][MAX];
int order;
int x1, y1, x2, y2, A;
int result;
inline int lowbit(int a)
{
return a & (a^(a-1));
}
void update(int x, int y, int val)
{
for(int i = x; i <= n; i += lowbit(i))
{
for(int j = y; j <= n; j += lowbit(j))
c[i][j] += val;
}
}
int getSum(int x, int y)
{
int sum = 0;
for(int i = x; i > 0; i -= lowbit(i))
{
for(int j = y; j > 0; j -= lowbit(j))
sum += c[i][j];
}
return sum;
}
int main()
{
// freopen("input.txt","r",stdin);
scanf("%d %d",&order, &n);
memset(c, 0, sizeof(c));
while(scanf("%d", &order) != EOF)
{
if(order == 3) break;
if(order == 1)
{
scanf("%d %d %d",&x1, &y1, &A);
x1++, y1++;
update(x1, y1, A);
}
else if(order == 2)
{
scanf("%d %d %d %d",&x1, &y1, &x2, &y2);
x1++, y1++, x2++, y2++;
result = getSum(x2, y2)-getSum(x1-1, y2)-getSum(x2, y1-1)+getSum(x1-1,y1-1);
printf("%d\n",result);
}
}
return 0;
}