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:
kibok = input().split()
kibo,k = int(kibok[0]),int(kibok[1])
#print(kibo,k)
loop=k*2
kibolist=[[0,0,"W"] for i in range(kibo)]
#print(len(kibox))
for i in range(kibo):
  kibon=input().split()
  kibox,kiboy,kiboc = int(kibon[0]),int(kibon[1]),kibon[2]
#  print(kibox,kiboy,kiboc)
  kibox=kibox%loop
  kiboy=kiboy%loop
#  print(kibox,kiboy,kiboc)
  kibolist[i]=(kibox,kiboy,kiboc)
#for i in range(kibo):
#  print(kibolist[i])
#white
maxkanaukibo=0
kanaukibo=0
m=0
for n in range(k):#0,1,k
  if maxkanaukibo==kibo:
    break
  for l in range(kibo):
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==0:
      if kibolist[l][2]=='W':
        kanaukibo=kanaukibo+1
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==1:
      if kibolist[l][2]=='B':
        kanaukibo=kanaukibo+1
  maxkanaukibo=max(kanaukibo,maxkanaukibo)
  kanaukibo=0
n=0
for m in range(k):#0,1,k
  if maxkanaukibo==kibo:
    break
  for l in range(kibo):
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==0:
      if kibolist[l][2]=='W':
        kanaukibo=kanaukibo+1
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==1:
      if kibolist[l][2]=='B':
        kanaukibo=kanaukibo+1
  maxkanaukibo=max(kanaukibo,maxkanaukibo)
  kanaukibo=0
m=0
for n in range(k):#0,1,k
  if maxkanaukibo==kibo:
    break
  for l in range(kibo):
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==0:
      if kibolist[l][2]=='B':
        kanaukibo=kanaukibo+1
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==1:
      if kibolist[l][2]=='W':
        kanaukibo=kanaukibo+1
  maxkanaukibo=max(kanaukibo,maxkanaukibo)
  kanaukibo=0
n=0
for m in range(k):#0,1,k
  if maxkanaukibo==kibo:
    break
  for l in range(kibo):
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==0:
      if kibolist[l][2]=='B':
        kanaukibo=kanaukibo+1
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==1:
      if kibolist[l][2]=='W':
        kanaukibo=kanaukibo+1
  maxkanaukibo=max(kanaukibo,maxkanaukibo)
  kanaukibo=0
print(maxkanaukibo)

Submission status: Wrong Answer

Solution code:
kibok = input().split()
kibo,k = int(kibok[0]),int(kibok[1])
#print(kibo,k)
loop=k*2
kibolist=[[0,0,"W"] for i in range(kibo)]
#print(len(kibox))
for i in range(kibo):
  kibon=input().split()
  kibox,kiboy,kiboc = int(kibon[0]),int(kibon[1]),kibon[2]
#  print(kibox,kiboy,kiboc)
  kibox=kibox%loop
  kiboy=kiboy%loop
#  print(kibox,kiboy,kiboc)
  kibolist[i]=(kibox,kiboy,kiboc)
#for i in range(kibo):
#  print(kibolist[i])
#white
maxkanaukibo=0
kanaukibo=0
m=0
for n in range(k):#0,1,k
  for l in range(kibo):
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==0:
      if kibolist[l][2]=='W':
        kanaukibo=kanaukibo+1
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==1:
      if kibolist[l][2]=='B':
        kanaukibo=kanaukibo+1
  maxkanaukibo=max(kanaukibo,maxkanaukibo)
  kanaukibo=0
n=0
for m in range(k):#0,1,k
  for l in range(kibo):
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==0:
      if kibolist[l][2]=='W':
        kanaukibo=kanaukibo+1
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==1:
      if kibolist[l][2]=='B':
        kanaukibo=kanaukibo+1
  maxkanaukibo=max(kanaukibo,maxkanaukibo)
  kanaukibo=0
m=0
for n in range(k):#0,1,k
  for l in range(kibo):
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==0:
      if kibolist[l][2]=='B':
        kanaukibo=kanaukibo+1
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==1:
      if kibolist[l][2]=='W':
        kanaukibo=kanaukibo+1
  maxkanaukibo=max(kanaukibo,maxkanaukibo)
  kanaukibo=0
n=0
for m in range(k):#0,1,k
  for l in range(kibo):
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==0:
      if kibolist[l][2]=='B':
        kanaukibo=kanaukibo+1
    if (int((kibolist[l][0]+m)/k)+int((kibolist[l][1]+n)/k))%2==1:
      if kibolist[l][2]=='W':
        kanaukibo=kanaukibo+1
  maxkanaukibo=max(kanaukibo,maxkanaukibo)
  kanaukibo=0
print(maxkanaukibo)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

