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: Runtime Error

Solution code:
N,K = map(int, input().split())
 
hope = []
for i in range(N):
    a,b,c = input().split()
    a = int(a)
    b = int(b)
    if c == 'B':
        c = True
    else:
        c = False
    d = [a, b, c]
    hope.append(d)
 
import numpy as np
 
hope = np.array(hope)
    
for i in range(N):
    hope[i][0] %= (K*2)
    hope[i][1] %= (K*2)
    
for i in range(N):
    if hope[i][0] >= K:
        hope[i][0] -= K
        hope[i][2] = not(hope[i][2])
    
    if hope[i][1] >= K:
        hope[i][1] -= K
        hope[i][2] = not(hope[i][2])
        
import copy
 
color_map = []
map = []
 
for i in range(K):
    map.append(True)
 
for i in range(K):
    color_map.append(copy.deepcopy(map))
    
color_map = np.array(color_map)
    
def slice_x():
    temp = []
    for i in range(K):
        temp.append(not(color_map[i][0]))
    
    if K > 1:
        for i in range(K):
            for j in range(K-1):
                color_map[i][j] = color_map[i][j+1]
    
    for i in range(K):
        color_map[i][K-1] = temp[i]
        
    return
 
def slice_y():
    temp = np.copy(color_map[0])
    
    for i in range(K):
        temp[i] = not(temp[i])
    
    if K > 1:
        for i in range(K-1):
            color_map[i] = color_map[i+1]
            
    color_map[K-1] = temp
    
    return
  
black_map = np.zeros((5,5), int)
white_map = np.zeros((5,5), int)

for i in range(N):
    if hope[i][2] == True:
        black_map[hope[i][0]][hope[i][1]] += 1
    else:
        white_map[hope[i][0]][hope[i][1]] += 1
  
sati = 0
max_sati = 0
 
for k in range(K):
    for j in range(K):
        sati += np.sum(black_map * np.logical_not(np.logical_xor(color_map, black_map)))
        sati += np.sum(white_map * np.logical_not(np.logical_xor(color_map, white_map)))
 
        if int(N/2) >= sati:
            sati = N - sati
 
        if max_sati < sati:
            max_sati = sati
        
        sati = 0
            
        slice_x()
    slice_y()
    
print(max_sati)

Submission status: Runtime Error

Solution code:
import gc
n, k = map(int, input().split())
k2 = k * 2
b = [[0] * (k * 3) for _ in range(k * 3)]
for i in range(n):
    x, y, c = input().split()
    x = int(x) % k2
    y = int(y) % k2
    if c == 'W':
        x = (x + k) % k2

    b[x][y] += 1
    if x < k:
        b[x + k2][y] += 1
    if y < k:
        b[x][y + k2] += 1
    if x < k and y < k:
        b[x + k2][y + k2] += 1

b_cs = [[0] * (k * 3 + 2) for _ in range(k * 3 + 2)]
for i in range(k * 3 + 1):
    for j in range(k * 3 + 1):
        b_cs[i + 1][j +
                    1] = b_cs[i + 1][j] + b_cs[i][j + 1] - b_cs[i][j] + b[i][j]


def func_b(x, y):
    return b_cs[x + k][y + k] - b_cs[x + k][y] - b_cs[x][y + k] + b_cs[x][y]


# print('black')
# print(*b, sep='\n')
# print(*b_cs, sep='\n')

ans = 0
for i in range(k + 1):
    for j in range(k + 1):
        cand = 0
        cand += func_b(i, j)
        cand += func_b(i + k, j + k)
        # print(i, j, cand)
        ans = max(ans, cand)

print(ans)

Submission status: Runtime Error

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

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()
mod = K * 2
XY = []
MAX = K*6 + 7
acc = list2d(MAX, MAX, 0)
for i in range(N):
    x, y, c = input().split()
    x = int(x)
    y = int(y)
    if c == 'B':
        x += K
    for a1 in range(3):
        for a2 in range(3):
            for b in [0, K]:
                if c == 'B':
                    b = -b
                x1 = x + a1 * K*2 + b
                y1 = y + a2 * K*2 + b
                x2 = x1 + K
                y2 = y1 + K
                x2 = min(x2, MAX-1)
                y2 = min(y2, MAX-1)
                acc[x1][y1] += 1
                acc[x2][y2] += 1
                acc[x1][y2] -= 1
                acc[x2][y1] -= 1

