-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2d array precomp.cpp
More file actions
103 lines (86 loc) · 2.15 KB
/
2d array precomp.cpp
File metadata and controls
103 lines (86 loc) · 2.15 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
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
96
97
98
99
100
101
102
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define all(X) (X).begin(), (X).end()
#define REP(i,a,b) for (int i = a; i<b; i++)
#define S second
#define ii pair<int, int>
#define PB push_back
#define MP make_pair
#define SQ(a) (a)*(a)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define endl '\n'
const int M = 1e9 + 7;
const int N = 1e3 + 10;
// long long fact[N];
/* Constraints
1 <= T <= 10^5
1 <= N <= 10^5
*/
// pre-compute to store all fact
// void precomp() {
// fact[0] = fact[1] = 1;
// for (int i = 2; i < N; i++) {
// fact[i] = fact[i - 1] * i;
// }
// }
// void solve() {
// // code
// }
int ar[N][N];
int pref[N][N];
int main() {
#ifndef ONLINE_JUDGE
//input.txt
freopen("input1.txt", "r", stdin);
//output.txt
freopen("output1.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
// precomp();
// normal way to calculate the sum of numbers in given range in an array
// O(Q*N) = 10 ^ 10 -> worst time complex
// int n;
// cin >> n;
// for (int i = 1; i <= n; i++) {
// cin >> a[i];
// //prefix sum
// pref[i] = pref[i - 1] + a[i]; // prefix sum ka array to calculate
// }
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> ar[i][j];
pref[i][j] = ar[i][j] + pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
}
}
int t;
cin >> t;
while (t--) {
// int l, r;
// cin >> l >> r;
// long long sum = 0;
// for (int i = l; i <= r; i++) {
// sum += a[i];
// }
// cout << sum << '\n';
// cout << pref[r] - pref[l - 1] << '\n'; // O(N) + O(Q) = 10 ^ 5
int a, b, c, d;
cin >> a >> b >> c >> d;
//***************for cal the sum of number in an matrix*************//
// long long sum = 0;
// for (int i = a; i <= c; i++) {
// for (int j = b; j <= d; j++) {
// sum += ar[i][j];
// }
// }
// cout << sum << '\n';
cout << pref[c][d] - pref[a - 1][d] - pref[c][b - 1] + pref[a - 1][b - 1] << '\n';
}
return 0;
}