N, K = map(int, input().split())
KK = 2*K
m = [[0] * (KK+K+1) for i in range(KK+K+1)]
for i in range(N):
    x, y, c = input().split()
    x, y = int(x), int(y)
    if c == 'B':
        y += K
    x %= KK
    y %= KK
    m[y+1][x+1] += 1
    if x < K and y < K:
        m[y+KK+1][x+KK+1] += 1
    elif x < K:
        m[y+1][x+KK+1] += 1
    elif y < K:
        m[y+KK+1][x+1] += 1
for i in range(1, KK+K+1):
    for j in range(KK+K):
        m[i][j+1] += m[i][j]
for j in range(1, KK+K+1):
    for i in range(KK+K):
        m[i+1][j] += m[i][j]
ans = 0
for i in range(K):
    for j in range(K):
        ans = max(ans, 2*m[i+K][j+K]-m[i+K][j]-m[i][j+K]+m[i][j]+m[i+KK][j+KK]-m[i+KK][j+K]-m[i+K][j+KK])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
XYC = [[int(x)%(2*K),int(y)%(2*K),int(c=="B")] for x,y,c in [input().split() for _ in [0]*N]]

score = [[[0]*(3*K+1) for _ in [0]*(3*K+1)] for _ in [0]*2]
for x,y,c in XYC:
    x+=1
    y+=1
    score[c][y][x] += 1
    if x<K: score[c][y][x+2*K] += 1
    if y<K: score[c][y+2*K][x] += 1
    if x<K and y<K: score[c][y+2*K][x+2*K] += 1

S = score
for c in range(2):
    for j in range(3*K):
        for i in range(3*K):
            S[c][i+1][j+1] = S[c][i][j+1] + score[c][i][j]
    for i in range(3*K):
        for j in range(3*K):
            S[c][i+1][j+1] += S[c][i+1][j]

def get_sum(c,si,sj,i,j):
    i+=1
    j+=1
    return S[c][i][j] + S[c][si][sj] - S[c][i][sj] - S[c][si][j]

ans = 0
for i in range(K):
    for j in range(2*K):
        tmp = 0
        tmp += get_sum(0,i,j,i+K-1,j+K-1)
        tmp += get_sum(1,i+K,j,i+2*K-1,j+K-1)
        j = (j+K)%(2*K)
        tmp += get_sum(1,i,j,i+K-1,j+K-1)
        tmp += get_sum(0,i+K,j,i+2*K-1,j+K-1)
        ans = max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
from subprocess import*
call(('julia','-e',"""
const lines=readlines()
input()=shift!(lines)
int(s)=parse(Int16,s)
intLine()=map(int,split(input()))
function nextLine(k)
    x,y,c=split(input())
    int(x)%2k+1,(int(y)+(c=="W")k)%2k+1
end
function solve(k,g)
    m=0
    for i=1:2k,j=1:2k
        a=0
        for x=-1:2,y=-1:2
            (x+y)%2!=0&&continue
            v,w=i+x*k,j+y*k
            v>0<w&&(a+=g[min(2k,v)][min(2k,w)])
            v>k&&w>0&&(a-=g[min(2k,v-k)][min(2k,w)])
            w>k&&v>0&&(a-=g[min(2k,v)][min(2k,w-k)])
            v>k<w&&(a+=g[min(2k,v-k)][min(2k,w-k)])
        end
        m=max(m,a)
    end
    m
end
function main()
    n,k=intLine()
    g=Array{Int16,1}[[0for j=1:2k]for i=1:2k]
    for i=1:n
        x,y=nextLine(k)
        g[x][y]+=1
    end
    println(solve(k,cumsum(map(cumsum,g))))
end
main()
"""))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math

# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
def main():
    N,K = map(int,input().split())
    maps = [[0 for _ in range(2*K)] for _ in range(2*K)]
    
    for i in range(N):
        inp = input().split(" ")
        tmp1 = int(inp[0])
        tmp2 = int(inp[1])
        if inp[2] == "W":
            tmp2 += K
        tmp1 = tmp1 % (2*K)
        tmp2 = tmp2 % (2*K)
        maps[tmp1][tmp2] += 1
        
    for i in range(2*K):
        for j in range(2*K):
            if i == 0:
                if j == 0:
                    continue
                maps[i][j] = maps[i][j] + maps[i][j-1]
                continue
            if j == 0:
                maps[i][j] = maps[i][j] + maps[i-1][j]
                continue
            maps[i][j] = maps[i][j] + maps[i][j-1] + maps[i-1][j] - maps[i-1][j-1]
            
    ans = 0
    for i in range(K):
        for j in range(K):
            tmp_i = i-1
            tmp_j = j-1
            tmptmp_i = i+K-1
            tmptmp_j = j+K-1
            
            hidari_ue = (tmp_i, tmp_j)
            migi_ue = (tmp_i, tmptmp_j)
            hidari_shita = (tmptmp_i, tmp_j)
            migi_shita = (tmptmp_i, tmptmp_j)
            
            tmp = 0
            if i == 0 and j == 0:
                tmp = maps[migi_shita[0]][migi_shita[1]]
            elif i == 0:
                tmp = maps[migi_shita[0]][migi_shita[1]] - maps[hidari_shita[0]][hidari_shita[1]]
            elif j == 0:
                tmp = maps[migi_shita[0]][migi_shita[1]] - maps[migi_ue[0]][migi_ue[1]]
            else:
                tmp = maps[migi_shita[0]][migi_shita[1]] - maps[migi_ue[0]][migi_ue[1]] - maps[hidari_shita[0]][hidari_shita[1]] + maps[hidari_ue[0]][hidari_ue[1]]
            if ans < tmp:
                ans = tmp
        
    print(ans)
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import math

# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
def main():
    N,K = map(int,input().split())
    maps = [[0 for _ in range(4*K)] for _ in range(4*K)]
    
    for i in range(N):
        inp = input().split(" ")
        tmp1 = int(inp[0])
        tmp2 = int(inp[1])
        if inp[2] == "W":
            tmp2 += K
        tmp1 = tmp1 % (2*K)
        tmp2 = tmp2 % (2*K)
        maps[tmp1][tmp2] += 1
        
        maps[tmp1+(2*K)][tmp2+(2*K)] += 1
        maps[tmp1][tmp2+(2*K)] += 1
        maps[tmp1+(2*K)][tmp2] += 1
        
    for i in range(4*K):
        for j in range(4*K):
            if i == 0:
                if j == 0:
                    continue
                maps[i][j] = maps[i][j] + maps[i][j-1]
                continue
            if j == 0:
                maps[i][j] = maps[i][j] + maps[i-1][j]
                continue
            maps[i][j] = maps[i][j] + maps[i][j-1] + maps[i-1][j] - maps[i-1][j-1]
            
    ans = 0
    for i in range(2*K):
        for j in range(2*K):
            tmp_i = i-1
            tmp_j = j-1
            tmptmp_i = i+K-1
            tmptmp_j = j+K-1
            
            hidari_ue = (tmp_i, tmp_j)
            migi_ue = (tmp_i, tmptmp_j)
            hidari_shita = (tmptmp_i, tmp_j)
            migi_shita = (tmptmp_i, tmptmp_j)
            
            tmp = 0
            if i == 0 and j == 0:
                tmp = maps[migi_shita[0]][migi_shita[1]]
            elif i == 0:
                tmp = maps[migi_shita[0]][migi_shita[1]] - maps[hidari_shita[0]][hidari_shita[1]]
            elif j == 0:
                tmp = maps[migi_shita[0]][migi_shita[1]] - maps[migi_ue[0]][migi_ue[1]]
            else:
                tmp = maps[migi_shita[0]][migi_shita[1]] - maps[migi_ue[0]][migi_ue[1]] - maps[hidari_shita[0]][hidari_shita[1]] + maps[hidari_ue[0]][hidari_ue[1]]
            if ans < tmp:
                ans = tmp
        
    print(ans)
	
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate
input = sys.stdin.readline
def main():
    N, K = map( int ,input().split())
    TK = K*2
    B = [[0]*(K*2) for _ in range(TK)]
    for _ in range(N):
        x, y, c = input().split()
        x, y = int(x), int(y)
        if c == "B":
            B[x%TK][y%TK] += 1
        else:
            B[x%TK][(y+K)%TK] += 1
    C = [[0]*(TK+1)] + [[0] + list( accumulate(B[i])) for i in range(TK)]
    for i in range(TK):
        for j in range(TK+1):
            C[i+1][j] += C[i][j]
    def get(i1, j1, i2, j2):
        return C[i2][j2] - C[i1][j2] - C[i2][j1] + C[i1][j1]
    ans = 0
    for i in range(K):
        for j in range(K):
            if ans < get(0,0,i,j) + get(i+K, 0, TK, j) + get(i, j, i+K, j+K) + get(0, j+K, i, TK) + get(K+i, K+j, TK, TK):
                ans = get(0,0,i,j) + get(i+K, 0, TK, j) + get(i, j, i+K, j+K) + get(0, j+K, i, TK) + get(K+i, K+j, TK, TK)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate
input = sys.stdin.readline
def main():
    N, K = map( int ,input().split())
    TK = K*2
    B = [[0]*(K*2) for _ in range(TK)]
    for _ in range(N):
        x, y, c = input().split()
        x, y = int(x), int(y)
        if c == "B":
            B[x%TK][y%TK] += 1
        else:
            B[x%TK][(y+K)%TK] += 1
    C = [[0]*(TK+1)] + [[0] + list( accumulate(B[i])) for i in range(TK)]
    for i in range(TK):
        for j in range(TK+1):
            C[i+1][j] += C[i][j]
    def get(i1, j1, i2, j2):
        return C[i2][j2] - C[i1][j2] - C[i2][j1] + C[i1][j1]
    ans = 0
    for i in range(K+1):
        for j in range(K+1):
            if ans < get(0,0,i,j) + get(i+K, 0, TK, j) + get(i, j, i+K, j+K) + get(0, j+K, i, TK) + get(K+i, K+j, TK, TK):
                ans = get(0,0,i,j) + get(i+K, 0, TK, j) + get(i, j, i+K, j+K) + get(0, j+K, i, TK) + get(K+i, K+j, TK, TK)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
