-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfenwick_tree_2d.cpp
More file actions
61 lines (53 loc) · 1023 Bytes
/
fenwick_tree_2d.cpp
File metadata and controls
61 lines (53 loc) · 1023 Bytes
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
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define MOD 1e9 + 7
vector<vector<int> > a;
int m,n,q;
void update(int x,int y,int val)
{
int i = x,j = y;
while(x<=m)
{
y = j;
while(y<=n)
{
a[x][y]+=val;
y+=(y & -y);
}
x+=(x & -x);
}
}
int read(int x,int y)
{
int val = 0,i = x,j = y;
while(x>0)
{
y = j;
while(y>0)
{
val+=a[x][y];
y-=(y & -y);
}
x-=(x & -x);
}
return val;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> m >> n >> q;
a.resize(m+1);
for(int i = 0;i <= m;i++) a[i].resize(n+1);
for(int i = 1;i <= m;i++)
{
a[i].resize(n+1);
for(int j = 1;j <= n;j++){ int x; cin >> x; update(i,j,x); }
}
while(q--)
{
int x1,y1,x2,y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << read(x2,y2)-read(x2,y1-1)-read(x1-1,y2)+read(x1-1,y1-1) << endl;
}
}