Mobile phones
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 21926 | Accepted: 10200 |
Description
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 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
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
Source
题目大意:
有四种操作:
0 -- N 初始化一个 N*N 的矩阵;
1 -- X -- Y -- Value 在矩阵的点(X,Y)加上 Value;
2 -- X_1-- Y_1 -- X_2 -- Y_2 查询左上角坐标为(X_1, Y_1) && 右下角坐标为(X_2, Y_2) 子矩阵的和
3 结束程序
大概思路:
使用二维树状数组模拟,需要注意的是
①题目下标从0开始,但是树状数组下标从1开始
②因为维护的是前缀和,所以求子矩阵的和等于删掉上边的和左边的再加上重复删掉的部分
AC code(4556K 516MS):
1 #include <map> 2 #include <set> 3 #include <list> 4 #include <stack> 5 #include <queue> 6 #include <vector> 7 #include <math.h> 8 #include <bitset> 9 #include <stdio.h> 10 #include <cstring> 11 #include <climits> 12 #include <iostream> 13 #include <algorithm> 14 #define INF 0x3f3f3f3f 15 #define ll long long int 16 using namespace std; 17 18 const int MAXN = 1100; 19 int c[MAXN][MAXN]; 20 int N; 21 22 int lowbit(int x) 23 { 24 return x&(-x); 25 } 26 27 void add(int x, int y, int value) 28 { 29 for(int i = x; i <= N; i+=lowbit(i)) 30 for(int j = y; j <= N; j+=lowbit(j)) 31 c[i][j]+=value; 32 } 33 34 int sum(int x, int y) 35 { 36 int res = 0; 37 for(int i = x; i > 0; i-=lowbit(i)) 38 for(int j = y; j > 0; j-=lowbit(j)) 39 res+=c[i][j]; 40 return res; 41 } 42 43 void MEM() 44 { 45 for(int i = 0; i <= N; i++) 46 for(int j = 0; j <= N; j++) 47 c[i][j] = 0; 48 } 49 int main() 50 { 51 ll ans; 52 int com; 53 int xi, yi, v, x1, x2, y1, y2; 54 while(~scanf("%d", &com)) 55 { 56 if(com == 3) break; 57 else if(com == 0) 58 { 59 scanf("%d", &N); 60 MEM(); 61 } 62 else if(com == 1) 63 { 64 scanf("%d%d%d", &xi, &yi, &v); 65 add(xi+1, yi+1, v); 66 } 67 else if(com == 2) 68 { 69 scanf("%d%d%d%d", &x1, &y1, &x2, &y2); 70 x1++, y1++, x2++, y2++; 71 ans = (sum(x2, y2) + sum(x1-1, y1-1) - sum(x2, y1-1) - sum(x1-1, y2)); 72 printf("%lld\n", ans); 73 } 74 } 75 return 0; 76 }
4403



被折叠的 条评论
为什么被折叠?



