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:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

n,k = map(int,readline().split())
data = [i.split() for i in readlines()]

cumsum_w = [[0] * (k+1) for _ in range(k+1)]
cumsum_b = [[0] * (k+1) for _ in range(k+1)]
for x,y,c in data:
    x = int(x)
    y = int(y)
    c = 0 if (c=='W') else 1
    c += x//k + y//k
    c %= 2
    x %= k
    y %= k
    if(c==0):
        cumsum_w[x][y] += 1
    else:
        cumsum_b[x][y] += 1

for i in range(k):
    for j in range(1,k):
        cumsum_w[i][j] += cumsum_w[i][j-1]
        cumsum_b[i][j] += cumsum_b[i][j-1]

for i in range(1,k):
    for j in range(k):
        cumsum_w[i][j] += cumsum_w[i-1][j]
        cumsum_b[i][j] += cumsum_b[i-1][j]

ans = 0
for x in range(k):
    for y in range(k):
        tmp = (cumsum_w[k-1][k-1]
                - cumsum_w[x-1][k-1]
                - cumsum_w[k-1][y-1]
                + cumsum_w[x-1][y-1]
                + cumsum_b[x-1][k-1]
                + cumsum_b[k-1][y-1]
                - cumsum_b[x-1][y-1])
        ans = max(ans,tmp)

        tmp = (cumsum_b[k-1][k-1]
                - cumsum_b[x-1][k-1]
                - cumsum_b[k-1][y-1]
                + cumsum_b[x-1][y-1]
                + cumsum_w[x-1][k-1]
                + cumsum_w[k-1][y-1]
                - cumsum_w[x-1][y-1])
        ans = max(ans,tmp)

print(ans)

Submission status: Wrong Answer

Solution code:
class cumsum2D:
    def __init__(self, data):
        self.N = len(data)
        self.M= len(data[0])
        self.cum = [[0] * (self.M + 1) for _ in range(self.N + 1)]
        for i in range(self.N):
            for j in range(self.M):
                self.cum[i + 1][j + 1] = self.cum[i][j + 1] + self.cum[i + 1][j] - self.cum[i][j] + data[i][j]

    def get(self,x1,y1,x2,y2):
        """
        長方形の領域 [x1,x2)*[y1,y2) の範囲内にインプットされた数字の和を取る
        """
        return self.cum[x2][y2] - self.cum[x1][y2] - self.cum[x2][y1] + self.cum[x1][y1]

########################################################################################################
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
B = [[0]*(2*K) for _ in range(2*K)]
W = [[0]*(2*K) for _ in range(2*K)]
for _ in range(N):
    x, y, c = input().split()
    x, y = int(x), int(y)
    if c=="B":
        B[x%(2*K)][y%(2*K)] += 1
    else:
        W[x%(2*K)][y%(2*K)] += 1
cumB, cumW = cumsum2D(B), cumsum2D(W)
res = 0
for i in range(K+1):
    for j in range(K+1):
        res = max(cumB.get(0,0,i,j) + cumB.get(i,j,i+K,j+K) + cumB.get(i+K,j+K,2*K,2*K) + cumB.get(i+K,0,2*K,j) + cumB.get(0,j+K,i,2*K) + cumW.get(i,0,i+K,j) + cumW.get(0,j,i,j+K) + cumW.get(i+K,j,2*K,j+K) + cumW.get(i,j+K,i+K,2*K), res)
print(res)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# N,M = map(int,sys.stdin.readline().split())
# a = tuple(map(int,sys.stdin.readline().split())) # single line with multi param
# a = tuple(int(sys.stdin.readline()) for _ in range(N)) # multi line with single param
# a = tuple(tuple(map(int,sys.stdin.readline().rstrip().split())) for _ in range(N)) # multi line with multi param
# s = sys.stdin.readline().rstrip()
# N = int(sys.stdin.readline())
# INF = float("inf")
import sys,collections,math,fractions
sys.setrecursionlimit(1000000)
INF = float("inf")

N,K = map(int,sys.stdin.readline().split())
xyc = tuple(tuple(sys.stdin.readline().rstrip().split()) for _ in range(N)) # multi line with multi param
xyc = tuple((int(x)%(2*K),int(y)%(2*K),c=="W")for x,y,c in xyc)
W = K
H = K
grid_w = [[0]*(W) for _ in range(H)]
grid_b = [[0]*(W) for _ in range(H)]

