-
Notifications
You must be signed in to change notification settings - Fork 0
/
fenwick-tree-2D.cpp
95 lines (87 loc) · 2.09 KB
/
fenwick-tree-2D.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(false); cin.tie(NULL);
#define ll long long
#define int long long
#define endl "\n"
#define INF (long long)1e15
#define debug(x) cout<<#x<<" "<<x<<endl
#define print(x) cout<<x<<endl
#define printc(x) cout<<x<<" "
#define foreach(arr) for(auto &ele : arr) cout<<ele<<" "
#define nextline cout<<"\n"
using namespace std;
const int MOD = 1e9+7;
vector<vector<int>> farr(1005,vector<int>(1005,0));
vector<vector<int>> arr(1005,vector<int>(1005,0));
void generateFenwickTree2D(int, int);
void update(int, int, int, int, int);
int query(int, int, int, int);
int prefixSum(int, int);
void generateFenwickTree2D(int row, int col){
for(int i=1;i<=row;++i){
for(int j=1;j<=col;++j){
update(i,j,arr[i][j],row,col);
}
}
}
void update(int x, int y, int val, int row, int col){
int xdash = x;
while(xdash<=row){
int ydash=y;
while(ydash<=col){
farr[xdash][ydash]+=val;
ydash+=(ydash&-ydash);
}
xdash+=(xdash&-xdash);
}
}
int query(int x1, int y1, int x2, int y2){
int inc1 = prefixSum(x2,y2);
int exc1 = prefixSum(x1-1,y2);
int exc2 = prefixSum(x2,y1-1);
int inc2 = prefixSum(x1-1,y1-1);
return inc1-exc1-exc2+inc2;
}
int prefixSum(int x, int y){
int sum=0;
int xdash = x;
while(xdash>0){
int ydash=y;
while(ydash>0){
sum+=farr[xdash][ydash];
ydash-=(ydash&-ydash);
}
xdash-=(xdash&-xdash);
}
return sum;
}
int32_t main(){
fastio;
ll t=1;
while(t--){
int row,col;
cin>>row>>col;
for(int i=1;i<=row;++i){
for(int j=1;j<=col;++j){
cin>>arr[i][j];
}
}
generateFenwickTree2D(row,col);
int q;
cin>>q;
char ch;
int x1,y1,x2,y2,val;
while(q--){
cin>>ch;
if(ch=='q'){
cin>>x1>>y1>>x2>>y2;
int ans = query(x1,y1,x2,y2);
print(ans);
}else if(ch=='u'){
cin>>x1>>y1>>val;
update(x1,y1,val,row,col);
}
}
}//while
return 0;
}