-
Notifications
You must be signed in to change notification settings - Fork 0
/
11658_fenwick.cpp
60 lines (59 loc) · 1.05 KB
/
11658_fenwick.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 210310 #11658 ±¸°£ ÇÕ ±¸Çϱâ3 Platinum V
// Fenwick Tree practice
// 2D segtree -> fenwick (query sum)
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 1025;
int a[MAX][MAX], tree[MAX][MAX], N, M;
int query(int x, int y) {
int ret = 0, r = x;
while (r) {
int c = y;
while (c) {
ret += tree[r][c];
c -= (c & -c);
}
r -= (r & -r);
}
return ret;
}
void update(int x, int y, int val) {
int r = x;
while (r <= N) {
int c = y;
while (c <= N) {
tree[r][c] += val;
c += (c & -c);
}
r += (r & -r);
}
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> N >> M;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
cin >> a[i][j];
update(i, j, a[i][j]);
}
}
while (M--) {
int ch;
cin >> ch;
if (!ch) {
int r, c, k;
cin >> r >> c >> k;
update(r, c, k - a[r][c]);
a[r][c] = k;
}
else {
int r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
cout << query(r2, c2) - query(r2, c1 - 1) -
query(r1 - 1, c2) + query(r1 - 1, c1 - 1) << "\n";
}
}
}