for x,y,c in xyc:
    r = (x//K+y//K)%2 # reverse or not
    c = c ^ r
    if c:
        grid_w[y%K][x%K] += 1
    else:
        grid_b[y%K][x%K] += 1
# 4 3
# 0 1 W
# 1 2 W
# 5 3 B
# 5 4 B

# 6 2
# 1 2 B
# 2 1 W
# 2 2 B
# 1 0 B
# 0 6 W
# 4 5 W
white =  [[0]*(W+1) for _ in range(H+1)]
black =  [[0]*(W+1) for _ in range(H+1)]
for r in range(H):
    for c in range(W):
        black[r+1][c+1] = black[r+1][c]+grid_b[r][c]
        white[r+1][c+1] = white[r+1][c]+grid_w[r][c]
for r in range(H):
    for c in range(W+1):
        black[r+1][c] += black[r][c]
        white[r+1][c] += white[r][c]
#print(grid_b,grid_w)
#print(black,white)
ret = 0
for r in range(K+1):
    for c in range(K+1):
        b = black[H][W]-black[H][c]-black[r][W]+3*black[r][c]
        w = white[0][c]+white[r][0]-2*white[r][c]
        #print(r,c,b,w)
        ret = max(ret,b+w)
        b = black[0][c]+black[r][0]-2*black[r][c]
        w = white[H][W]-white[H][c]-white[r][W]+3*white[r][c]
        #print(r,c,b,w)
        ret = max(ret,b+w)

print(ret)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# N,M = map(int,sys.stdin.readline().split())
# a = tuple(map(int,sys.stdin.readline().split())) # single line with multi param
# a = tuple(int(sys.stdin.readline()) for _ in range(N)) # multi line with single param
# a = tuple(tuple(map(int,sys.stdin.readline().rstrip().split())) for _ in range(N)) # multi line with multi param
# s = sys.stdin.readline().rstrip()
# N = int(sys.stdin.readline())
# INF = float("inf")
import sys,collections,math,fractions
sys.setrecursionlimit(1000000)
INF = float("inf")

N,K = map(int,sys.stdin.readline().split())
xyc = tuple(tuple(sys.stdin.readline().rstrip().split()) for _ in range(N)) # multi line with multi param
xyc = tuple((int(x)%K,int(y)%K,c)for x,y,c in xyc)
W = max(x for x,y,c in xyc)+1
H = max(y for x,y,c in xyc)+1
grid = [[0]*(W) for _ in range(H)]

for x,y,c in xyc:
    if c == "W":
        grid[y][x] += 1
    else:
        grid[y][x] -= 1

left_top =  [[0]*(W+1) for _ in range(H+1)]
for r in range(H):
    for c in range(W):
        left_top[r+1][c+1] = left_top[r+1][c]+grid[r][c]
for r in range(H):
    for c in range(W):
        left_top[r+1][c] += left_top[r][c]

right_bottom =  [[0]*(W+1) for _ in range(H+1)]
for r in range(H)[::-1]:
    for c in range(W)[::-1]:
        right_bottom[r][c] = right_bottom[r][c+1]+grid[r][c]

for r in range(H)[::-1]:
    for c in range(W)[::-1]:
        right_bottom[r][c] += right_bottom[r+1][c]

#print(left_top,right_bottom)
ret = 0
for r in range(H):
    ret = max(ret,abs(left_top[r][W]-right_bottom[r][0]))
for c in range(W):
    ret = max(ret,abs(left_top[H][c]-right_bottom[0][c]))
# for r in range(H):
#     for c in range(W):
#         ret = max(ret,abs(left_top[r][c]-right_bottom[r][c]))

print(ret)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# N,M = map(int,sys.stdin.readline().split())
# a = tuple(map(int,sys.stdin.readline().split())) # single line with multi param
# a = tuple(int(sys.stdin.readline()) for _ in range(N)) # multi line with single param
# a = tuple(tuple(map(int,sys.stdin.readline().rstrip().split())) for _ in range(N)) # multi line with multi param
# s = sys.stdin.readline().rstrip()
# N = int(sys.stdin.readline())
# INF = float("inf")
import sys,collections,math,fractions
sys.setrecursionlimit(1000000)
INF = float("inf")

N,K = map(int,sys.stdin.readline().split())
xyc = tuple(tuple(sys.stdin.readline().rstrip().split()) for _ in range(N)) # multi line with multi param
xyc = tuple((int(x)%K,int(y)%K,c)for x,y,c in xyc)
W = max(x for x,y,c in xyc)+1
H = max(y for x,y,c in xyc)+1
grid = [[0]*(W) for _ in range(H)]

for x,y,c in xyc:
    if c == "W":
        grid[y][x] += 1
    else:
        grid[y][x] -= 1

left_top =  [[0]*(W+1) for _ in range(H+1)]
for r in range(H):
    for c in range(W):
        left_top[r+1][c+1] = left_top[r+1][c]+grid[r][c]
for r in range(H):
    for c in range(W):
        left_top[r+1][c] += left_top[r][c]

right_bottom =  [[0]*(W+1) for _ in range(H+1)]
for r in range(H)[::-1]:
    for c in range(W)[::-1]:
        right_bottom[r][c] = right_bottom[r][c+1]+grid[r][c]

for r in range(H)[::-1]:
    for c in range(W)[::-1]:
        right_bottom[r][c] += right_bottom[r+1][c]

#print(left_top,right_bottom)
ret = 0
for r in range(H):
    ret = max(ret,abs(left_top[r][W]-right_bottom[r][0]))
for c in range(W):
    ret = max(ret,abs(left_top[H][c]-right_bottom[0][c]))
for r in range(H):
    for c in range(W):
        ret = max(ret,abs(left_top[r][c]-right_bottom[r][c]))

print(ret)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())

