n,k=map(int,input().split())
    l=[input().split() for _ in range(n)]
    w=[[0]*(k+1) for _ in range(k+1)]
    b=[[0]*(k+1) for _ in range(k+1)]
    for i in range(n):
        if ((int(l[i][0])//k+int(l[i][1])//k)%2==0)^(l[i][2]=='W'):
            b[int(l[i][0])%k][int(l[i][1])%k]+=1
        else:
            w[int(l[i][0])%k][int(l[i][1])%k]+=1
     
    for i in range(1,k+1):
        b[0][i]+=b[0][i-1]
        b[i][0]+=b[i-1][0]
        w[0][i]+=w[0][i-1]
        w[i][0]+=w[i-1][0]
    for i in range(1,k+1):
        for j in range(1,k+1):
            b[i][j]+=b[i-1][j]+b[i][j-1]-b[i-1][j-1]
            w[i][j]+=w[i-1][j]+w[i][j-1]-w[i-1][j-1]
     
    ans=0
    for i in range(k+1):
        for j in range(k+1):
            ans=max(ans,b[k][k]-b[i][k]-b[k][j]+2*b[i][j]+w[i][k]+w[k][j]-2*w[i][j])
            ans=max(ans,w[k][k]-w[i][k]-w[k][j]+2*w[i][j]+b[i][k]+b[k][j]-2*b[i][j])
    print(ans)

n,k=map(int,input().split())
l=[input().split() for _ in range(n)]
w=[[0]*(k+1) for _ in range(k+1)]
b=[[0]*(k+1) for _ in range(k+1)]
for i in range(n):
    if ((int(l[i][0])//k+int(l[i][1])//k)%2==0)^(l[i][2]=='W'):
        b[int(l[i][0])%k][int(l[i][1])%k]+=1
    else:
        w[int(l[i][0])%k][int(l[i][1])%k]+=1

for i in range(1,k+1):
    b[0][i]+=b[0][i-1]
    b[i][0]+=b[i-1][0]
    w[0][i]+=w[0][i-1]
    w[i][0]+=w[i-1][0]
for i in range(1,k+1):
    for j in range(1,k+1):
        b[i][j]+=b[i-1][j]+b[i][j-1]-b[i-1][j-1]
        w[i][j]+=w[i-1][j]+w[i][j-1]-w[i-1][j-1]

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

Submission
問題 	D - Checker
ユーザ名 	lpo
投稿日時 	2019/10/11 18:33:24
言語 	PyPy3 (2.4.0)
状態    n,k=map(int,input().split())
    l=[input().split() for _ in range(n)]
    w=[[0]*(k+1) for _ in range(k+1)]
    b=[[0]*(k+1) for _ in range(k+1)]
    for i in range(n):
        if ((int(l[i][0])//k+int(l[i][1])//k)%2==0)^(l[i][2]=='W'):
            b[int(l[i][0])%k][int(l[i][1])%k]+=1
        else:
            w[int(l[i][0])%k][int(l[i][1])%k]+=1
     
    for i in range(1,k+1):
        b[0][i]+=b[0][i-1]
        b[i][0]+=b[i-1][0]
        w[0][i]+=w[0][i-1]
        w[i][0]+=w[i-1][0]
    for i in range(1,k+1):
        for j in range(1,k+1):
            b[i][j]+=b[i-1][j]+b[i][j-1]-b[i-1][j-1]
            w[i][j]+=w[i-1][j]+w[i][j-1]-w[i-1][j-1]
     
    ans=0
    for i in range(k+1):
        for j in range(k+1):
            ans=max(ans,b[k][k]-b[i][k]-b[k][j]+2*b[i][j]+w[i][k]+w[k][j]-2*w[i][j])
            ans=max(ans,w[k][k]-w[i][k]-w[k][j]+2*w[i][j]+b[i][k]+b[k][j]-2*b[i][j])
    print(ans)

n,k=map(int,input().split())
l=[input().split() for _ in range(n)]
w=[[0]*(k+1) for _ in range(k+1)]
b=[[0]*(k+1) for _ in range(k+1)]
for i in range(n):
    if ((int(l[i][0])//k+int(l[i][1])//k)%2==0)^(l[i][2]=='W'):
        b[int(l[i][0])%k][int(l[i][1])%k]+=1
    else:
        w[int(l[i][0])%k][int(l[i][1])%k]+=1

for i in range(1,k+1):
    b[0][i]+=b[0][i-1]
    b[i][0]+=b[i-1][0]
    w[0][i]+=w[0][i-1]
    w[i][0]+=w[i-1][0]
for i in range(1,k+1):
    for j in range(1,k+1):
        b[i][j]+=b[i-1][j]+b[i][j-1]-b[i-1][j-1]
        w[i][j]+=w[i-1][j]+w[i][j-1]-w[i-1][j-1]

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

### FIXED CODE ###

n, k = map(int, input().split())
imos = [[0 for i in range(k)] for j in range(k)]
white = 0
for i in range(n):
    x, y, c = input().split()
    x = int(x)
    y = int(y)
    if c == "W":
        c = 1
    else:
        c = 0
    x %= 2*k
    y %= 2*k

    if x > k-1:
        x -= k
        c += 1
    if y > k-1:
        y -=k
        c += 1
    c %= 2

    if c == 1:
        white += 1
        imos[x][y] -= 1
    else:
        imos[x][y] += 1

for i in range(k):
    for j in range(k-1):
        imos[i][j+1] += imos[i][j]

for j in range(k):
    for i in range(k-1):
        imos[i+1][j] += imos[i][j]

ans = 0
for i in range(k):
    for j in range(k):
        exp = imos[k-1][k-1] - imos[k-1][j] - imos[i][k-1] + 2 * imos[i][j] + white
        ans = max(ans, max(exp, n - exp))

print(ans)