-
Notifications
You must be signed in to change notification settings - Fork 0
/
RSQ(2DPrefix_Sum).cpp
46 lines (42 loc) · 1 KB
/
RSQ(2DPrefix_Sum).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
#include <bits/stdc++.h>
using namespace std;
int arr[110][110];
int query[110][110];
int n;
int main(int argc, char const *argv[])
{
while(cin >> n){
// input
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++)
cin >> arr[i][j];
}
// bulid prefix query
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
query[i][j] = arr[i][j];
if(i - 1 >= 0) query[i][j] += query[i-1][j];
if(j - 1 >= 0) query[i][j] += query[i][j-1];
if(i - 1 >= 0 && j - 1 >= 0) query[i][j] -= query[i-1][j-1];
}
}
int temp;
int maximum = 0x80000000;
// find the maximum sum in any range
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
for(int k = i; k < n; k++){
for(int t = j; t < n; t++){
temp = query[k][t];
if(i - 1 >= 0) temp -= query[i-1][t];
if(j - 1 >= 0) temp -= query[k][j-1];
if(i - 1 >= 0 && j - 1 >= 0) temp += query[i-1][j-1];
if(maximum < temp) maximum = temp;
}
}
}
}
printf("%d\n", maximum);
}
return 0;
}