Bcnt = [[0 for j in range(K+1)] for i in range(K+1)]
Wcnt = [[0 for j in range(K+1)] for i in range(K+1)]

for i in range(N):
    x,y,c = input().split()
    x,y = int(x),int(y)
    if ((x-x%K)/K + (y-y%K)/K)%2:
        if c == 'W':
            Bcnt[x%K+1][y%K+1] += 1
        else:
            Wcnt[x%K+1][y%K+1] += 1
    else:
        if c == 'W':
            Wcnt[x%K+1][y%K+1] += 1
        else:
            Bcnt[x%K+1][y%K+1] += 1

# 二次元累積和
for i in range(K):
    for j in range(K):
        Bcnt[i+1][j+1] += Bcnt[i+1][j] + Bcnt[i][j+1] - Bcnt[i][j]
        Wcnt[i+1][j+1] += Wcnt[i+1][j] + Wcnt[i][j+1] - Wcnt[i][j]

ans = 0
Bs = Bcnt[K][K]
Ws = Wcnt[K][K]

for i in range(K+1):
    for j in range(K+1):
        B = Bcnt[K][K] - Bcnt[K][j] - Bcnt[i][K] + Bcnt[i][j]
        W = Wcnt[K][K] - Wcnt[K][j] - Wcnt[i][K] + Wcnt[i][j]
        ans = max(ans, B+Ws-W, W+Bs-B)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())

Bcnt = [[0 for j in range(K+1)] for i in range(K+1)]
Wcnt = [[0 for j in range(K+1)] for i in range(K+1)]

for i in range(N):
    x,y,c = input().split()
    x,y = int(x),int(y)
    if ((x-x%K)//K + (y-y%K)//K)%2:
        if c == 'W':
            Bcnt[x%K+1][y%K+1] += 1
        else:
            Wcnt[x%K+1][y%K+1] += 1
    else:
        if c == 'W':
            Wcnt[x%K+1][y%K+1] += 1
        else:
            Bcnt[x%K+1][y%K+1] += 1

# 二次元累積和
for i in range(K):
    for j in range(K):
        Bcnt[i+1][j+1] += Bcnt[i+1][j] + Bcnt[i][j+1] - Bcnt[i][j]
        Wcnt[i+1][j+1] += Wcnt[i+1][j] + Wcnt[i][j+1] - Wcnt[i][j]

ans = 0
Bs = Bcnt[K][K]
Ws = Wcnt[K][K]

for i in range(K+1):
    for j in range(K+1):
        B = Bcnt[K][K] - Bcnt[K][j] - Bcnt[i][K] + Bcnt[i][j]
        W = Wcnt[K][K] - Wcnt[K][j] - Wcnt[i][K] + Wcnt[i][j]
        ans = max(ans, B+Ws-W, W+Bs-B)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())

Bcnt = [[0 for j in range(K)] for i in range(K)]
Wcnt = [[0 for j in range(K)] for i in range(K)]
Bsum = 0
Wsum = 0
for i in range(N):
    x,y,c = input().split()
    x,y = int(x),int(y)
    if ((x-x%K)//K + (y-y%K)//K)%2:
        if c == 'W':
            Bcnt[x%K][y%K] += 1
            Bsum += 1
        else:
            Wcnt[x%K][y%K] += 1
            Wsum += 1
    else:
        if c == 'W':
            Wcnt[x%K][y%K] += 1
            Wsum += 1
        else:
            Bcnt[x%K][y%K] += 1
            Bsum += 1

# 二次元累積和

for j in range(K-1):
    Bcnt[0][j+1] += Bcnt[0][j]
    Wcnt[0][j+1] += Wcnt[0][j]
    Bcnt[j+1][0] += Bcnt[j][0]
    Wcnt[j+1][0] += Wcnt[j][0]


for i in range(K-1):
    for j in range(K-1):
        Bcnt[i+1][j+1] += Bcnt[i+1][j] + Bcnt[i][j+1] - Bcnt[i][j]
        Wcnt[i+1][j+1] += Wcnt[i+1][j] + Wcnt[i][j+1] - Wcnt[i][j]

ans = 0

for i in range(K):
    for j in range(K):
        ans1 = Bcnt[K-1][K-1] - Bcnt[K-1][j] - Bcnt[i][K-1] + Bcnt[i][j]
        ans2 = Wcnt[K-1][K-1] - Wcnt[K-1][j] - Wcnt[i][K-1] + Wcnt[i][j]
        ans = max(ans, ans1 + Wsum - ans2, ans2 + Bsum - ans1)
print(ans)

Submission status: Wrong Answer

Solution code:
#!python3

# input
N, K = list(map(int, input().split()))
xyc = [input().split() for _ in range(N)]


def add(A, p1, p2):
    A[p1[0]][p1[1]] += 1
    A[p1[0]][p2[1] + 1] -= 1
    A[p2[0] + 1][p1[1]] -= 1
    A[p2[0] + 1][p2[1] + 1] += 1


def main():
    d = {"W": "B", "B": "W"}
    A = [[0] * (K + 2) for _ in range(K + 2)]
    for x, y, c in xyc:
        x = int(x) % (2 * K)
        y = int(y) % (2 * K)
        if (x >= K and y < K) or (x < K and y >= K):
            c = d[c]
        x = x % K
        y = y % K

        if c == "W":
            add(A, (0, 0), (x, y))
            add(A, (x + 1, y + 1), (K, K))
        else:
            add(A, (0, y + 1), (x, K))
            add(A, (x + 1, 0), (K, y))
        
    W = [[0] * (K + 1) for _ in range(K + 1)]
    W[0][0] = A[0][0]
    ans = A[0][0]
    for i in range(1, K + 1):
        W[i][0] = W[i - 1][0] + A[i][0]
        W[0][i] = W[0][i - 1] + A[0][i]
        ans = max(ans, W[i][0], W[0][i])
    for i in range(1, K + 1):
        for j in range(1, K + 1):
            W[i][j] = A[i][j] + W[i - 1][j] + W[i][j - 1] - W[i - 1][j - 1]
            ans = max(ans, W[i][j])
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())

