对应 POJ 题目:点击打开链接
Mobile phones
Time Limit: 5000MS |
|
Memory Limit: 65536K |
Total Submissions: 17403 |
|
Accepted: 8037 |
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
题意:
有 3 个操作,(0, s) 表示一个 s*s 的矩阵(此操作在一个例子中只出现一次);(1, x, y, val) 表示把 (x, y) 坐标的值加上 val;(2, x1, y1, x2, y2) 表示求以 (x1, y1) 坐标和 (x2, y2) 坐标为对角的矩阵的元素和;(3) 表示结束(此操作在一个例子中只出现一次)。
思路:
二维树状数组(基础:点击打开链接 (点进拉到后面有翻译) 或点击打开链接),很裸的题了,切记下标不能从 0 开始。二维树状数组中的 sum(x, y) 表示以(1, 1) 坐标和 (x, y) 坐标为对角的矩阵的元素和。所以要求以 (x1, y1) 坐标和 (x2, y2) 坐标为对角的矩阵的元素和可以这样:ans = sum(x2, y2) - sum(x1-1, y2) - sum(x2, y1-1) + sum(x1-1, y1-1)。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1105
#define max_x 1100
#define max_y 1100
int c[N][N];
int lowbit(int x)
{
return (x & (-x));
}
void update(int x, int y, int val)
{
while(x <= max_x){
int yt = y;
while(yt <= max_y){
c[x][yt] += val;
yt += lowbit(yt);
}
x += lowbit(x);
}
}
int query(int x, int y)
{
int s = 0;
while(x > 0){
int yt = y;
while(yt > 0){
s += c[x][yt];
yt -= lowbit(yt);
}
x -= lowbit(x);
}
return s;
}
int main()
{
#if 0
freopen("in.txt","r",stdin);
#endif
int od, squares;
scanf("%d%d", &od, &squares);
while(scanf("%d", &od) == 1){
if(od == 1){
int x, y, val;
scanf("%d%d%d", &x, &y, &val);
update(x+1, y+1, val);
}
else if(od == 2){
int sum;
int x1, y1, x2, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
sum = query(x2+1, y2+1) - query(x1, y2+1) - query(x2+1, y1) + query(x1, y1);
printf("%d\n", sum);
}
else if(od == 3){
break;
}
}
return 0;
}