CSU-ACM2017暑期训练16-树状数组 F - Matrix Sum HihoCoder - 1336
2017-08-14 22:26
411 查看
F - Matrix Sum
You are given an N × N matrix. At the beginning every element is 0. Write a program supporting 2 operations: 1. Add x y value: Add value to the element Axy. (Subscripts starts from 0 2. Sum x1 y1 x2 y1: Return the sum of every element Axy for x1 ≤ x ≤ x2, y1 ≤ y ≤ y2.
Input
The first line contains 2 integers N and M, the size of the matrix and the number of operations. Each of the following M line contains an operation. 1 ≤ N ≤ 1000, 1 ≤ M ≤ 100000 For each Add operation: 0 ≤ x < N, 0 ≤ y < N, -1000000 ≤ value ≤ 1000000 For each Sum operation: 0 ≤ x1 ≤ x2 < N, 0 ≤ y1 ≤ y2 < N
Output
For each Sum operation output a non-negative number denoting the sum modulo 109+7.
Sample Input
5 8 Add 0 0 1 Sum 0 0 1 1 Add 1 1 1 Sum 0 0 1 1 Add 2 2 1 Add 3 3 1 Add 4 4 -1 Sum 0 0 4 4
Sample Output
1 2 3
套用二位树状数组的求和、更新函数进行模拟即可。注意输入的下标需要加一,以免对树状数组下标为零的元素进行操作。
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <cstring> #include <queue> using namespace std; const int MOD = 1e9+7, maxn = 1004; int n, m; int c[maxn][maxn], a[maxn][maxn]; void add(int x, int y, int d){ for(int i = x; i <= n; i += i&-i) for(int j = y; j <= n; j += j&-j) c[i][j] = (c[i][j]+d) % MOD; } int sum(int x, int y){ int ret = 0; for(int i = x; i > 0; i -= i&-i) for(int j = y; j > 0; j -= j&-j) ret = (ret+c[i][j]) % MOD; return ret; } int main(){ #ifdef TEST freopen("test.txt", "r", stdin); #endif // TEST char ord[4]; int x, y, xx, yy, s; while(cin >> n >> m){ memset(c, 0, sizeof(c)); for(int i = 0; i < m; i++){ scanf("%s", ord); if(*ord == 'A'){ scanf("%d%d%d", &x, &y, &s); add(++x, ++y, s); } if(*ord == 'S'){ int ans; scanf("%d%d%d%d", &x, &y, &xx, &yy); x++, y++, xx++, yy++; ans = ((sum(xx,yy) - sum(xx,y-1) - sum(x-1,yy) + sum(x-1,y-1))%MOD+MOD)%MOD; printf("%d\n",ans); } } } return 0; }
相关文章推荐
- CSU-ACM2017暑期训练16-树状数组 H - Bubble Sort HDU - 5775
- CSU-ACM2017暑期训练16-树状数组 A - Ultra-QuickSort POJ - 2299
- CSU-ACM2017暑期训练16-树状数组 C - 按钮控制彩灯实验 CSU - 1770
- CSU-ACM2017暑期训练16-树状数组 B - Stars POJ - 2352
- CSU-ACM2017暑期训练16-树状数组 D - 斜率小于0的连线数量 51Nod - 1107
- CSU-ACM2017暑期训练16-树状数组 G - KiKi's K-Number HDU - 2852
- CSU-ACM2017暑期训练1-Debug与STL Jury Marks-set判重
- CSU-ACM2017暑期训练4-dfs E - The Settlers of Catan POJ - 2258
- Train Problem I CSU-ACM2017暑期训练1-Debug与STL
- CSU-ACM2017暑期训练4-dfs F - Twenty-four point CSU - 1600
- CSU-ACM2017暑期训练5-三分 E - Line belt HDU - 3400
- CSU-ACM2017暑期训练8-动态规划初步 F - Boredom
- CSU-ACM2017暑期训练12-KMP G - Next[]树 CodeForces - 432D
- hihoCoder 1336:Matrix Sum(二维树状数组)
- CSU-ACM2017暑期训练3-递推与递归I - Erratic Expansion UVA - 12627
- CSU-ACM2017暑期训练1-Debug与STL C-Languages-map判重
- CSU-ACM2017暑期训练1-Debug与STL A - Surprising Strings
- CSU-ACM2017暑期训练1-Debug与STL E - Train Problem I
- Function Run Fun--CSU-ACM2017暑期训练3-递推与递归
- CSU-ACM2017暑期训练3-递推与递归 G - 展开字符串 HDU - 1274