Problem:


Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:

AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

Output

Print the maximum number of desires that can be satisfied at the same time.

Sample Input 1

4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

4

He can satisfy all his desires by painting as shown in the example above.

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

4



Sample input:
4 3
0 1 W
1 2 W
5 3 B
5 4 B


Sample output:
4

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x, y, c = [None] * N, [None] * N, [None] * N
KK = 2 * K
table = [[0] * (KK+1) for i in range(KK+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] %= KK
    y[i] %= KK
    table[y[i]+1][x[i]+1] += 1

for i in range(KK+1):
    for j in range(KK):
        table[i][j+1] += table[i][j]
for i in range(KK):
    for j in range(KK+1):
        table[i+1][j] += table[i][j]

def s(x1, x2, y1, y2):
    global table, KK
    MOD = KK + 1
    x1 %= MOD
    y1 %= MOD
    x2 %= MOD
    y2 %= MOD
    x11 = x1
    x21 = 0
    y11 = y1
    y21 = 0
    if x1 < x2:
        x12 = x2
        x22 = 0
    else:
        x12 = KK
        x22 = x2 + 1
    if y1 < y2:
        y12 = y2
        y22 = 0
    else:
        y12 = KK
        y22 = y2 + 1
    f = lambda _x1, _x2, _y1, _y2: \
            table[_y2][_x2] - table[_y2][_x1] \
            - table[_y1][_x2] + table[_y1][_x1]
    return f(x11, x12, y11, y12) + f(x21, x22, y11, y12) \
            + f(x11, x12, y21, y22) + f(x21, x22, y21, y22)

ans = 0
for i in range(KK):
    for j in range(K + K//2):
        tmp = s(i, i+K, j, j+K)
        tmp += s(i+K, i+K+K, j+K, j+K+K)
        ans = max((ans, tmp))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x, y, c = [None] * N, [None] * N, [None] * N
KK = 2 * K
table = [[0] * (KK+1) for i in range(KK+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] %= KK
    y[i] %= KK
    table[y[i]+1][x[i]+1] += 1

for i in range(KK+1):
    for j in range(KK):
        table[i][j+1] += table[i][j]
for i in range(KK):
    for j in range(KK+1):
        table[i+1][j] += table[i][j]

def s(x1, x2, y1, y2):
    global table, KK
    MOD = KK + 1
    x1 %= MOD
    y1 %= MOD
    x2 %= MOD
    y2 %= MOD
    x11 = x1
    x21 = 0
    y11 = y1
    y21 = 0
    if x1 < x2:
        x12 = x2
        x22 = 0
    else:
        x12 = KK
        x22 = x2 + 1
    if y1 < y2:
        y12 = y2
        y22 = 0
    else:
        y12 = KK
        y22 = y2 + 1
    f = lambda _x1, _x2, _y1, _y2: \
            table[_y2][_x2] - table[_y2][_x1] \
            - table[_y1][_x2] + table[_y1][_x1]
    return f(x11, x12, y11, y12) + f(x21, x22, y11, y12) \
            + f(x11, x12, y21, y22) + f(x21, x22, y21, y22)

ans = 0
for i in range(KK):
    for j in range(K):
        tmp = s(i, i+K, j, j+K)
        tmp += s(i+K, i+K+K, j+K, j+K+K)
        ans = max((ans, tmp))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x, y, c = [None] * N, [None] * N, [None] * N
KK = 2 * K
table = [[0] * (KK+1) for i in range(KK+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] %= KK
    y[i] %= KK
    table[y[i]+1][x[i]+1] += 1

for i in range(KK+1):
    for j in range(KK):
        table[i][j+1] += table[i][j]
for i in range(KK):
    for j in range(KK+1):
        table[i+1][j] += table[i][j]

def s(x1, x2, y1, y2):
    global table, KK
    MOD = KK + 1
    x2 %= MOD
    y2 %= MOD
    x11 = x1
    x21 = 0
    y11 = y1
    y21 = 0
    if x1 < x2:
        x12 = x2
        x22 = 0
    else:
        x12 = KK
        x22 = x2 + 1
    if y1 < y2:
        y12 = y2
        y22 = 0
    else:
        y12 = KK
        y22 = y2 + 1
    f = lambda _x1, _x2, _y1, _y2: \
            table[_y2][_x2] - table[_y2][_x1] \
            - table[_y1][_x2] + table[_y1][_x1]
    return f(x11, x12, y11, y12) + f(x21, x22, y11, y12) \
            + f(x11, x12, y21, y22) + f(x21, x22, y21, y22)

ans = 0
for i in range(K):
    for j in range(K):
        tmp = s(i, i+K, j, j+K)
        tmp += s(i+K, i+K+K, j+K, j+K+K)
        ans = max((ans, tmp))
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import sys

def color(i, k):
    if ((i % (2*k)) / k) == 0:
        return 'B'
    else:
        return 'W'
    
n, k = [int(e) for e in raw_input().split()]
tl = []
for i in range(0, n):
    x, y, c = [e for e in raw_input().split()]
    tl.append([int(x), int(y), c])

# print
# print n, k
# print tl

# for i in range(0, 2*k):
#     print color(i, k),
# print

maxHx = 0
for i in range(0, 2*k):
    h = 0
    for e in tl:
        x, y, c = e
        if color(x + i, k) == c:
            h += 1
    if h > maxHx:
        maxHx = h
# print 'X', maxHx

maxHy = 0
for i in range(0, 2*k):
    h = 0
    for e in tl:
        x, y, c = e
        if color(y + i, k) == c:
            h += 1
    if h > maxHy:
        maxHy = h
# print 'Y', maxHy

sys.stdout.write('%d\n' % min(maxHx, maxHy))
sys.stdout.flush()

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
B = [[0]*k for i in range(k)]
W = [[0]*k for i in range(k)]
for i in range(n):
    x, y, c = input().split(); x = int(x); y = int(y)
    x %= 2*k; y %= 2*k
    if (k <= x) == (k <= y):
        (B if c is 'B' else W)[y%k][x%k] += 1
    else:
        (W if c is 'B' else B)[y%k][x%k] += 1
def calc(M, n):
    D = [[0]*(n+1) for i in range(n+1)]
    for i in range(1, n+1):
        tmp = 0
        for j in range(1, n+1):
            tmp += M[i-1][j-1]
            D[i][j] = tmp + D[i-1][j]
    return D

# [a, c) * [b, d)
def su(D, a, b, c, d):
    return D[d][c] - D[d][a] - D[b][c] + D[b][a]

DB = calc(B, k)
DW = calc(W, k)
ans = 0
for i in range(k):
    for j in range(k):
        ans = max(ans, su(DB, 0, 0, i, j) + su(DB, i, j, k, k) + su(DW, i, 0, k, j) + su(DW, 0, j, i, k))
        ans = max(ans, su(DW, 0, 0, i, j) + su(DW, i, j, k, k) + su(DB, i, 0, k, j) + su(DB, 0, j, i, k))
print(ans)

Submission status: Wrong Answer

Solution code:
nk = [int(x) for x in raw_input().strip().split()]
d = []
for i in range(nk[0]):
    d_tmp = raw_input().strip().split()
    d.append([int(d_tmp[0]), int(d_tmp[1]), int(d_tmp[2] == 'B')])

n1 = nk[0]
k1 = nk[1]
k2 = k1 * 2

n = 0
#for i in range(k2):
#    for j in range(k2):
#        n_tmp = 0
#        for k in range(n1):
#            x = (d[k][0] - i) // k1 % 2
#            y = (d[k][1] - j) // k1 % 2
#            if x == y and d[k][2] == 'B':
#                n_tmp += 1
#            elif x != y and d[k][2] == 'W':
#                n_tmp += 1
#        if n_tmp > n:
#            n = n_tmp
import time
t = time.clock()
for i in range(k1):
    for j in range(k1):
        n_tmp = [0, 0]
        for k in range(n1):
            x = (d[k][0] - i) // k1 % 2
            y = (d[k][1] - j) // k1 % 2
#            if x == y and d[k][2]:
#                n_tmp[0] += 1
#            elif x != y and not d[k][2]:
#                n_tmp[0] += 1
#            if x == y and not d[k][2]:
#                n_tmp[1] += 1
#            elif x != y and d[k][2]:
#                n_tmp[1] += 1
            if x == y:
                n_tmp[1 - d[k][2]] += 1
            else:
                n_tmp[d[k][2]] += 1
        if n_tmp[0] > n:
            n = n_tmp[0]
        if n_tmp[1] > n:
            n = n_tmp[1]
        n_tmp = 0
print time.clock() - t
print n

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# coding=utf-8

import sys

import numpy as np

n, k = map(int, sys.stdin.readline().strip().split(" "))
xyc = [l.strip().split(" ") for l in sys.stdin.readlines()]
xy = [tuple((int(_x), int(_y) if _c == "W" else int(_y) + k)) for (_x, _y, _c) in xyc]
xy = [tuple((_x % (2 * k), _y % (2 * k))) for (_x, _y) in xy]
print(xy)

ans = np.zeros((2 * k, 2 * k), dtype=np.int32)
for _x, _y in xy:
    if (_x - k + 0.5) * (_y - k + 0.5) > 0:
        _x %= k
        _y %= k
        ans[_x, _y] += 1
        ans[_x + k, _y] -= 1
        ans[_x, _y + k] -= 1
        ans[_x + k, _y + k] += 1
        ans[_x + k, _y + k] += 1
        ans[_x + k, 0] += 1
        ans[_x + k, _y] -= 1
        ans[0, _y + k] += 1
        ans[_x, _y + k] -= 1
        ans[0, 0] += 1
        ans[_x, 0] -= 1
        ans[0, _y] -= 1
        ans[_x, _y] += 1
    else:
        _x %= k
        _y %= k
        ans[_x, _y] -= 1
        ans[_x + k, _y] += 1
        ans[_x, _y + k] += 1
        ans[_x + k, _y + k] -= 1
        ans[_x + k, _y + k] -= 1
        ans[_x + k, 0] -= 1
        ans[_x + k, _y] += 1
        ans[0, _y + k] -= 1
        ans[_x, _y + k] += 1
        # ans[0, 0] -= 1
        ans[_x, 0] += 1
        ans[0, _y] += 1
        ans[_x, _y] -= 1
# print(ans)
# print(ans.cumsum(axis=1).cumsum(axis=0))
print(np.max(ans.cumsum(axis=1).cumsum(axis=0)))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# coding=utf-8

import itertools
import sys

import numpy as np

n, k = map(int, sys.stdin.readline().strip().split(" "))
xyc = [l.strip().split(" ") for l in sys.stdin.readlines()]
xyc = [tuple((int(_x), int(_y), _c == "W")) for (_x, _y, _c) in xyc]
xyc = [
    tuple((_x % k, _y % k, _c if (_x // k + _y // k) % 2 == 0 else not _c)) for (_x, _y, _c) in xyc
]


def checker(base, condition):
    i, j, f = base
    x, y, c = condition
    if not ((x <= i and y <= j) or (i < x and j < y)):
        f = not f
    return f == c


xs = np.asarray([_x for (_x, _y, _c) in xyc])
ys = np.asarray([_y for (_x, _y, _c) in xyc])
cs = np.asarray([_c for (_x, _y, _c) in xyc])
ans = 0
for i, j, f in itertools.product(set(xs), set(ys), {True, False}):
    checks = np.logical_not(np.logical_xor((xs - i - 0.5) * (ys - j - 0.5) > 0, cs)) == f
    # checks = [checker((i, j, f), _xyc) for _xyc in xyc]
    print(checks)
    ans = max(ans, sum(checks))
    if ans == n:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# coding=utf-8

import itertools
import sys

import numpy as np

n, k = map(int, sys.stdin.readline().strip().split(" "))
xyc = [l.strip().split(" ") for l in sys.stdin.readlines()]
xyc = [tuple((int(_x), int(_y), _c == "W")) for (_x, _y, _c) in xyc]
xyc = [
    tuple((_x % k, _y % k, _c if (_x // k + _y // k) % 2 == 0 else not _c)) for (_x, _y, _c) in xyc
]


def checker(base, condition):
    i, j, f = base
    x, y, c = condition
    if not ((x <= i and y <= j) or (i < x and j < y)):
        f = not f
    return f == c


xs = np.asarray([_x for (_x, _y, _c) in xyc])
ys = np.asarray([_y for (_x, _y, _c) in xyc])
cs = np.asarray([_c for (_x, _y, _c) in xyc])
ans = 0
for i, j, f in itertools.product(set(xs), set(ys), {True, False}):
    checks = np.logical_not(np.logical_xor((xs - i - 0.5) * (ys - j * 0.5) > 0, cs)) == f
    ans = max(ans, sum(checks))
    if ans == n:
        break
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, raw_input().split())
x,y,c=[],[],[]
K2=2*K
xx,yy=[],[]

for i in range(N):
    x0,y0,col = raw_input().split()
    x.append(int(x0))
    y.append(int(y0))
    c.append(col)

    x0=int(x0)%K
    y0=int(y0)%K
    xx.append(x0)
    yy.append(y0)
   
xmin,xmax=min(xx),max(xx)
ymin,ymax=min(yy),max(yy)

print xmin,xmax
print ymin,ymax

countmax=0
for a in range(xmin,xmax+1):
    for b in range(ymin,ymax+1):
        okc1,okc2=0,0
        for i in range(N):
            x1=(x[i]-a)%K2
            y1=(y[i]-b)%K2

            ok1,ok2=1,1

            q=(x1/K)==0 #q=(0<=x1 and x1<K)
            r=(y1/K)==1 #r=(K<=y1 and y1<K2)
            s=(x1/K)==1 #s=(K<=x1 and x1<K2)
            t=(y1/K)==0 #t=( 0<=y1 and y1<K)

            if (c[i]=="B") :
               if ((q and r) or (s and t)) : ok1=0
               if ((q and t) or (s and r)) : ok2=0
            if (c[i]=="W") : 
               if ((q and r) or (s and t)) : ok2=0
               if ((q and t) or (s and r)) : ok1=0
            okc1=okc1+ok1
            okc2=okc2+ok2

        if (countmax<okc1) :countmax=okc1 
        if (countmax<okc2) :countmax=okc2
        if (countmax==N) :break
    if(countmax==N):break

print countmax

Submission status: Wrong Answer

Solution code:
import numpy as np

N, K = map(int, input().split())
ub = K * 2
bw = {"B": np.zeros((ub, ub), int), "W": np.zeros((ub, ub), int)}
for i in range(N):
	x, y, c = input().split()
	bw[c][int(x) % ub][int(y) % ub] += 1

def accum_arr(arr):
	acc_h = np.zeros((ub, ub), int)
	acc_v = np.zeros((ub, ub), int)
	acc = np.zeros((ub, ub), int)
	acc_h[:, 0] = arr[:, 0] + arr[:, - (K - 1):].sum(axis=1)
	acc_v[0, :] = arr[0, :] + arr[- (K - 1):, :].sum(axis=0)

	for i in range(1, ub):
		acc_h[:, i] = arr[:, i] + acc_h[:, i - 1] - arr[:, (i - K) % ub]
		acc_v[i, :] = arr[i, :] + acc_v[i - 1, :] - arr[(i - K) % ub, :]

	acc[0][0] = arr[0][0] + arr[0, - (K - 1):].sum() + arr[- (K - 1):, 0].sum() + arr[-(K - 1):, - (K - 1):].sum()

	for j in range(1, ub):
		acc[0][j] = acc[0][j - 1] + acc_v[0][j] - acc_v[0][(j - K) % ub]

	for j in range(ub):
		for i in range(1, ub):
			acc[i][j] = acc[i - 1][j] + acc_h[i][j] - acc_h[(i - K) % ub][j]

	return acc


b_acc = accum_arr(bw["B"])
w_acc = accum_arr(bw["W"])

ans = 0
for x in range(ub):
	for y in range(ub):
		x_cmp = (x + K) % ub
		y_cmp = (y + K) % ub
		tmp_ans = b_acc[x][y] + b_acc[x_cmp][y_cmp] + w_acc[x][y_cmp] + w_acc[x_cmp][y]
		if tmp_ans > ans:
			ans = tmp_ans

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
f = [[0 for j in range(2*k+1)]for i in range(2*k+1)]
for i in range(n):
    x,y = map(int, input().split()[:2])
    y = y + k
    x %= 2 * k
    y %= 2 * k
    f[x+1][y+1] = 1

s = [[0 for j in range(2*k+1)] for i in range(2*k+1)]
for i in range(1, 2*k+1):
    for j in range(1, 2*k+1):
        s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + f[i][j]

def count(a, b, c, d):
    if a == 0 and c == 0: return 0
    if b == 0 and d == 0: return 0
    if a == 0 and b == 0: return s[c+1][d+1]
    if a == 0: return s[c+1][d+1] - s[c+1][b]
    if b == 0: return s[c+1][d+1] - s[a][d+1]
    return s[c+1][d+1] - s[a][d+1] - s[c+1][b] + s[a][b]

def check(p):
    if p < 0: return 0
    if p > 2 * k - 1: return 2 * k -1
    return p

ans = 0
for i in range(2 * k):
    for j in range(2 * k):
        tmp = count(i, j, check(i+k-1), check(j+k-1))
        tmp += count(check(i-k), check(j-k), check(i-1), check(j-1))
        tmp += count(check(i+k), check(j+k), check(i+2*k-1), check(j+2*k-1))
        tmp += count(check(i-k), check(j+k), check(i-1), check(j+2*k-1))
        tmp += count(check(j+k), check(j-k), check(i+2*k-1), check(j-1))
        if tmp > ans: ans = tmp
print(ans)

Submission status: Wrong Answer

Solution code:
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):
    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(2*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)

Submission status: Wrong Answer

Solution code:
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):
    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)))

if ans > N:
    sys.exit(1)
print(ans)

Submission status: Wrong Answer

Solution code:
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 = f(y12, x12, y11, x11)
    ret += f(y22, x22, y21, x21)
    ret += f(y12, x22, y11, 0)
    ret += f(y22, x12, 0, 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)

Submission status: Wrong Answer

Solution code:
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 = f(y12, x12, y11, x11)
    ret += f(y22, x22, y21, x21)
    ret += f(y12, x22, y11, 0)
    ret += f(y22, x12, 0, x11)
    return ret


for i in range(2*K+1):
    for j in range(2*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)

Submission status: Wrong Answer

Solution code:
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(0, x12, y22, x21))
    # print("a4: %d" % f(y12, 0, y22, x22))
    ret = f(y12, x12, y11, x11) + f(y22, x22, y21, x21)
    a = max((0, f(y12, 0, y22, x22)))
    b = max((0, f(0, x12, y22, x21)))
    return ret + a + b


for i in range(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)

Submission status: Wrong Answer

Solution code:
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
    x2 = x2 % MOD + x2 // MOD
    y2 = y1 + K
    y2 = y2 % MOD + y2 // 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))
    return f(y12, x12, y11, x11) + f(y22, x22, y21, x21)


for i in range(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)

Submission status: Wrong Answer

Solution code:
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 %= MOD
    y1 %= MOD
    x2 = (x1 + K) % MOD + x1 // MOD
    y2 = (y1 + K) % MOD + y1 // MOD
    if x1 < x2:
        x11, x12, x21, x22 = x1, x2, 0, 0
    else:
        x11, x12, x21, x22 = x1, 2*K, 0, x2
    if y1 < y2:
        y11, y12, y21, y22 = y1, y2, 0, 0
    else:
        y11, y12, y21, y22 = y1, 2*K, 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))
    return f(y12, x12, y11, x11) + f(y22, x22, y21, x21)


for i in range(2*K+1):
    for j in range(2*K+1):
        # 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)

Submission status: Wrong Answer

Solution code:
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 %= MOD
    y1 %= MOD
    x2 = (x1 + K) % MOD
    y2 = (y1 + K) % MOD
    if x1 < x2:
        x11, x12, x21, x22 = x1, x2, 0, 0
    else:
        x11, x12, x21, x22 = x1, 2*K, 0, x2
    if y1 < y2:
        y11, y12, y21, y22 = y1, y2, 0, 0
    else:
        y11, y12, y21, y22 = y1, 2*K, 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))
    return f(y12, x12, y11, x11) + f(y22, x22, y21, x21)


for i in range(2*K+1):
    for j in range(2*K+1):
        # 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)