r=[[0]*k for _ in range(k)]
s=[[0]*k for _ in range(k)]
for i in range(n):
    x,y,z=input().split()
    x,y=int(x)%k,int(y)%k
    if z=='B':
        r[x][y]+=1
    else:
        s[x][y]+=1
    
for i in range(k):
    for j in range(k-1):
        r[i][j+1]+=r[i][j]
        s[i][j+1]+=s[i][j]
for i in range(k-1):
    for j in range(k):
        r[i+1][j]+=r[i][j]
        s[i+1][j]+=s[i][j]

for i in r:
    print(*i)

print()

for i in s:
    print(*i)

ans=0
for i in range(k):
    for j in range(k):
        b=r[k-1][k-1]-r[i][k-1]-r[k-1][j]+3*r[i][j]
        w=s[i][k-1]+s[k-1][j]+s[i][j]
        ans=max(ans,b+w)
        print(i,j,b,w)
print(ans)

Submission status: Wrong Answer

Solution code:
#(6,6)周期
#36種の希望

import numpy as np
N,K = map(int,input().split())
KK = 2*K
hope = np.zeros((KK,KK),dtype=np.int32)

XYC = [input().split() for _ in range(N)]
X,Y,C = zip(*XYC)
X = np.array(X, dtype=np.int32)
Y = np.array(Y, dtype=np.int32)
C = np.array(C, dtype='U1')

X[C == 'B'] -= K

X %= KK
Y %= KK

for x,y in zip(X,Y):
  hope[X,Y] += 1

# (x,y)を希望された場合、[x,x+K), [y,y+K)に終点があるべき
# 区間の重なってる枚数を集計。1,0,0,-1, cumsum
A = np.zeros((KK+K,KK+K),dtype=np.int32)
A[:-K,:-K] = hope[:]
A[K:,:-K] += -hope[:]
A[:-K,K:] += -hope[:]
A[K:,K:] += hope[:]
A = A.cumsum(axis = 0).cumsum(axis = 1)

# 実質同じ場所をまとめる
B = A[:,:KK]
B[:,:K] += A[:,KK:] #(3K,2K)
A = B[:KK,:]
A[:K,:] += B[KK:,:] #(2K,2K)
B = A[:K,:] #(K,2K)
B[:,:K] += A[K:,K:]
B[:,K:] += A[K:,:K]

answer = B.max()
print(answer)

Submission status: Wrong Answer

Solution code:
import numpy as np
n,k=map(int,input().split())
w=np.zeros((2*k,2*k),dtype="int64")
for i in range(n):
    x,y,c=input().split()
    x,y=int(x),int(y)
    if(c=="B"):
        x-=k
    x%=2*k
    y%=2*k
    w[x][y]+=1
