-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD_X-Sum.py
64 lines (53 loc) · 1.43 KB
/
D_X-Sum.py
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
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
m, n = list(map(int, input().split()))
chess = []
for _ in range(m):
row = list(map(int, input().split()))
chess.append(row)
right_sum = [[chess[i][j] for j in range(len(chess[0]))] for i in range(len(chess))]
left_sum = [[chess[i][j] for j in range(len(chess[0]))] for i in range(len(chess))]
# Build prefix sum diagonally towards right
for i in range(m):
k = i
j=0
last = 0
while k < m and j < n:
right_sum[k][j] +=last
last = right_sum[k][j]
k+=1
j+=1
for j in range(1,n):
k = j
l =0
last = 0
while k < n and l < m:
right_sum[l][k] +=last
last = right_sum[l][k]
k+=1
l+=1
# Build prefix sum diagonally towards left
for i in range(m):
k = i
j=n-1
last = 0
while k < m and j > -1:
left_sum[k][j] +=last
last = left_sum[k][j]
k+=1
j-=1
for j in range(n-2,-1,-1):
k = j
l = 0
last = 0
while k > -1 and l < m:
left_sum[l][k] +=last
last = left_sum[l][k]
k-=1
l+=1
max_diag = 0
for i in range(chess):
for j in range(chess[i]):
tmp =