amat_white,amat_black=[],[]
for i in range(2*K):
  amat_white.append([0]*(2*K))
  amat_black.append([0]*(2*K))

smat_white,smat_black=[],[]
for i in range(2*K+1):  
  smat_white.append([0]*(2*K+1))
  smat_black.append([0]*(2*K+1))

num_black=0
for i in range(N):
  x,y,c=input().split()  
  xx=int(x)%(2*K)
  yy=int(y)%(2*K)
  
  if xx<K and yy<K:
    if c=="W":
      amat_white[xx][yy]+=1
      amat_white[xx+K][yy+K]+=1
    else:
      amat_black[xx][yy]+=1
      amat_black[xx+K][yy+K]+=1
      num_black+=1
  elif xx>=K and yy>=K:
    if c=="W":
      amat_white[xx-K][yy-K]+=1
      amat_white[xx][yy]+=1
    else:
      amat_black[xx-K][yy-K]+=1
      amat_black[xx][yy]+=1
      num_black+=1
  elif xx<K and yy>=K:
    if c=="W":
      amat_black[xx][yy-K]+=1
      amat_black[xx+K][yy]+=1
      num_black+=1
    else:
      amat_white[xx][yy-K]+=1
      amat_white[xx+K][yy]+=1
  elif xx>=K and yy<K:
    if c=="W":
      amat_black[xx-K][yy]+=1
      amat_black[xx][yy+K]+=1
      num_black+=1
    else:
      amat_white[xx-K][yy]+=1
      amat_white[xx][yy+K]+=1
  else:
    None
#print(amat_white)
#print(amat_black)

for i in range(1,2*K+1):
  for j in range(1,2*K+1):
    smat_white[i][j]=amat_white[i-1][j-1]+smat_white[i][j-1]+smat_white[i-1][j]-smat_white[i-1][j-1]
    smat_black[i][j]=amat_black[i-1][j-1]+smat_black[i][j-1]+smat_black[i-1][j]-smat_black[i-1][j-1]
#print(smat_white)
#print(smat_black)

max_answer=0
for i in range(K+1):
  for j in range(K+1):
    answer_white=smat_white[i+K][j+K]-smat_white[i][j+K]-smat_white[i+K][j]+smat_white[i][j]
    answer_black=num_black-(smat_black[i+K][j+K]-smat_black[i][j+K]-smat_black[i+K][j]+smat_black[i][j])
    max_answer=max(max_answer,(answer_white+answer_black))
    #print(i,j,answer_white,answer_black)
    
print(max_answer)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
B = [[0]*(2*K+1) for  _ in range(2*K+1)]
B2 = [[0]*(2*K+1) for  _ in range(2*K+1)]
for _ in range(N):
    a, b, c = input().strip().split()
    a, b, c = int(a), int(b), 1 if c == 'B' else 0
    x, y = (a+K*c)%(2*K)+1, b%(2*K)+1
    if x > K and y > K:
        x, y = x-K, y-K
    if x <= K and y > K:
        x, y = x+K, y-K
    B[x][y] += 1
    B2[(x+K)%(2*K)][y] += 1
R = [[0]*(2*K+1) for  _ in range(2*K+1)]
R2 = [[0]*(2*K+1) for  _ in range(2*K+1)]
for i in range(1, 2*K+1):
    for j in range(1, 2*K+1):
        R[i][j] = R[i-1][j] + R[i][j-1] - R[i-1][j-1] + B[i][j]
        R2[i][j] = R2[i-1][j] + R2[i][j-1] - R2[i-1][j-1] + B2[i][j]
