Problem p03458 (python)

Fixed submission:
import sys
N, K = map(int, input().split())
x, y, c = [0] * N, [0] * N, [0] * N
board = [[0] * (2 * K + 1) for i in range(2 * K + 1)]
MOD = 2 * K + 1
for i in range(N):
    x[i], y[i], c[i] = input().split()
    x[i], y[i] = int(x[i]), int(y[i])
    if c[i] == 'W':
        x[i] += K
        c[i] = 'B'
    x[i] %= 2 * K
    y[i] %= 2 * K
    board[y[i]+1][x[i]+1] += 1

# for i in range(2*K+1):
#     print(' '.join(map(str, board[i])))
# print('================')
for i in range(2*K+1):
    for j in range(2*K):
        board[i][j+1] += board[i][j]
for j in range(2*K+1):
    for i in range(2*K):
        board[i+1][j] += board[i][j]

# for i in range(2*K+1):
#     print(' '.join(map(str, board[i])))
ans = 0
def c(x1, y1):
    global K, board
    x1 = x1 % MOD + x1 // MOD
    y1 = y1 % MOD + y1 // MOD
    x2 = (x1 + K) % MOD + (x1 + K) // MOD
    y2 = (y1 + K) % MOD + (y1 + K) // MOD
    if x1 < x2:
        x11, x12, x21, x22 = x1, x2, 0, 0
    else:
        x11, x12, x21, x22 = x1, -1, 0, x2
    if y1 < y2:
        y11, y12, y21, y22 = y1, y2, 0, 0
    else:
        y11, y12, y21, y22 = y1, -1, 0, y2
    f = lambda a, b, c, d: board[a][b] + board[c][d]\
            - board[a][d] - board[c][b]
    # print("x11: %d, x12: %d" % (x11, x12))
    # print("x21: %d, x22: %d" % (x21, x22))
    # print("y11: %d, y12: %d" % (y11, y12))
    # print("y21: %d, y22: %d" % (y21, y22))
    # print("a1: %d" % f(y12, x12, y11, x11))
    # print("a2: %d" % f(y22, x22, y21, x21))
    # print("a3: %d" % f(y12, x22, y11, 0))
    # print("a4: %d" % f(y22, x12, 0, x11))
    ret = 0
    ret += f(y12, x12, y11, x11)
    ret += f(y22, x22, y21, x21)
    ret += f(y12, x22, y11, x21)
    ret += f(y22, x12, y21, x11)
    return ret


for i in range(2*K+1):
    for j in range(K+1):
        # print('i: %d, j: %d' % (i, j))
        # print('c(i, j): %d' % c(i, j))
        # print('c(i+K, j+K): %d' % c(i+K, j+K))
        # print('')
        ans = max((ans, c(i, j) + c(i+K, j+K)))

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.exit(1)
N, K = map(int, input().split())
x, y, c = [0] * N, [0] * N, [0] * N
board = [[0] * (2 * K + 1) for i in range(2 * K + 1)]
MOD = 2 * K + 1
for i in range(N):
    x[i], y[i], c[i] = input().split()
    x[i], y[i] = int(x[i]), int(y[i])
    if c[i] == 'W':
        x[i] += K
        c[i] = 'B'
    x[i] %= 2 * K
    y[i] %= 2 * K
    board[y[i]+1][x[i]+1] += 1

for i in range(2*K):
    for j in range(2*K):
        board[i][j+1] += board[i][j]
for j in range(2*K+1):
    for i in range(2*K):
        board[i+1][j] += board[i][j]

# for i in range(2*K+1):
#     print(' '.join(map(str, board[i])))
ans = 0
def c(x1, y1):
    global K, board
    x1 = x1 % MOD + x1 // MOD
    y1 = y1 % MOD + y1 // MOD
    x2 = (x1 + K) % MOD + (x1 + K) // MOD
    y2 = (y1 + K) % MOD + (y1 + K) // MOD
    if x1 < x2:
        x11, x12, x21, x22 = x1, x2, 0, 0
    else:
        x11, x12, x21, x22 = x1, -1, 0, x2
    if y1 < y2:
        y11, y12, y21, y22 = y1, y2, 0, 0
    else:
        y11, y12, y21, y22 = y1, -1, 0, y2
    f = lambda a, b, c, d: board[a][b] + board[c][d]\
            - board[a][d] - board[c][b]
    # print("x11: %d, x12: %d" % (x11, x12))
    # print("x21: %d, x22: %d" % (x21, x22))
    # print("y11: %d, y12: %d" % (y11, y12))
    # print("y21: %d, y22: %d" % (y21, y22))
    # print("a1: %d" % f(y12, x12, y11, x11))
    # print("a2: %d" % f(y22, x22, y21, x21))
    # print("a3: %d" % f(y12, x22, y11, 0))
    # print("a4: %d" % f(y22, x12, 0, x11))
    ret = 0
    ret += f(y12, x12, y11, x11)
    ret += f(y22, x22, y21, x21)
    ret += f(y12, x22, y11, x21)
    ret += f(y22, x12, y21, x11)
    return ret


for i in range(2*K+1):
    for j in range(K+1):
        # print('i: %d, j: %d' % (i, j))
        # print('c(i, j): %d' % c(i, j))
        # print('c(i+K, j+K): %d' % c(i+K, j+K))
        # print('')
        ans = max((ans, c(i, j) + c(i+K, j+K)))

print(ans)