for i in range(MAX):
    acc[i] = list(accumulate(acc[i]))
for j in range(MAX):
    for i in range(MAX-1):
        acc[i+1][j] += acc[i][j]
mx = 0
for j in range(MAX):
    for i in range(MAX):
        mx = max(mx, acc[i][j])
print(mx)

Submission status: Runtime Error

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

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()
mod = K * 2
XY = []
MAX = K*6 + 7
acc = list2d(MAX, MAX, 0)
for i in range(N):
    x, y, c = input().split()
    x = int(x)
    y = int(y)
    if c == 'B':
        x += K
    for a1 in range(3):
        for a2 in range(3):
            for b in [0, K]:
                if c == 'B':
                    a = -a
                x1 = x + a1 * K*2 + b
                y1 = y + a2 * K*2 + b
                x2 = x1 + K
                y2 = y1 + K
                x2 = min(x2, MAX-1)
                y2 = min(y2, MAX-1)
                acc[x1][y1] += 1
                acc[x2][y2] += 1
                acc[x1][y2] -= 1
                acc[x2][y1] -= 1

for i in range(MAX):
    acc[i] = list(accumulate(acc[i]))
for j in range(MAX):
    for i in range(MAX-1):
        acc[i+1][j] += acc[i][j]
mx = 0
for j in range(MAX):
    for i in range(MAX):
        mx = max(mx, acc[i][j])
print(mx)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
N, K = map(int, input().split())
bdic = defaultdict(int)
wdic = defaultdict(int)
for i in range(N):
  x, y, c = input().split()
  x = int(x)
  y = int(y)
  if c=='W':
    wdic[(x%(2*K),y%(2*K))] += 1
  else:
    bdic[(x%(2*K),y%(2*K))] += 1
b_sum = [[0]*(4*K+1) for i in range(4*K+1)]
w_sum = [[0]*(4*K+1) for i in range(4*K+1)]
for i in range(4*K):
  for j in range(4*K):
    si = i
    sj = j
    if si>=2*K:
      si -= 2*K
    if sj>=2*K:
      sj -= 2*K
    b_sum[i+1][j+1] = b_sum[i][j+1]+b_sum[i+1][j]-b_sum[i][j]+bdic[(si,sj)]
    w_sum[i+1][j+1] = w_sum[i][j+1]+w_sum[i+1][j]-w_sum[i][j]+wdic[(si,sj)]
ans = 0
for i in range(2*K+1):
  for j in range(2*K+1):
    bn = (b_sum[2*K+i][2+K+j] - b_sum[K+i][2*K+j] - b_sum[2*K+i][K+j] + 2*b_sum[K+i][K+j]
          - b_sum[K+i][j] - b_sum[i][K+j] + b_sum[i][j])
    
    wn = (w_sum[K+i][2*K+j]+w_sum[2*K+i][K+j]-2*w_sum[K+i][K+j]
    - w_sum[i][2*K+j] + w_sum[i][K+j] - w_sum[2*K+i][j] + w_sum[K+i][j] - w_sum[i][j])
    
    ans = max(ans, bn+wn)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

read = sys.stdin

N, K = map(int, read.readline().split())
k = 2 * K
xyc = np.array(read.read().split(), np.str).reshape(-1, 3)
x = xyc[:, 0].astype(int)
y = xyc[:, 1].astype(int)
c = xyc[:, 2]

x = np.where(c == 'B', x - K, x)
x %= k
y %= k

# grid[[1,2],[1,2]]+=1 としてもgrid[1,2]=1であり重複は考慮されない。
# uniqueを使って(x,y)の重複をカウントする
xy, cnt = np.unique(x + y * 10000, return_counts=True)
x = xy % 10000
y = xy // 10000

grid = np.zeros((3 * K, 3 * K), np.int64)
grid[x, y] += cnt
grid[x + K, y + K] += cnt
grid[x + K, y] -= cnt
grid[x, y + K] -= cnt
np.cumsum(grid, axis=0, out=grid)
np.cumsum(grid, axis=1, out=grid)