r = 0
for i in range(K):
    for j in range(K):
        t1 = R[2*K][j] + R[i+K][K] - R[i][K] - 2*R[i+K][j] + 2*R[i][j]
        r = max(r, t1)
        t1 = R2[2*K][j] + R2[i+K][K] - R2[i][K] - 2*R2[i+K][j] + 2*R2[i][j]
        r = max(r, t1)
print(r)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
B = [[0]*(2*K+1) for  _ in range(2*K+1)]
B2 = [[0]*(2*K+1) for  _ in range(2*K+1)]
for _ in range(N):
    a, b, c = input().strip().split()
    a, b, c = int(a), int(b), 1 if c == 'B' else 0
    x, y = (a+K*c)%(2*K)+1, b%(2*K)+1
    if x > K and y > K:
        x, y = x-K, y-K
    if x <= K and y > K:
        x, y = x+K, y-K
    B[x][y] += 1
    B2[(x+K)%(2*K)][y] += 1
R = [[0]*(2*K+1) for  _ in range(2*K+1)]
R2 = [[0]*(2*K+1) for  _ in range(2*K+1)]
for i in range(1, 2*K+1):
    for j in range(1, 2*K+1):
        R[i][j] = R[i-1][j] + R[i][j-1] - R[i-1][j-1] + B[i][j]
        R2[i][j] = R2[i-1][j] + R2[i][j-1] - R2[i-1][j-1] + B2[i][j]
r = 0
for i in range(K):
    for j in range(K):
        t1 = R[i+K][K] - (R[i][K] + R[i+K][j] - R[i][j])
        t0 = R[i][j]
        tt = R[2*K][j] - R[i+K][j]
        r = max(r, t0+t1+tt)
        t1 = R2[i+K][K] - (R2[i][K] + R2[i+K][j] - R2[i][j])
        t0 = R2[i][j]
        tt = R2[2*K][j] - R2[i+K][j]
        r = max(r, t0+t1+tt)
print(r)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
B = [[0]*(2*K+1) for  _ in range(2*K+1)]
B2 = [[0]*(2*K+1) for  _ in range(2*K+1)]
for _ in range(N):
    a, b, c = input().strip().split()
    a, b, c = int(a), int(b), 1 if c == 'B' else 0
    x, y = (a+K*c)%(2*K)+1, b%(2*K)+1
    if x > K and y > K:
        x, y = x-K, y-K
    if x <= K and y > K:
        x, y = x+K, y-K
    B[x][y] += 1
    B2[(x+K)%(2*K)][y] += 1
R = [[0]*(2*K+1) for  _ in range(2*K+1)]
R2 = [[0]*(2*K+1) for  _ in range(2*K+1)]
for i in range(1, 2*K+1):
    for j in range(1, 2*K+1):
        R[i][j] = R[i-1][j] + R[i][j-1] - R[i-1][j-1] + B[i][j]
        R2[i][j] = R2[i-1][j] + R2[i][j-1] - R2[i-1][j-1] + B2[i][j]
r = 0
for i in range(K):
    for j in range(K):
        t1 = R[i+K][j+K] - (R[i][j+K] + R[i+K][j] - R[i][j])
        t0 = R[i][j]
        tt = R[2*K][j] - R[i+K][j]
        ts = R[i][2*K] - R[i][2*K]
        r = max(r, t0+t1+tt+ts)
        t1 = R2[i+K][j+K] - (R2[i][j+K] + R2[i+K][j] - R2[i][j])
        t0 = R2[i][j]
        tt = R2[2*K][j] - R2[i+K][j]
        ts = R2[i][2*K] - R2[i][2*K]
        r = max(r, t0+t1+tt+ts)
print(r)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
B = [[0]*(2*K+1) for  _ in range(2*K+1)]
B2 = [[0]*(2*K+1) for  _ in range(2*K+1)]
for _ in range(N):
    a, b, c = input().strip().split()
    a, b, c = int(a), int(b), 1 if c == 'B' else 0
    x, y = (a+K*c)%(2*K)+1, b%(2*K)+1
    if x > K and y > K:
        x, y = x-K, y-K
    if x <= K and y > K:
        x, y = x+K, y-K
    B[x][y] += 1
    B2[(x+K)%K][y] += 1
R = [[0]*(2*K+1) for  _ in range(2*K+1)]
R2 = [[0]*(2*K+1) for  _ in range(2*K+1)]
for i in range(1, 2*K+1):
    for j in range(1, 2*K+1):
        R[i][j] = R[i-1][j] + R[i][j-1] - R[i-1][j-1] + B[i][j]
        R2[i][j] = R2[i-1][j] + R2[i][j-1] - R2[i-1][j-1] + B2[i][j]
r = 0
for i in range(K):
    for j in range(K):
        t1 = R[i+K][j+K] - (R[i][j+K] + R[i+K][j] - R[i][j])
        t0 = R[i][j]
        tt = R[2*K][j] - R[i+K][j]
        ts = R[i][2*K] - R[i][2*K]
        r = max(r, t0+t1+tt+ts)
        t1 = R2[i+K][j+K] - (R2[i][j+K] + R2[i+K][j] - R2[i][j])
        t0 = R2[i][j]
        tt = R2[2*K][j] - R2[i+K][j]
        ts = R2[i][2*K] - R2[i][2*K]
        r = max(r, t0+t1+tt+ts)
