forked from Vishalrai2002/CP_Math_CB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path009PrefixSum_2dArray.cpp
More file actions
54 lines (46 loc) · 1.2 KB
/
009PrefixSum_2dArray.cpp
File metadata and controls
54 lines (46 loc) · 1.2 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
/*
Given 2d array of N*N integers Given Q queries and in each
queries give a,b,c and d print sum of square of (a,b) as top
left point and (c,d) as bottom right points
constraints
1<=N<=10^3
1<=a[i][j]<=10^9
1<=Q<=10^5
1<=a,b,c,d <=N
*/
#include<iostream>
using namespace std;
const int N=1e3+10;
int arr[N][N];
int pf[N][N]; // Declared 2d prefix sum array
int main(){
int n;
cin>>n;
// int arr[n][n];
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>arr[i][j];
// Below i Calculated the prefix sum and store it
// Check notes
pf[i][j]=arr[i][j]+pf[i][j-1]+pf[i-1][j]-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;
// Time Complexity -> O(N^2) + O(Q+N^2) -> 10^5+10^6 = 10^11 give TLE
// for(int i=a;i<=c;i++){
// for(int j=b;j<=d;j++){
// sum+=arr[i][j];
// }
// }
// cout<<sum<<endl;
// Time Complexity -> O(N^2) + O(Q) -> 10^6 + 10^5 = 10^6
// Check notes
cout<<pf[c][d]-pf[a-1][d]-pf[c][b-1]+pf[a-1][b-1]<<endl;
}
return 0;
}