p1 = grid[:K, :K] + grid[k:, k:] + grid[K:k, K:k] + grid[k:, :K] + grid[:K, k:]
p2 = grid[K:k, :K] + grid[K:k, k:] + grid[:K, K:k] + grid[k:, K:k]
print(max(p1.max(), p2.max()))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=[input().split() for _ in range(n)]
b=[[0]*k for _ in range(k)]
w=[[0]*k for _ in range(k)]
anb=[[0]*k for _ in range(k)]
anw=[[0]*k for _ in range(k)]
ans=0
i=0
while i<n:
    m=(((int(l[i][0]))//k)+((int(l[i][1]))//k))%2
    if (m==0 and str(l[i][2])=="W"):
        w[l[i][0]][l[i][1]] +=1    
    elif (m==1 and str(l[i][2])=="B"):
        b[l[i][0]][l[i][1]] +=1   
    elif:(m==1 and str(l[i][2])=="W"):
        anw[l[i][0]][l[i][1]] +=1 
    else:
        anb[l[i][0]][l[i][1]] +=1 
      

for a in range(0,k):
     for b in range(0,k):
         total=sum([w[a:k][b:k])+sum(anw[0:a][0:b])+sum([b[a:k][b:k])+sum(anb[0:a][0:b])
         ans=max(ans,total,n-total)
print(ans)

Submission status: Runtime Error

Solution code:
    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 status: Runtime Error

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

Submission status: Runtime Error

Solution code:

    l.append(input().split())
ans=0
for a in range(0,k):
     for b in range(0,k):
         i=0
         total=0
         while i<n:
           m=((int(l[i][0])-a)//k)%2
           h=((int(l[i][1])-b)//k)%2
           if ((m+h)%2==0 and str(l[i][2])=="W") or ((m+h)%2==1 and str(l[i][2])=="B"):
               total=total+1
               i=i+1
           else:
               i=i+1

         else:
              ans=max(ans,total,n-total)
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=[]
for j in range(0,4):
    l.append(input().split())
ans=0
for a in range(0,k):
     for b in range(0,k):
         i=0
         total=0
         while i<n:
           m=(abs(int(l[i][0])-a)//k)%2
           h=(abs(int(l[i][1])-b)//k)%2
           if ((m+h)%2==0 and str(l[i][2])=="W") or ((m+h)%2==1 and str(l[i][2])=="B"):
               total=total+1
               i=i+1
           else:
               i=i+1

         else:
              ans=max(ans,total,n-total)
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())

from numpy import arrer



def s(a,b):
   i=0
   total=0
   while i<n:
      x1,y1,z=map(str,input().split())
      x,y=map(int,[x1,y1])
      l=(abs(x+y-a-b)//k)%2
      if (l==0 and z="W") or (l==1 and z="b"):
          total=total+1
          i=i+1

      else:
          i=i+1

   else:
       return s=max(total,n-total)

 print(max(s(a,b)) if (a for xrange(k)) and (if b for xrange(k)))

Submission status: Runtime Error

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

import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()
mod = K * 2
XY = []
MAX = K*2 + 7
acc = list2d(MAX, MAX, 0)
for i in range(N):
    x, y, c = input().split()
    x = int(x)
    y = int(y)
    if c == 'B':
        x += K
    x %= mod
    y %= mod
    x2 = x + K
    y2 = y + K
    acc[x][y] += 1
    acc[x2][y2] += 1
    acc[x][y2] -= 1
    acc[x2][y] -= 1
    XY.append((x, y))
    if c == 'B':
        x -= K
        x %= mod
        y += K
        y %= mod
        x2 = x + K
        y2 = y + K
    else:
        x += K; y += K
        x2 += K; y2 += K
    acc[x][y] += 1
    acc[x2][y2] += 1
    acc[x][y2] -= 1
    acc[x2][y] -= 1

for i in range(MAX):
    acc[i] = list(accumulate(acc[i]))
for j in range(MAX):
    for i in range(MAX-1):
        acc[i+1][j] += acc[i][j]
mx = 0
for j in range(MAX):
    for i in range(MAX):
        mx = max(mx, acc[i][j])
print(mx)

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
lsw=[0]*(k*2)
for i in range(n):
  a,b,c=input().split()
  a=int(a)%(k*2)
  b=int(b)%(k*2)
  if c=="B":
    b=(b+k)%(k*2)
  if a>=k:
    b=(b+k)%(k*2)
  lsw[b]+=1
ans=[sum(lsw[:k])]
for i in range(1,k+1):
  ans.append(ans[-1]+lsw[k+i]-lsw[i])
print(max(ans))

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
lsw=[0]*(k*2)
for i in range(n):
  a,b,c=input().split()
  a,b=int(a)%(k*2),int(b)%(k*2)
  if c=="B":
    b=(b+k)%(k*2)
  if a>=k:
    b=(b+k)%(k*2)
  lsw[b]+=1
ans=[sum(lsw[:k])]
for i in range(1,k+1):
  ans.append(ans[-1]+lsw[k+i]-lsw[i])
print(max(ans))

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, k = LI()
l = 2 * k
acc = [[0] * 3 * k for _ in range(l)]
for _ in range(n):
    x, y, c = LS()
    x = int(x); y = int(y)
    if c == 'W':
        x += k
    x %= 2 * k
    y %= 2 * k
    acc[y][x] += 1
    for i, j in ((1, 1), (1, -1), (-1, 1), (-1, -1), (2, 0), (-2, 0)):
        if 0 <= y + k * i < l and 0 <= x + k * j < l:
            acc[y + k * i][x + k * j] += 1



acc = [[0] * (l + 1)] + [list(accumulate([0] + acc[y])) for y in range(l)]
for y in range(l):
    for x in range(l + 1):
        acc[y + 1][x] += acc[y][x]


ans = 0
for y in range(k + 1):
    for x in range(2 * k + 1):
        ans = max(ans, acc[y + k][x + k] - acc[y][x + k] - acc[y + k][x] + acc[y][x])


print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
import itertools
N, K = [int(_) for _ in input().split()]
XYC = []
b = np.zeros((2 * K, 2 * K), dtype=int)
for _ in range(N):
    x, y, c = input().split()
    x = int(x)
    y = int(y)
    if c == 'W':
        y += K
    dy = 2 * K * (y // (2 * K))
    y -= dy
    x -= dy
    x %= 2 * K
    b[x, y] += 1
    b[(x + K) % (2 * K), y + K] += 1
c = np.pad(b, [(1, 0), (1, 0)], 'constant')
acc = c.cumsum(axis=0).cumsum(axis=1)
ans = max(acc[i + K, j + K] - acc[i, j + K] - acc[i + K, j] + acc[i, j]
          for i, j in itertools.product(range(K), range(K)))
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
import itertools
N, K = [int(_) for _ in input().split()]
XYC = []
b = np.zeros((2 * K, 2 * K), dtype=int)
for _ in range(N):
    x, y, c = input().split()
    x = int(x)
    y = int(y)
    if c == 'W':
        y += K
    dy = K * (y // (2 * K))
    y -= dy
    x -= dy
    x %= 2 * K
    b[x, y] += 1
    b[(x + K) % (2 * K), y + K] += 1
c = np.pad(b, [(1, 0), (1, 0)], 'constant')
acc = c.cumsum(axis=0).cumsum(axis=1)
ans = max(acc[i + K, j + K] - acc[i, j + K] - acc[i + K, j] + acc[i, j]
          for i, j in itertools.product(range(K), range(K)))
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

def judge(k,x,y):
    return (x//k + y//k)%2

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

ans = 0
for i in range(k):
    x_tmp = np.array(x) + i
    for j in range(k):
        y_tmp = np.array(y) + j
        for l in range(n):
            if judge(k,x_tmp[l],y_tmp[l]) == c[l]:
                count += 1
        tmp = max(count,n-count)
        ans = max(ans,tmp)
        count = 0
print(ans)

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
l=2*k
r=[[0]*(l+1) for _ in range(l+1)]

for i in range(n):
    x,y,z=input().split()
    x,y=int(x)%l,int(y)%l
    if z=='B':
        r[x][y]+=1
    else:
        r[(x+k)%l][y]+=1
    
for i in range(l):
    for j in range(l):
        r[i+1][j+1]+=r[i+1][j]+r[i][j+1]-r[i][j]

for i in r:
    print(*i)

print()

for i in s:
    print(*i)

ans=0
for i in range(l):
    for j in range(l):
        b=r[l][l] # TBD
        ans=max(ans,b+w)
        print(i,j,b,w)
print(ans)

Submission status: Runtime Error

Solution code:
6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Submission status: Runtime Error

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)]

for _ in range(N):
  x,y,c  = input().split()
  x = int(x)
  y = int(y)
  if c == 'B':
    x -= K
  hope[x%KK,y%KK] += 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: Runtime Error

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: Runtime Error

Solution code:
kl = set(range(1, 2 * 1000 + 10))
board_tmp = [0 for i in kl]
board = []
for i in kl:
    board.append(board_tmp[:])
n, k = map(int, input().split())
xy_list = []
nl = set(range(n))
kl = set(range(1, 2 * k + 1))
total += 0
for i in nl:
    xi, yi, wb = input().split()
    xi = (int(xi) if wb == 'B' else int(xi) + k) % (2 * k)
    yi = int(yi) % (2 * k)
    board[xi+1][yi+1] += 1
    total += 1
for i in kl:
    for j in kl:
        board[i][j] += board[i][j - 1]
for j in kl:
    for i in kl:
        board[i][j] += board[i - 1][j]
ans = -1
kl = set(range(k+1))
for i in kl:
    for j in kl:
        ik = i + k
        jk = j + k
        a = board[i][j]
        b = board[ik][jk] - board[ik][j] \
            - board[i][jk] + board[i][j]
        c = board[2 * k][j] - board[ik][j]
        d = board[i][2 * k] - board[i][jk]
        e = board[2 * k][2 * k] - board[2 * k][jk] \
            - board[ik][2 * k] + board[ik][jk]
        tmp = a + b + c + d + e
        ans = max(ans, max(tmp, total - tmp))
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
xy_list = []
nl = set(range(n))
kl = set(range(1, 2 * k + 1))
board = [[0 for i in kl] for j in kl]
for i in nl:
    xi, yi, wb = input().split()
    xi = (int(xi) if wb == 'B' else int(xi) + k) % (2 * k)
    yi = int(yi) % (2 * k)
    board[xi+1][yi+1] += 1
# O(NK^2)
for i in kl:  # K*
    for j in kl:  # K*
        board[i][j] += board[i][j - 1]
for j in kl:  # K*
    for i in kl:  # K*
        board[i][j] += board[i - 1][j]
ans = -1
kl = set(range(2 * k))
for i in kl:
    for j in kl:
        ia = max(i - k, 0)
        ja = max(j - k, 0)
        ik = min(i + k, 2 * k)
        jk = min(j + k, 2 * k)
        a = board[i][j] - board[i][ja] \
            - board[ia][j] + board[ia][ja]  # 左下
        b = board[ik][jk] - board[ik][j] \
            - board[i][jk] + board[i][j]  # 中央
        c = board[2 * k][j] - board[ik][j]  # 右上
        d = board[i][2 * k] - board[i][jk]  # 左上
        e = board[2 * k][2 * k] - board[2 * k][jk] \
            - board[ik][2 * k] + board[ik][jk]  # 右上
        ans = max(ans, a + b + c + d + e)
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
xy_list = []
nl = set(range(n))
for i in nl:
    xi, yi, wb = input().split()
    xi = (int(xi) if wb == 'B' else int(xi) + k) % (2 * k)
    yi = int(yi) % (2 * k)
    board[xi+1][yi+1] += 1
kl = set(range(1, 2 * k + 1))
board = [[0 for i in kl] for j in kl]
# O(NK^2)
for i in kl:  # K*
    for j in kl:  # K*
        board[i][j] += board[i][j - 1]
for j in kl:  # K*
    for i in kl:  # K*
        board[i][j] += board[i - 1][j]
ans = -1
kl = set(range(2 * k))
for i in kl:
    for j in kl:
        ia = max(i - k, 0)
        ja = max(j - k, 0)
        ik = min(i + k, 2 * k)
        jk = min(j + k, 2 * k)
        a = board[i][j] - board[i][ja] \
            - board[ia][j] + board[ia][ja]  # 左下
        b = board[ik][jk] - board[ik][j] \
            - board[i][jk] + board[i][j]  # 中央
        c = board[2 * k][j] - board[ik][j]  # 右上
        d = board[i][2 * k] - board[i][jk]  # 左上
        e = board[2 * k][2 * k] - board[2 * k][jk] \
            - board[ik][2 * k] + board[ik][jk]  # 右上
        ans = max(ans, a + b + c + d + e)
print(ans)

Submission status: Runtime Error

Solution code:
kmax = 1000
board = [[0 for i in range(kmax + 10)] for j in range(kmax + 10)]
n, k = map(int, input().split())
xy_list = []
nl = set(range(n))
for i in nl:
    xi, yi, wb = input().split()
    xi = (int(xi) if wb == 'B' else int(xi) + k) % (2 * k)
    yi = int(yi) % (2 * k)
    board[xi+1][yi+1] += 1
kl = set(range(1, 2 * k + 1))
# O(NK^2)
for i in kl:  # K*
    for j in kl:  # K*
        board[i][j] += board[i][j - 1]
for j in kl:  # K*
    for i in kl:  # K*
        board[i][j] += board[i - 1][j]
ans = -1
kl = set(range(2 * k))
for i in kl:
    for j in kl:
        ia = max(i - k, 0)
        ja = max(j - k, 0)
        ik = min(i + k, 2 * k)
        jk = min(j + k, 2 * k)
        a = board[i][j] - board[i][ja] \
            - board[ia][j] + board[ia][ja]  # 左下
        b = board[ik][jk] - board[ik][j] \
            - board[i][jk] + board[i][j]  # 中央
        c = board[2 * k][j] - board[ik][j]  # 右上
        d = board[i][2 * k] - board[i][jk]  # 左上
        e = board[2 * k][2 * k] - board[2 * k][jk] \
            - board[ik][2 * k] + board[ik][jk]  # 右上
        ans = max(ans, a + b + c + d + e)
print(ans)

Submission status: Runtime Error

Solution code:
n, k = [int(i) for i in input().split(" ")]
dm = [[0 for j in range(0, 2 * k)] for i in range(0, 2 * k)]
s = [[0 for j in range(0, 2 * k + 1)] for  i in range(0, 2 * k + 1)]

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

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

def srect(i, j, r, l):
    global s
    return s[r][l] - s[r][j] - s[i][l] + s[i][j]

m = 0
for i in range(0, k):
    for j in range(0, k):
        aw = srect(i, j, i + k, j + k) + srect(i + k, j + k, 2 * k, 2 * k)
        raw = s[2 * k][2 * k] - aw
        if m < max(aw, raw):
            m = max(aw, raw)
print(m)

#for i in s:
#    print(i)

Submission status: Runtime Error

Solution code:
import numpy as np
n, k = [int(item) for item in input().split()]
xyc = [[item for item in input().split()] for _ in range(n)]

a = [[0] * 2*k for _ in range(2*k)]
for x, y, c in xyc:
    x = int(x)
    y = int(y)
    if c == "W":
        a[x%(2*k)][y%(2*k)] +=  1
    else: 
        a[(x+k)%(2*k)][(y)%(2*k)] += 1

cumsum = np.cumsum(np.cumsum(a, axis=0), axis=1)
aflip = np.flip(np.flip(a, axis=1), axis=0)
cumsum_flip =  np.cumsum(np.cumsum(aflip, axis=0), axis=1)

ans = 0
for i in range(k):
    for j in range(k):
        ret = 0
        ik = i+k
        jk = j+k
        hm = 2*k-1
        ret += (cumsum[ik][hm] - cumsum[i][hm] + 2 * cumsum[i][jk] + cumsum[hm][j+k] - cumsum[hm][j] + 2 * cumsum[ik][j] - 2 * cumsum[ik][jk] - 2 * cumsum[i][j])
        ans = max(ans, ret, n-ret)
print(ans)