print(r)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
B = [[0]*(2*K+1) for  _ in range(2*K+1)]
B2 = [[0]*(2*K+1) for  _ in range(2*K+1)]
for _ in range(N):
    a, b, c = input().strip().split()
    a, b, c = int(a), int(b), 1 if c == 'B' else 0
    x, y = (a+K*c)%(2*K)+1, b%(2*K)+1
    x2, y2 = (a+K*(1-c))%(2*K)+1, b%(2*K)+1
    if x > K and y > K:
        x, y = x-K, y-K
    if x2 > K and y2 > K:
        x2, y2 = x2-K, y2-K
    B[x][y] += 1
    B2[x2][y2] += 1
R = [[0]*(2*K+1) for  _ in range(2*K+1)]
R2 = [[0]*(2*K+1) for  _ in range(2*K+1)]
for i in range(1, 2*K+1):
    for j in range(1, 2*K+1):
        R[i][j] = R[i-1][j] + R[i][j-1] - R[i-1][j-1] + B[i][j]
        R2[i][j] = R2[i-1][j] + R2[i][j-1] - R2[i-1][j-1] + B2[i][j]
r = 0
for i in range(K):
    for j in range(K):
        t1 = R[i+K][j+K] - (R[i][j+K] + R[i+K][j] - R[i][j])
        t0 = R[i][j]
        tt = R[2*K][j] - R[i+K][j]
        ts = R[i][2*K] - R[i][2*K]
        r = max(r, t0+t1+tt+ts)
        t1 = R2[i+K][j+K] - (R2[i][j+K] + R2[i+K][j] - R2[i][j])
        t0 = R2[i][j]
        tt = R2[2*K][j] - R2[i+K][j]
        ts = R2[i][2*K] - R2[i][2*K]
        r = max(r, t0+t1+tt+ts)
print(r)

Submission status: Wrong Answer

Solution code:
def f(i, j):
    i1, j1 = i, j
    if i1 > 2*K:
        return 0
    if j1 > 2*K:
        return 0
    i2, j2 = min(i+K, 2*K), min(j+K, 2*K)
    if i < 0:
        if j < 0:
            r = R[i2][j2]
        else:
            r = R[i2][j2] - R[i2][j1]
    elif j<0:
        r = R[i2][j2] - R[i1][j2]
    else:
#        print(i1,j1,i2,j2)
        r = R[i2][j2] - R[i1][j2] - R[i2][j1] + R[i1][j1]
    return r

N, K = map(int, input().split())
B = [[0]*(2*K+1) for  _ in range(2*K+1)]
for _ in range(N):
    a, b, c = input().strip().split()
    a, b, c = int(a), int(b), 1 if c == 'W' else 0
    B[(a+K*c)%(2*K)+1][b%(2*K)+1] += 1
R = [[0]*(2*K+1) for  _ in range(2*K+1)]
for i in range(1, 2*K+1):
    for j in range(1, 2*K+1):
        R[i][j] = R[i-1][j] + R[i][j-1] - R[i-1][j-1] + B[i][j]
r = 0
for i in range(K):
    for j in range(K):
        t = f(i,j)+f(i-K, j-K)+f(i+K, j+K)+f(i-K, j+K)+f(i+K, j-K)
#        print(i,j, f(i,j),f(i-K, j-K),f(i+K, j+K),f(i-K, j+K),f(i-K, j+K))
#        print(i, j, t, f(i-K,j))
        r = max(r, t)
#        print(i, j, t0,t1,t2)
for i in range(K):
    for j in range(K):
        i += K
        t = f(i,j)+f(i-K, j-K)+f(i-K, j+K)+f(i-2*K, j)
        r = max(r, t)
print(r)

Submission status: Wrong Answer

Solution code:
def f(i, j):
    i1, j1 = min(2*K, max(i, 0)), min(2*K, max(j, 0))
    i2, j2 = min(2*K, max(i+K, 0)), min(2*K, max(j+K, 0))
    r = R[i2][j2] - R[i1][j2] - R[i2][j1] + R[i1][j1]
#    print(i1,j1,i2,j2, r, R[i2][j2])
    return r

N, K = map(int, input().split())
B = [[0]*(2*K+1) for  _ in range(2*K+1)]
for _ in range(N):
    a, b, c = input().strip().split()
    a, b, c = int(a), int(b), 1 if c == 'W' else 0
    B[(a+K*c)%(2*K)+1][b%(2*K)+1] += 1
R = [[0]*(2*K+1) for  _ in range(2*K+1)]
for i in range(1, 2*K+1):
    for j in range(1, 2*K+1):
        R[i][j] = R[i-1][j] + R[i][j-1] - R[i-1][j-1] + B[i][j]
