-
Notifications
You must be signed in to change notification settings - Fork 0
/
11658.cpp
74 lines (59 loc) · 1.41 KB
/
11658.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
const int MX = 1030;
struct FenwickTree{
int node[MX];
void Update(int n, int diff){
while(n < MX){
node[n] += diff;
n += (n & -n);
}
}
int Query(int n){
int ret = 0;
while(n){
ret += node[n];
n -= (n & -n);
}
return ret;
}
};
struct FenwickTree2D{
FenwickTree tree[MX];
void Update(int x, int y, int diff){
while(x < MX){
tree[x].Update(y, diff);
x += (x & -x);
}
}
int Query(int x, int y){
int ret = 0;
while(x){
ret += tree[x].Query(y);
x -= (x & -x);
}
return ret;
}
}tree2D;
int N, M, w, X1, Y1, X2, Y2, c, arr[MX][MX];
int main(){
cin.tie(nullptr), ios::sync_with_stdio(false);
cin >> N >> M;
for(int x = 1; x <= N; x++)
for(int y = 1; y <= N; y++){
cin >> arr[x][y];
tree2D.Update(x, y, arr[x][y]);
}
while(M--){
cin >> w;
if(w == 0){
cin >> X1 >> Y1 >> c;
int diff = c - arr[X1][Y1];
arr[X1][Y1] = c;
tree2D.Update(X1, Y1, diff);
}else{
cin >> X1 >> Y1 >> X2 >> Y2;
cout << (tree2D.Query(X2, Y2) - tree2D.Query(X1 - 1, Y2) - tree2D.Query(X2, Y1 - 1) + tree2D.Query(X1 - 1, Y1 - 1)) << "\n";
}
}
}