-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefixsum2d.cpp
More file actions
58 lines (46 loc) · 1.17 KB
/
prefixsum2d.cpp
File metadata and controls
58 lines (46 loc) · 1.17 KB
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
/*
Given 2d array a of N*N integers. Given Q queries and
in each query given a, b, c and d. Print sum of square
represented by (a,b) as top left point and (c,d) as top bottom right
point
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
//basic
//int ar[N][N];
int ar[N][N];
long long pf[N][N];
int main() {
#ifndef ONLINE_JUDGE
freopen("input1.txt", "r", stdin);
freopen("output1.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
//basic code
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> ar[i][j];
pf[i][j] = ar[i][j] + pf[i - 1][j] + pf[i][j - 1] - pf[i - 1][j - 1];
}
}
int q;
cin >> q;
while (q--) {
int a, b, c, d;
cin >> a >> b >> c >> d;
// long long sum = 0;
// for (int i = a; i <= c; i++) {
// for (int j = b; j <= d; j++) {
// sum += ar[i][j];
// }
// }
// cout << sum << endl;
//prefix sum 2d
cout << pf[c][d] - pf[a - 1][d] - pf[c][b - 1] + pf[a - 1][b - 1] << endl;
//tc -> O(N^2) + O(Q)
} //tc -> O(N^2) + O(Q*N^2)
}