#for b in R:
#    print(b)
#for b in B:
#    print(b)
r = 0
for i in range(2*K):
    for j in range(2*K):
        t = f(i,j)+f(i-K, j-K)+f(i+K, j+K)+f(i-K, j+K)+f(i-K, j+K)
#        print(i, j, f(i,j))
        r = max(r, t)
#        print(i, j, t0,t1,t2)
print(r)

Submission status: Wrong Answer

Solution code:
def f(i, j, B):
    B[i][j] - B[i-K][j] - B[i][j-K] + B[i-K]

N, K = map(int, input().split())
B = [[0]*(2*K+1) for  _ in range(2*K+1)]
for _ in range(N):
    a, b, c = input().strip().split()
    a, b, c = int(a), int(b), 1 if c == 'W' else 0
    B[(a+K*c)%(2*K)+1][b%(2*K)+1] += 1
R = [[0]*(2*K+1) for  _ in range(2*K+1)]
for i in range(1, 2*K+1):
    for j in range(1, 2*K+1):
        R[i][j] = R[i-1][j] + R[i][j-1] - R[i-1][j-1] + B[i][j]
#for b in R:
#    print(b)
#for b in B:
#    print(b)
r = 0
for i in range(K+1):
    for j in range(K+1):
        t1 = R[i+K][j+K] - R[i][j+K] - R[i+K][j] + R[i][j]
        t0 = R[i][j]
        t2 = R[2*K][2*K] - (R[i+K][2*K] + R[2*K][j+K] - R[i+K][j+K])
        tt = R[2*K][j] - R[i+K][j]
        ts = R[i][2*K] - R[i][j+K]
        r = max(r, t0+t1+t2+tt+ts)
#        print(i, j, t0,t1,t2)
print(r)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
B = [[0]*(2*K+1) for  _ in range(2*K+1)]
for _ in range(N):
    a, b, c = input().strip().split()
    a, b, c = int(a), int(b), 1 if c == 'W' else 0
    B[(a+K*c)%(2*K)+1][b%(2*K)+1] += 1
R = [[0]*(2*K+1) for  _ in range(2*K+1)]
for i in range(1, 2*K+1):
    for j in range(1, 2*K+1):
        R[i][j] = R[i-1][j] + R[i][j-1] - R[i-1][j-1] + B[i][j]
#for b in R:
#    print(b)
#for b in B:
#    print(b)
r = 0
for i in range(K):
    for j in range(K):
        t1 = R[i+K][j+K] - (R[i][j+K] + R[i+K][j] - R[i][j])
        t0 = R[i][j]
        t2 = R[2*K][2*K] - (R[i+K][2*K] + R[2*K][j+K] - R[i+K][j+K])
        tt = R[2*K][j] - R[i+K][j]
        ts = R[i][2*K] - R[i][j+K]
        r = max(r, t0+t1+t2+tt+ts)
#        print(i, j, t0,t1,t2)
print(r)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
B = [[0]*(2*K+1) for  _ in range(2*K+1)]
for _ in range(N):
    a, b, c = input().strip().split()
    a, b, c = int(a), int(b), 1 if c == 'B' else 0
    B[(a+K*c)%(2*K)+1][b%(2*K)+1] += 1
R = [[0]*(2*K+1) for  _ in range(2*K+1)]
for i in range(1, 2*K+1):
    for j in range(1, 2*K+1):
        R[i][j] = R[i-1][j] + R[i][j-1] - R[i-1][j-1] + B[i][j]
r = 0
for i in range(K):
    for j in range(K):
        t1 = R[i+K][j+K] - (R[i][j+K] + R[i+K][j] - R[i][j])
        t0 = R[i][j]
        t2 = R[2*K][2*K] - (R[i+K][2*K] + R[2*K][j+K] - R[i+K][j+K])
        tt = R[2*K][j] - R[i+K][j]
        ts = R[i][2*K] - R[i][2*K]
        r = max(r, t0+t1+t2+tt+ts)
#        print(i, j, t0,t1,t2)
print(r)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
input = sys.stdin.readline