ans=0
w=np.tile(w,(2,2))
for i in range(k):
    for j in range(k):
        ans=max(np.sum(w[i:i+k,j:j+k])+np.sum(w[i+k:i+2*k,j+k:j+2*k]),ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
w=[[0]*12 for i in range(6)]
for i in range(n):
    x,y,c=input().split()
    x,y=int(x),int(y)
    if(c=="B"):
        x-=3
    x%=6
    y%=6
    w[x][y]+=1
    w[x][y+6]+=1
ans=0
w+=w
for i in range(6):
    for j in range(6):
        tmp=0
        for k in w[i+3:i+6]:
            tmp+=sum(k[j+3:j+6])
        for k in w[i:i+3]:
            tmp+=sum(k[j:j+3])
        ans=max(tmp,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
w=[[0]*6 for i in range(6)]
for i in range(n):
    x,y,c=input().split()
    x,y=int(x),int(y)
    if(c=="B"):
        x-=3
    x%=6
    y%=6
    w[x][y]+=1
ans=0
for i in range(3):
    for j in range(3):
        tmp=0
        for k in w[:i]+w[i+3:]:
            tmp+=sum(k[j:j+3])
        for k in w[i:i+3]:
            tmp+=sum(k[:j]+k[j+3:])
        ans=max(tmp,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
B=[[0]*(2*K) for i in range(2*K)]
W=[[0]*(2*K) for i in range(2*K)]
w=0
for i in range(N):
    x,y,c=map(str,input().split())
    x=int(x)
    y=int(y)
    if c=="B":
        B[y%(2*K)][x%(2*K)]=+1
    else:
        w+=1
        W[y%(2*K)][x%(2*K)]+=1
sumb=[[0]*(2*K+1) for i in range(2*K+1)]
sumw=[[0]*(2*K+1) for i in range(2*K+1)]
for i in range(2*K):
    for j in range(2*K):
        sumb[i+1][j+1]=sumb[i][j+1]+sumb[i+1][j]-sumb[i][j]+B[i][j]
        sumw[i+1][j+1]=sumw[i][j+1]+sumw[i+1][j]-sumw[i][j]+W[i][j]


ans=0
for i in range(K):
    for j in range(K):
        for _ in range(2):
            s=0
            s+=sumb[i][j]-sumb[i][0]-sumb[0][j]+sumb[0][0]#
            s+=sumb[i+K][j+K]-sumb[i][j+K]-sumb[i+K][j]+sumb[i][j]
            s+=sumb[i][2*K]-sumb[0][2*K]-sumb[i][j+K]+sumb[0][j+K]#
            s+=sumb[2*K][j]-sumb[i+K][j]-sumb[2*K][0]+sumb[i+K][0]#
            s+=sumb[2*K][2*K]-sumb[i+K][2*K]-sumb[2*K][j+K]+sumb[i+K][j+K]#

            s+=sumw[i][j+K]-sumw[0][j+K]-sumw[i][j]+sumw[0][j]#
            s+=sumw[i+K][j]-sumw[i][j]-sumw[i+K][0]+sumw[i][0]#
            s+=sumw[2*K][j+K]-sumw[i+K][j+K]-sumw[2*K][j]+sumw[i+K][j]#
            s+=sumw[i+K][2*K]-sumw[i+K][j+K]-sumw[i][2*K]+sumw[i][j+K]#

            ans=max(ans,s)
            sumb,sumw=sumw,sumb

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
B=[[0]*(2*K) for i in range(2*K)]
W=[[0]*(2*K) for i in range(2*K)]
w=0
for i in range(N):
    x,y,c=map(str,input().split())
    x=int(x)
    y=int(y)
    if c=="B":
        B[y%(2*K)][x%(2*K)]=+1
    else:
        w+=1
        W[y%(2*K)][x%(2*K)]=+1
sumb=[[0]*(2*K+1) for i in range(2*K+1)]
sumw=[[0]*(2*K+1) for i in range(2*K+1)]
for i in range(2*K):
    for j in range(2*K):
        sumb[i+1][j+1]+=sumb[i][j+1]+sumb[i+1][j]-sumb[i][j]+B[i][j]
        sumw[i+1][j+1]+=sumw[i][j+1]+sumw[i+1][j]-sumw[i][j]+W[i][j]


ans=0
for i in range(K):
    for j in range(K):
        for _ in range(2):
            s=0
            s+=sumb[i][j]-sumb[i][0]-sumb[0][j]+sumb[0][0]#
            s+=sumb[i+K][j+K]-sumb[i][j+K]-sumb[i+K][j]+sumb[i][j]
            s+=sumb[i][2*K]-sumb[0][2*K]-sumb[i][j+K]+sumb[0][j+K]#
            s+=sumb[2*K][j]-sumb[i+K][j]-sumb[2*K][0]+sumb[i+K][0]#
            s+=sumb[2*K][2*K]-sumb[i+K][2*K]-sumb[2*K][j+K]+sumb[i+K][j+K]#

            s+=sumw[i][j+K]-sumw[0][j+K]-sumw[i][j]+sumw[0][j]#
            s+=sumw[i+K][j]-sumw[i][j]-sumw[i+K][0]+sumw[i][0]#
            s+=sumw[2*K][j+K]-sumw[i+K][j+K]-sumw[2*K][j]+sumw[i+K][j]#
            s+=sumw[i+K][2*K]-sumw[i+K][j+K]-sumw[i][2*K]+sumw[i][j+K]#

            ans=max(ans,s)
            sumb,sumw=sumw,sumb

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
B=[[0]*(2*K) for i in range(2*K)]
W=[[0]*(2*K) for i in range(2*K)]
w=0
for i in range(N):
    x,y,c=map(str,input().split())
    x=int(x)
    y=int(y)
    if c=="B":
        B[y%(2*K)][x%(2*K)]=+1
    else:
        w+=1
        W[y%(2*K)][x%(2*K)]=+1
sumb=[[0]*(2*K+1) for i in range(2*K+1)]
sumw=[[0]*(2*K+1) for i in range(2*K+1)]
for i in range(2*K):
    for j in range(2*K):
        sumb[i+1][j+1]=sumb[i][j+1]+sumb[i+1][j]-sumb[i][j]+B[i][j]
        sumw[i+1][j+1]=sumw[i][j+1]+sumw[i+1][j]-sumw[i][j]+W[i][j]

ans=0
for i in range(K):
    for j in range(K):
        for _ in range(2):
            s=0
            s+=sumb[i][j]-sumb[i][0]-sumb[0][j]+sumb[0][0]#
            s+=sumb[i+K][j+K]-sumb[i][j+K]-sumb[i+K][j]+sumb[i][j]
            s+=sumb[i][2*K]-sumb[0][2*K]-sumb[i][j+K]+sumb[0][j+K]#
            s+=sumb[2*K][j]-sumb[i+K][j]-sumb[2*K][0]+sumb[i+K][0]#
            s+=sumb[2*K][2*K]-sumb[i+K][2*K]-sumb[2*K][j+K]+sumb[i+K][j+K]#

            s+=sumw[i][j+K]-sumw[0][j+K]-sumw[i][j]+sumw[0][j]#
            s+=sumw[i+K][j]-sumw[i][j]-sumw[i+K][0]+sumw[i][0]#
            s+=sumw[2*K][j+K]-sumw[i+K][j+K]-sumw[2*K][j]+sumw[i+K][j]#
            s+=sumw[i+K][2*K]-sumw[i+K][j+K]-sumw[i][2*K]+sumw[i][j+K]#

            ans=max(ans,s)
            sumb,sumw=sumw,sumb

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
B=[[0]*(2*K) for i in range(2*K)]
W=[[0]*(2*K) for i in range(2*K)]
w=0
for i in range(N):
    x,y,c=map(str,input().split())
    x=int(x)
    y=int(y)
    if c=="B":
        B[y%(2*K)][x%(2*K)]=+1
    else:
        w+=1
        W[y%(2*K)][x%(2*K)]=+1
sumb=[[0]*(2*K+1) for i in range(2*K+1)]
sumw=[[0]*(2*K+1) for i in range(2*K+1)]
for i in range(2*K):
    for j in range(2*K):
        sumb[i+1][j+1]=sumb[i][j+1]+sumb[i+1][j]-sumb[i][j]+B[i][j]
        sumw[i+1][j+1]=sumw[i][j+1]+sumw[i+1][j]-sumw[i][j]+W[i][j]
bm=sumb
wm=sumw
ma = 0
for i in range(K):
  for j in range(K):
    for _ in range(2):
      s = 0
      s += bm[i][j]-bm[i][0]-bm[0][j]+bm[0][0]
      s += wm[K+i][j]-wm[K+i][0]-wm[i][j]+wm[i][0]
      s += wm[i][K+j]-wm[i][j]-wm[0][K+j]+wm[0][j]
      s += bm[i+K][j+K]-bm[i+K][j]-bm[i][j+K]+bm[i][j]
      s += bm[i][2*K]-bm[0][2*K]-bm[i][j+K]+bm[0][j+K]
      s += bm[2*K][j]-bm[2*K][0]-bm[i+K][j]+bm[i+K][0]
      s += wm[2*K][j+K]-wm[2*K][j]-wm[K+i][j+K]+wm[K+i][j]
      s += wm[i+K][2*K]-wm[i][2*K]-wm[K+i][j+K]+wm[i][j+K]
      s += bm[2*K][2*K]-bm[2*K][j+K]-bm[i+K][2*K]+bm[i+K][j+K]
      ma = max(ma,s)
      bm,wm=wm,bm
print (ma)

Submission status: Wrong Answer

Solution code:
N,K = [ int(it) for it in input().split() ]
K2 = 2*K
li = [ (lambda x,y,z:[int(x)%K2,int(y)%K2,0 if z=="W" else 1] )(*(input().split())) for i in range(N)]

bm = [[0]*(K2+1) for i in range(K2+1)]
wm = [[0]*(K2+1) for i in range(K2+1)]
for x,y,z in li:
  if (z==0):
    wm[x+1][y+1] += 1
  else:
    bm[x+1][y+1] += 1

for i in range(1,2*K+1):
  for j in range(1,2*K+1):
    bm[i][j]=bm[i-1][j]+bm[i][j-1]-bm[i-1][j-1]+bm[i][j]
    wm[i][j]=wm[i-1][j]+wm[i][j-1]-wm[i-1][j-1]+wm[i][j]
sumb=bm
sumw=wm
ans=0
for i in range(K):
    for j in range(K):
        for _ in range(2):
            s=0
            s+=sumb[i][j]-sumb[i][0]-sumb[0][j]+sumb[0][0]#
            s+=sumb[i+K][j+K]-sumb[i][j+K]-sumb[i+K][j]+sumb[i][j]#
            s+=sumb[i][2*K]-sumb[0][2*K]-sumb[i][j+K]+sumb[0][j+K]#
            s+=sumb[2*K][j]-sumb[i+K][j]-sumb[2*K][0]+sumb[i+K][0]#
            s+=sumb[2*K][2*K]-sumb[i+K][2*K]-sumb[2*K][j+K]+sumb[i+K][j+K]#

            s+=sumw[i][j+K]-sumw[0][j+K]-sumw[i][j]+sumw[0][j]#
            s+=sumw[i+K][j]-sumw[i][j]-sumw[i+K][0]+sumw[i][0]#
            s+=sumw[2*K][j+K]-sumw[i+K][j+K]-sumw[2*K][j]+sumw[i+K][j]#
            s+=sumw[i+K][2*K]-sumw[i+K][j+K]-sumw[i][2*K]-sumw[i][j+K]#

            ans=max(ans,s)
            sumb,sumw=sumw,sumb

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
B=[[0]*(2*K) for i in range(2*K)]
W=[[0]*(2*K) for i in range(2*K)]
w=0
for i in range(N):
    x,y,c=map(str,input().split())
    x=int(x)
    y=int(y)
    if c=="B":
        B[y%(2*K)][x%(2*K)]=+1
    else:
        w+=1
        W[y%(2*K)][x%(2*K)]=+1
sumb=[[0]*(2*K+1) for i in range(2*K+1)]
sumw=[[0]*(2*K+1) for i in range(2*K+1)]
for i in range(2*K):
    for j in range(2*K):
        sumb[i+1][j+1]=sumb[i][j+1]+sumb[i+1][j]-sumb[i][j]+B[i][j]
        sumw[i+1][j+1]=sumw[i][j+1]+sumw[i+1][j]-sumw[i][j]+W[i][j]
ans=0
for i in range(K):
    for j in range(K):
        for _ in range(2):
            s=0
            s+=sumb[i][j]-sumb[i][0]-sumb[0][j]+sumb[0][0]#
            s+=sumb[i+K][j+K]-sumb[i][j+K]-sumb[i+K][j]+sumb[i][j]#
            s+=sumb[i][2*K]-sumb[0][2*K]-sumb[i][j+K]+sumb[0][j+K]#
            s+=sumb[2*K][j]-sumb[i+K][j]-sumb[2*K][0]+sumb[i+K][0]#
            s+=sumb[2*K][2*K]-sumb[i+K][2*K]-sumb[2*K][j+K]+sumb[i+K][j+K]#

            s+=sumw[i][j+K]-sumw[0][j+K]-sumw[i][j]+sumw[0][j]#
            s+=sumw[i+K][j]-sumw[i][j]-sumw[i+K][0]+sumw[i][0]#
            s+=sumw[2*K][j+K]-sumw[i+K][j+K]-sumw[2*K][j]+sumw[i+K][j]#
            s+=sumw[i+K][2*K]-sumw[i+K][j+K]-sumw[i][2*K]-sumw[i][j+K]#

            ans=max(ans,s)
            sumb,sumw=sumw,sumb

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = [ int(it) for it in input().split() ]
K2 = 2*K
li = [ (lambda x,y,z:[int(x)%K2,int(y)%K2,0 if z=="W" else 1] )(*(input().split())) for i in range(N)]

bm = [[0]*(K2+1) for i in range(K2+1)]
wm = [[0]*(K2+1) for i in range(K2+1)]
for x,y,z in li:
  if (z==0):
    wm[x+1][y+1] += 1
  else:
    bm[x+1][y+1] += 1
for i in range(1,K+1):
  for j in range(1,K+1):
    bm[i][j]=bm[i-1][j]+bm[i][j-1]-bm[i-1][j-1]+bm[i][j]
    wm[i][j]=wm[i-1][j]+wm[i][j-1]-wm[i-1][j-1]+wm[i][j]

ma = 0
for i in range(K):
  for j in range(K):
    for _ in range(2):
      s = 0
      s += bm[i][j]-bm[i][0]-bm[0][j]+bm[0][0]
      s += wm[K+i][j]-wm[K+i][0]-wm[i][j]+wm[i][0]
      s += wm[i][K+j]-wm[i][j]-wm[0][K+j]+wm[0][j]
      s += bm[i+K][j+K]-bm[i+K][j]-bm[i][j+K]+bm[i][j]
      s += bm[i][2*K]-bm[0][2*K]-bm[i][j+K]+bm[0][j+K]
      s += bm[2*K][j]-bm[2*K][0]-bm[i+K][j]+bm[i+K][0]
      s += wm[2*K][j+K]-wm[2*K][j]-wm[K+i][j+K]+wm[K+i][j]
      s += wm[i+K][2*K]-wm[i][2*K]-wm[K+i][j+K]+wm[i][j+K]
      s += bm[2*K][2*K]-bm[2*K][j+K]-bm[i+K][2*K]+bm[i+K][j+K]
      #print ("saidai",s)
      ma = max(ma,s)
      bm,wm=wm,bm
print (ma)

Submission status: Wrong Answer

Solution code:
N,K = [int(x) for x in input().split()]
xyc = [[x for x in input().split()]for _ in range(N)]
xc = []
ans = 0

for x in xyc:
    X = int(x[0])%(2*K)
    Y = int(x[1])%(2*K)
    if(Y<K):
        xc.append([X,x[2]])
    else:
        if(x[2]=='B'):
            xc.append([X,'W'])
        else:
            xc.append([X,'B'])

for i in range(2*K-1):
    cnt = 0
    for x in xc:
        if(i<K):
            if(x[1]=='B' and (i<=x[0] and x[0]<i+K)):
                cnt += 1
            elif(x[1]=='W' and not (i<=x[0] and x[0]<i+K)):
                cnt += 1
        else:
            if(x[1]=='W' and (i-K<=x[0] and x[0]<i)):
                cnt += 1
            elif(x[1]=='B' and not (i-K<=x[0] and x[0]<i)):
                cnt += 1
    ans = max(ans,cnt)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
imos=[[0]*(k*2+1) for _ in range(k*2+1)]
def kukan(z,  c):
    if c=='B':
        if 0<z<k:
            return [(0, k-z), (k*2-z, k*2)]
        elif z==0:
            return [(0, k)]
        else:
            return [(k*2-z, k*3-z)]
    else:
        if 0<=z<=k:
            return [(k-z, k*2-z)]
        else:
            return [(0, k*2-z), (k*3-z, k*2)]

for i in range(n):
    x, y, c = input().split()
    x=int(x)%(k*2)
    y=int(y)%(k*2)
    kukanx=kukan(x, c)  
    kukany=kukan(y, c)
    for kx in kukanx:
        for ky in kukany:
            imos[kx[0]][ky[0]]+=1
            imos[kx[0]][ky[1]]-=1
            imos[kx[1]][ky[0]]-=1
            imos[kx[1]][ky[1]]+=1
def sum_imos(l):
    w=len(l)
    h=len(l[0])
    for i in range(w):
        for j in range(1, h):
            l[i][j]+=l[i][j-1]
    for i in range(1, w):
        for j in range(h):
            l[i][j]+=l[i-1][j]
    return l

imos=sum_imos(imos)
print(max(max(imos[i]) for i in range(k*2+1) ))

Submission status: Wrong Answer

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

S = [[0] * (2*K+1) for _ in range(2*K+1)]
a = [[0] * (2*K) for _ in range(2*K)]
for i in range(N):
    x, y, t = input().split()
    x = int(x)
    y = int(y)
    if t == 'B':
        x += K
    x = x % (2*K)
    y = y % (2*K)
    a[x][y] += 1

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


ans = 0
rans = N

for i in range(K):
    for j in range(K):
        a = S[i+1+K][j+1+K] - S[i][j+1+K] - S[i+1+K][j] + S[i][j]
        b = S[i][j] - S[i][0] - S[j][0] + S[0][0]
        ans = max(ans, a+b)
        rans = min(rans, a+b)

print(max(ans, N - rans))

Submission status: Wrong Answer

Solution code:
import math
import sys

nn = [x.strip() for x in sys.stdin.readlines()]

fi = nn.pop(0).split()
n = [x.split() for x in nn]
#print(n)

k = int(fi[1])
kk = k*2
brd = [[ 0 for x in range(k)] for y in range(k)]

for i in n:
    sgn = 1
    if i[2] == 'B':
        sgn *= -1
    if (int(i[0]))% kk > k:
        sgn *= -1
    if (int(i[1])) % kk > k:
        sgn *= -1
    
    brd[int(i[0])% k ][int(i[1]) % k] += sgn


#sm = [[0 for x in range(k)] for y in range(k)]
sm = sum([sum(x) for x in brd])
#print(sm)
ans = 0

for ox in range(k+1):
    for oy in range(k+1):
        #print(sum([x[:oy] for x in bbd[ox:]]+ [x[oy:]for x in bbd[:ox]],[]))
        sb = sum(sum([x[:oy] for x in brd[ox:]]+ [x[oy:]for x in brd[:ox]],[]))
    ans = max(ans,sm+sb,sm-2*sb)

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import sys

n = [x.strip() for x in sys.stdin.readlines()]
print(n)
fi = n.pop(0).split()


k = int(fi[1])
print(k)
kk = k*2
print(kk)
brd = [[ 0 for x in range(k)] for y in range(k)]

for j in n:
    print(j)
    i = str(j).split()
    sgn = 1
    if i[2] == 'B':
        sgn *= -1
    if (int(i[0]))% kk > k:
        sgn *= -1
    if (int(i[1])) % kk > k:
        sgn *= -1
    
    brd[int(i[0])% k ][int(i[1]) % k] += sgn


#sm = [[0 for x in range(k)] for y in range(k)]
sm = sum([sum(x) for x in brd])
#print(sm)
ans = 0

for ox in range(k):
    if not ox % 100: print(ox)
    for oy in range(k):
        sb = sum(sum( brd[ox:][oy:]+ brd[ox:][oy:],[]))
    ans = max(ans,sm+sb)

print(ans)