def main():
    N, K = map(int, input().split())
    black = defaultdict(int)
    white = defaultdict(int)
    for _ in range(N):
        x, y, c = input().split()
        x = int(x)
        y = int(y)
        if c == 'B':
            x_, y_ = x % (2*K), y % (2*K)
            if x_ < K and y_ < K:
                black[x_ * K + y_] += 1
            elif x_ >= K and y_ < K:
                white[(x_ - K) * +y_] += 1
            elif x_ < K and y_ >= K:
                white[x_ * K + y_ - K] += 1
            else:
                black[(x_ - K)*K + y_ - K] += 1
        else:
            x_, y_ = x % (2 * K), y % (2 * K)
            if x_ < K and y_ < K:
                white[x_*K + y_] += 1
            elif x_ >= K and y_ < K:
                black[(x_ - K)*K + y_] += 1
            elif x_ < K and y_ >= K:
                black[x_*K+ y_ - K] += 1
            else:
                white[(x_ - K)*K + y_ - K] += 1

    cs_b = [[0] * (K+1) for _ in range(K+1)]
    cs_w = [[0] * (K+1) for _ in range(K+1)]
    for i in range(K):
        for j in range(K):
            cs_b[i+1][j+1] = cs_b[i+1][j] + cs_b[i][j+1] - cs_b[i][j] + black[i*K+j]
            cs_w[i + 1][j + 1] = cs_w[i + 1][j] + cs_w[i][j + 1] - cs_w[i][j] + white[i*K+j]

    ans = 0
    for i in range(K):
        for j in range(K):
            ans_b = cs_b[i][j] + cs_b[-1][-1] - cs_b[-1][j] - cs_b[i][-1] + cs_b[i][j]
            ans_w = cs_w[i][-1] - cs_w[i][j] + cs_w[-1][j] - cs_w[i][j]
            ans_w2 = cs_w[i][j] + cs_w[-1][-1] - cs_w[-1][j] - cs_w[i][-1] + cs_w[i][j]
            ans_b2 = cs_b[i][-1] - cs_b[i][j] + cs_b[-1][j] - cs_b[i][j]
            ans = max(ans, ans_b + ans_w, ans_b2 + ans_w2)

    print(ans)
    '''
    print(cs_b)
    print(cs_w)
    print(black)
    print(white)
    '''


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
input = sys.stdin.readline

N, K = map(int, input().split())
black = defaultdict(int)
white = defaultdict(int)
for _ in range(N):
    x, y, c = input().split()
    x = int(x)
    y = int(y)
    if c == 'B':
        x_, y_ = x % (2*K), y % (2*K)
        if x_ < K and y_ < K:
            black[(x_, y_)] += 1
        elif x_ >= K and y_ < K:
            white[(x_ - K, y_)] += 1
        elif x_ < K and y_ >= K:
            white[(x_, y_ - K)] += 1
        else:
            black[(x_ - K, y_ - K)] += 1
    else:
        x_, y_ = x % (2 * K), y % (2 * K)
        if x_ < K and y_ < K:
            white[(x_, y_)] += 1
        elif x_ >= K and y_ < K:
            black[(x_ - K, y_)] += 1
        elif x_ < K and y_ >= K:
            black[(x_, y_ - K)] += 1
        else:
            white[(x_ - K, y_ - K)] += 1

cs_b = [[0] * (K+1) for _ in range(K+1)]
cs_w = [[0] * (K+1) for _ in range(K+1)]
for i in range(K):
    for j in range(K):
        cs_b[i+1][j+1] = cs_b[i+1][j] + cs_b[i][j+1] - cs_b[i][j] + black[(i, j)]
        cs_w[i + 1][j + 1] = cs_w[i + 1][j] + cs_w[i][j + 1] - cs_w[i][j] + white[(i, j)]

ans = 0
for i in range(K):
    for j in range(K):
        ans_b = cs_b[i][j] + cs_b[-1][-1] - cs_b[-1][j] - cs_b[i][-1] + cs_b[i][j]
        ans_w = cs_w[i][-1] - cs_w[i][j] + cs_w[-1][j] - cs_w[i][j]
        ans = max(ans, ans_b + ans_w)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

n, k = [ int(v) for v in input().split() ]
hope = [ [ a for a in input().split() ] for i in range(n) ]

b_board = [ [ 0 for i in range(k)] for j in range(k) ]
w_board = [ [ 0 for i in range(k)] for j in range(k) ]
b_board_2 = [ [ 0 for i in range(k)] for j in range(k) ]
w_board_2 = [ [ 0 for i in range(k)] for j in range(k) ]
b_board_3 = [ [ 0 for i in range(k+1)] for j in range(k+1) ]
w_board_3 = [ [ 0 for i in range(k+1)] for j in range(k+1) ]

for h, w, c in hope:
    if c == "B":
        b_board[int(w)%k][int(h)%k] += 1
    else:
        w_board[int(w)%k][int(h)%k] += 1

for i in range(k):
    b, w = 0, 0
    for j in range(k):
        b += b_board[i][j]
        b_board_2[i][j] = b
        w += w_board[i][j]
        w_board_2[i][j] = w

for i in range(k):
    b, w = 0, 0
    for j in range(k):
        b += b_board_2[j][i]
        b_board_3[j+1][i+1] = b
        w += w_board_2[j][i]
        w_board_3[j+1][i+1] = w

total_b, total_w = b_board_3[k][k], w_board_3[k][k]
ans = 0

for i in range(1,k+1):
    for j in range(1,k+1):
        b1 = b_board_3[i][j] - b_board_3[0][j] - b_board_3[i][0]
        b2 = total_b - b_board_3[i][k] - b_board_3[k][j] + b_board_3[i][j]
        w1 = w_board_3[i][j] - w_board_3[0][j] - w_board_3[i][0]
        w2 = total_w - w_board_3[i][k] - w_board_3[k][j] + w_board_3[i][j]
        ans = max(b1+b2+(total_w-w1-w2),w1+w2+(total_b-b1-b2),ans)

print(ans)