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:
temp=input().split(" ")
n=int(temp[0])
k=int(temp[1])
k2=2*k
tileb=[[0 for i in range(k2*2)] for j in range(k2*2)]
for i in range(n):
    temp=input().split(" ")
    x=int(temp[0])
    y=int(temp[1])
    c=temp[2]
    if c=='B':  
        tileb[y%(k2)][x%(k2)]+=1
    elif c=='W':  
        tileb[(y+k)%k2][x%k2]+=1
#ruisekiwa=[[0 for i in range(k2)] for j in range(k2)]
# for x in range(0,k2):
#     ruisekiwa[0][x]=tileb[0][x]
#print(tileb)

ans=0
for y in range(0,k2*2):
    for x in range(1,k2*2):
        tileb[y][x]+=tileb[y][x-1]

for x in range(0,k2*2):
    for y in range(1,k2*2):
        tileb[y][x]+=tileb[y-1][x]

def nijiruisekiwa(x,y,k): #左下(x,y) 長さkの正方形領域の和
    global tileb     
    return tileb[y+k-1][x+k-1]-tileb[y+k-1][x-1]-tileb[y-1][x+k-1]+tileb[y-1][x-1]
for x in range(0,k2):
    for y in range(0,k2):
        temp=0
        a=nijiruisekiwa(x,y,k)
        b=nijiruisekiwa(x+k,y+k,k)

        temp=a+b
        if temp>ans:
            # print(a)
            # print(b)
            # print("x={0}  y={1}  ".format(x,y))
            # print(temp)
            ans=temp
print(ans)

Submission status: Wrong Answer

Solution code:
import re
import numpy as np
temp=input().split(" ")
n=int(temp[0])
k=int(temp[1])
k2=2*k
tileb=[[0 for i in range(k2)] for j in range(k2)]
tileb=np.array(tileb)
for i in range(n):
    temp=input().split(" ")
    x=int(temp[0])
    y=int(temp[1])
    c=temp[2]
    if c=='B':  
        tileb[y%(k2)][x%(k2)]+=1
    elif c=='W':  
        tileb[(y+k)%k2][x%k2]+=1
#ruisekiwa=[[0 for i in range(k2)] for j in range(k2)]
# for x in range(0,k2):
#     ruisekiwa[0][x]=tileb[0][x]

ans=0
for y in range(1,k2):
    for x in range(1,k2):
        tileb[y][x]+=tileb[y][x-1]
for x in range(1,k2):
    for y in range(1,k2):
        tileb[y][x]+=tileb[y-1][x]

#print(tileb)

tileb=np.concatenate([tileb,tileb],axis=0)
# print(tileb)

tileb=np.concatenate([tileb,tileb],axis=1)
#print(tileb)


def nijiruisekiwa(x,y,k): #左下(x,y) 長さkの正方形領域の和
    global tileb     
    return tileb[y+k-1][x+k-1]-tileb[y+k-1][x-1]-tileb[y-1][x+k-1]+tileb[y-1][x-1]
for x in range(1,k2):
    for y in range(1,k2):
        temp=0
        a=nijiruisekiwa(x,y,k)
        b=nijiruisekiwa(x+k,y+k,k)

        temp=a+b
        if temp>ans:
            # print(a)
            # print(b)
            # print("x={0}  y={1}  ".format(x,y))
            # print(temp)
            ans=temp
print(ans)

Submission status: Wrong Answer

Solution code:
import re
import numpy as np
temp=input().split(" ")
n=int(temp[0])
k=int(temp[1])
k2=2*k
tileb=[[0 for i in range(k2)] for j in range(k2)]
tileb=np.array(tileb)
for i in range(n):
    temp=input().split(" ")
    x=int(temp[0])
    y=int(temp[1])
    c=temp[2]
    if c=='B':  
        tileb[y%(k2)][x%(k2)]+=1
    elif c=='W':  
        tileb[(y+k)%k2][x%k2]+=1
#ruisekiwa=[[0 for i in range(k2)] for j in range(k2)]
# for x in range(0,k2):
#     ruisekiwa[0][x]=tileb[0][x]

ans=0
for y in range(1,k2):
    for x in range(1,k2):
        tileb[y][x]+=tileb[y][x-1]
for x in range(1,k2):
    for y in range(1,k2):
        tileb[y][x]+=tileb[y-1][x]

#print(tileb)

tileb=np.concatenate([tileb,tileb],axis=0)
# print(tileb)

tileb=np.concatenate([tileb,tileb],axis=1)
print(tileb)


def nijiruisekiwa(x,y,k): #左下(x,y) 長さkの正方形領域の和
    global tileb     
    return tileb[y+k-1][x+k-1]-tileb[y+k-1][x-1]-tileb[y-1][x+k-1]+tileb[y-1][x-1]
for x in range(1,k2):
    for y in range(1,k2):
        temp=0
        a=nijiruisekiwa(x,y,k)
        b=nijiruisekiwa(x+k,y+k,k)

        temp=a+b
        if temp>ans:
            # print(a)
            # print(b)
            # print("x={0}  y={1}  ".format(x,y))
            # print(temp)
            ans=temp
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter
import numpy as np

n, k = list(map(int,input().split()))
table = np.zeros((2*k,2*k),dtype='int32')
for i in range(n):
    t = input().split()
    if(t[2]=='B'):
        table[int(t[0])%(2*k)][int(t[1])%(2*k)] += 1
    else:
        table[(int(t[0])+k)%(2*k)][int(t[1])%(2*k)] += 1
        

n_table = np.zeros((3*k+1,3*k+1),dtype='int32')
n_table[1:2*k+1,1:2*k+1] = table
n_table[2*k+1:3*k+1,1:2*k+1] = table[0:k,0:2*k]
n_table[1:2*k+1,2*k+1:3*k+1] = table[0:2*k,0:k]
n_table[2*k+1:3*k+1,2*k+1:3*k+1] = table[0:k,0:k]

i_table = np.zeros((3*k+1,3*k+1),dtype='int32')

for i in range(0,3*k+1):
    for j in range(1,3*k+1):
        i_table[i][j] += i_table[i][j-1] + n_table[i][j] 
for i in range(1,3*k+1):
    for j in range(0,3*k+1):
        i_table[i][j] += i_table[i-1][j]  

ans = 0
for i in range(0,k):
    for j in range(0,k):        
        hope1 = i_table[i+k][j+k] - i_table[i+k][j] - i_table[i][j+k] + i_table[i][j]
        hope2 = i_table[i+2*k][j+2*k] - i_table[i+2*k][j+k] - i_table[i+k][j+2*k] + i_table[i+k][j+k]
        ans = max(ans,hope1+hope2)
print(ans)

Submission status: Wrong Answer

Solution code:
def solve():
    import numpy as np
    N,K=map(int,input().split())
    m=np.zeros((3*K,3*K),dtype=int)
    L=2*K
    o=0
    for _ in [0]*N:
        x,y,c=input().split()
        x,y=int(x)%L,int(y)%L
        t=c=='W'
        m[x,y]+=1-2*t
        o+=t
    for _ in [0,0]:
        m[L:]=m[:K]
        m=np.cumsum(m,axis=0)
        m[:L]=m[K:]-m[:L]
        m=m.T
    m=m[:L,:L]


    print(o+m.max())


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
def solve():
    import numpy as np
    N,K=map(int,input().split())
    m=np.zeros((3*K,3*K),dtype=int)
    L=2*K
    o=0
    for _ in [0]*N:
        x,y,c=input().split()
        x,y=int(x)%L,int(y)%L
        t=c=='W'
        m[x,y]+=1-2*t
        o+=t
    for _ in [0,0]:
        m[L:]=m[:K]
        m=np.cumsum(m,axis=0)
        m[:L]=m[K:]-m[:L]
        m=m.T
    m=m[:L,:L]

    print(m.max())
    print(o+m.max())


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import numpy as np
N,K=map(int,input().split())
m=np.zeros((3*K,3*K),dtype=int)
L=2*K
o=0
for _ in [0]*N:
	x,y,c=input().split()
	x,y=int(x)%L,int(y)%L
	t=c=='W'
	m[x,y]+=1-2*t
	o+=t
for _ in [0,0]:
	m[L:]=m[:K]
	np.cumsum(m,axis=0,out=m)
	m[:L]-=m[K:]
	m=-m.T
print(o+np.abs(m[:K,:K]+m[K:L,K:L]).max())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

class Ruiwa():
    def __init__(self, a):
        self.H = h = len(a)
        self.W = w = len(a[0])
        self.R = r = a
        for i in range(h):
            for j in range(1,w):
                r[i][j] += r[i][j-1]

        for i in range(1,h):
            for j in range(w):
                r[i][j] += r[i-1][j]

    def search(self, x1, y1, x2, y2):
        if x1 > x2 or y1 > y2:
            return 0

        r = self.R
        rr = r[y2][x2]
        if x1 > 0 and y1 > 0:
            return rr - r[y1-1][x2] - r[y2][x1-1] + r[y1-1][x1-1]
        if x1 > 0:
            rr -= r[y2][x1-1]
        if y1 > 0:
            rr -= r[y1-1][x2]

        return rr

def main():
    n,k = LI()
    a = [LS() for _ in range(n)]
    k2 = k*2
    k4 = k*5
    t = [[0] * (k4) for _ in range(k4)]
    for xs,ys,c in a:
        x = int(xs)
        y = int(ys)
        if c == 'W':
            x += k
        px = x%k2
        py = y%k2
        t[px][py] += 1
        t[px+k][py+k] += 1
        t[px][py+k] -= 1
        t[px+k][py] -= 1

        px += k
        py += k
        t[px][py] += 1
        t[px+k][py+k] += 1
        t[px][py+k] -= 1
        t[px+k][py] -= 1

        px += k
        py -= k
        t[px][py] += 1
        t[px+k][py+k] += 1
        t[px][py+k] -= 1
        t[px+k][py] -= 1

        px -= k2
        py += k2
        t[px][py] += 1
        t[px+k][py+k] += 1
        t[px][py+k] -= 1
        t[px+k][py] -= 1


    rui = Ruiwa(t)

    return max(map(max,rui.R))


print(main())

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

class Ruiwa():
    def __init__(self, a):
        self.H = h = len(a)
        self.W = w = len(a[0])
        self.R = r = a
        for i in range(h):
            for j in range(1,w):
                r[i][j] += r[i][j-1]

        for i in range(1,h):
            for j in range(w):
                r[i][j] += r[i-1][j]

    def search(self, x1, y1, x2, y2):
        if x1 > x2 or y1 > y2:
            return 0

        r = self.R
        rr = r[y2][x2]
        if x1 > 0 and y1 > 0:
            return rr - r[y1-1][x2] - r[y2][x1-1] + r[y1-1][x1-1]
        if x1 > 0:
            rr -= r[y2][x1-1]
        if y1 > 0:
            rr -= r[y1-1][x2]

        return rr

def main():
    n,k = LI()
    a = [LS() for _ in range(n)]
    k2 = k*2
    k4 = k*4
    t = [[0] * (k4) for _ in range(k4)]
    for xs,ys,c in a:
        x = int(xs)
        y = int(ys)
        if c == 'W':
            x += k
        px = x%k2
        py = y%k2
        t[px][py] += 1
        t[px+k][py+k] += 1
        t[px][py+k] -= 1
        t[px+k][py] -= 1

        px += k
        py += k
        t[px][py] += 1
        t[px+k][py+k] += 1
        t[px][py+k] -= 1
        t[px+k][py] -= 1

    rui = Ruiwa(t)

    return max(map(max,rui.R))


print(main())

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from itertools import accumulate
def inpl(): return tuple(map(int, input().split()))
N, K = inpl()
P = [[0 for _ in range(2*K)] for _ in range(K)]

for _ in range(N):
    x, y, c = input().split()
    x, y = int(x), int(y)
    if c=="W":
        y += K
    x, y = int(x)%(2*K), int(y)%(2*K)
    if x >= K:
        if y < K:
            x -= K
            y += K
        else:
            x -= K
            y -= K
    
    P[0][y] += 1
    if x == K-1:
        if y == K-1:
            pass
        elif y > K-1:
            P[0][y-K] -= 1
        elif y < K-1:
            P[0][-1] += 1
            P[0][y+K] -= 1
    else:
        if y == K-1:
            P[x+1][-1] += 1
            P[x+1][y] -= 2
        elif y == 2*K - 1:
            P[x+1][y-K] += 1
            P[x+1][y] -= 1
            P[0][y-K] -= 1
        elif y > K-1:
            P[x+1][-1] += 1
            P[x+1][y-K] += 1
            P[x+1][y] -= 2
            P[0][y-K] -= 1
        elif y < K-1:
            P[0][-1] += 1
            P[x+1][y+K] += 1
            P[x+1][-1] -= 1
            P[0][y+K] -= 1
            P[x+1][y] -= 2

S1 = [list(accumulate(l)) for l in zip(*P)]
S2 = [max(list(accumulate(l[::-1]))) for l in zip(*S1)]

print(max(S2))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from itertools import accumulate
def inpl(): return tuple(map(int, input().split()))
N, K = inpl()
P = [[0 for _ in range(2*K)] for _ in range(K)]

for _ in range(N):
    x, y, c = input().split()
    x, y = int(x), int(y)
    if c=="W":
        y += K
    x, y = int(x)%(2*K), int(y)%(2*K)
    if x >= K:
        if y < K:
            x -= K
            y += K
        else:
            x -= K
            y -= K
    
    P[0][y] += 1
    if x == K-1:
        if y == K-1:
            pass
        elif y > K-1:
            P[0][y-K] -= 1
        elif y < K-1:
            P[0][-1] += 1
            P[0][y+K] -= 1
    else:
        if y == K-1:
            P[x+1][-1] += 1
            P[x+1][y] == -2
        elif y == 2*K - 1:
            P[x+1][y-K] += 1
            P[x+1][y] -= 1
            P[0][y-K] -= 1
        elif y > K-1:
            P[x+1][-1] += 1
            P[x+1][y-K] += 1
            P[x+1][y] -= 2
            P[0][y-K] -= 1
        elif y < K-1:
            P[0][-1] += 1
            P[x+1][y+K] += 1
            P[x+1][-1] -= 1
            P[0][y+K] -= 1
            P[x+1][y] -= 2

S1 = [list(accumulate(l)) for l in zip(*P)]
S2 = [max(list(accumulate(l[::-1]))) for l in zip(*S1)]

print(max(S2))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from itertools import accumulate
def inpl(): return tuple(map(int, input().split()))
N, K = inpl()
P = [[0 for _ in range(2*K)] for _ in range(K)]

for _ in range(N):
    x, y, c = input().split()
    x, y = int(x), int(y)
    if c=="W":
        y += K
    x, y = int(x)%(2*K), int(y)%(2*K)
    if x < K:
        pass
    elif x >= K and y < K:
        x -= K
        y += K
    else:
        x -= K
        y -= K
    
    P[0][y] += 1
    if x == K-1:
        if y > K-1:
            P[0][y-K] -= 1
        elif y < K-1:
            P[0][-1] += 1
            P[0][y+K] -= 1
    else:
        if y == K-1:
            P[x+1][-1] += 1
            P[x+1][y] == -2
        elif y == 2*K - 1:
            P[x+1][y-K] += 1
            P[x+1][y] -= 1
            P[0][y-K] -= 1
        elif y > K-1:
            P[x+1][-1] += 1
            P[x+1][y-K] += 1
            P[x+1][y] -= 2
            P[0][y-K] -= 1
        elif y < K-1:
            P[0][-1] += 1
            P[x+1][y+K] += 1
            P[x+1][-1] -= 1
            P[0][y+K] -= 1
            P[x+1][y] -= 2

S1 = [list(accumulate(l)) for l in zip(*P)]
S2 = [max(list(accumulate(l[::-1]))) for l in zip(*S1)]

print(max(S2))

Submission status: Wrong Answer

Solution code:
from operator import sub

N, K = map(int, input().split())

numA = [[0] * (K + 1) for i in range(K + 1)]
numB = [[0] * (K + 1) for i in range(K + 1)]

for i in range(N):
    x, y, c = input().split()
    x = int(x) % (2 * K)
    y = int(y) % (2 * K)

    if c == 'B':
        if (x // K == 0 and y // K == 0) or (x // K == 1 and y // K == 1):
            numA[x % K][y % K] += 1
        else:
            numB[x % K][y % K] += 1
    else:
        if (x // K == 1 and y // K == 0) or (x // K == 0 and y // K == 1):
            numA[x % K][y % K] += 1
        else:
            numB[x % K][y % K] += 1


sumXA = list(map(sum, numA))
sumYA = list(map(sum, zip(*numA)))

sumXB = list(map(sum, numB))
sumYB = list(map(sum, zip(*numB)))

sumA = sum(sumXA)
sumB = sum(sumXB)

Arr = [[0] * (K + 1) for i in range(K + 1)]
numOKA = sumA
numOKB = sumB
for x in range(K + 1):

    tempOKA = numOKA
    tempOKB = numOKB
    for y in range(K + 1):
        Arr[x][y] = max(tempOKA + sumB - tempOKB, sumA - tempOKA + tempOKB)
        tempOKA -= sumYA[y]
        tempOKB -= sumYB[y]

    sumYA = list(map(sub, sumYA, numA[x]))
    sumYA = list(map(sub, sumYA, numA[x]))
    numOKA -= sumXA[x]

    sumYB = list(map(sub, sumYB, numB[x]))
    sumYB = list(map(sub, sumYB, numB[x]))
    numOKB -= sumXB[x]

print(max(max(Arr)))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from itertools import accumulate
def inpl(): return tuple(map(int, input().split()))
N, K = inpl()
P = [[0 for _ in range(2*K)] for _ in range(K)]

for _ in range(N):
    x, y, c = input().split()
    x, y = int(x), int(y)
    if c=="W":
        y += K
    x, y = int(x)%(2*K), int(y)%(2*K)
    if x < K:
        pass
    elif x >= K and y < K:
        x -= K
        y += K
    else:
        x -= K
        y -= K
    
    P[0][y] += 1
    if x == K-1:
        if y > K-1:
            P[0][y-K] -= 1
        elif y < K-1:
            P[0][-1] += 1
            P[0][y+K] -= 1
    else:
        if y == K-1:
            P[x+1][-1] += 1
            P[x+1][y] == -2
        elif y == 2*K - 1:
            P[x+1][y-K] += 1
            P[x+1][y] -= 1
            P[0][y-K] -= 1
        elif y > K-1:
            P[x+1][-1] += 1
            P[x+1][y-K] += 1
            P[x+1][y] -= 2
            P[0][y-K] -= 1
        else:
            P[0][-1] += 1
            P[x+1][y+K] += 1
            P[x+1][y+K+1] -=1
            P[0][y+K] -= 1
            P[x+1][y] -= 2

S1 = [list(accumulate(l)) for l in zip(*P)]
S2 = [max(list(accumulate(l[::-1]))) for l in zip(*S1)]

print(max(S2))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from itertools import accumulate
def inpl(): return tuple(map(int, input().split()))
N, K = inpl()
P = [[0 for _ in range(2*K)] for _ in range(K)]

for _ in range(N):
    x, y, c = input().split()
    x, y = int(x), int(y)
    if c=="W":
        y += K
    x, y = int(x)%(2*K), int(y)%(2*K)
    if x < K:
        pass
    elif x >= K and y < K:
        x -= K
        y += K
    else:
        x -= K
        y -= K
    
    P[0][y] += 1
    if x == K-1:
        if y > K-1:
            P[0][y-K] -= 1
        elif y < K-1:
            P[0][-1] += 1
            P[0][y+K] -= 1
    else:
        if y == K-1:
            P[x+1][-1] += 1
            P[x+1][y] == -2
        elif y == 2*K - 1:
            P[x+1][y-K] += 1
            P[x+1][y] -= 1
            P[0][y-K] -= 1
        elif y > K-1:
            P[x+1][-1] += 1
            P[x+1][y-K] += 1
            P[x+1][y] -= 2
            P[x][y-K] -= 1
        else:
            P[0][-1] += 1
            P[x+1][y+K] += 1
            P[x+1][y+K+1] -=1
            P[x][y+K] -= 1
            P[x+1][y] -= 2

S1 = [list(accumulate(l)) for l in zip(*P)]
S2 = [max(list(accumulate(l[::-1]))) for l in zip(*S1)]

print(max(S2))

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
#xs = []
#ys = []
#cs = []

kk = k * 2

pos = [0] * (kk * kk)

for _ in range(n):
    x, y, c = input().split()
    x = int(x) % kk
    y = int(y) % kk
    if c == 'B':
        x = (x + k) % kk
    pos[y * kk + x] += 1

#for i in range(kk):
#    print(pos[i*kk:i*kk+kk])
#print('--')

acc = [0] * (kk * kk)
for y in range(kk):
    s = acc[y * kk] = sum(pos[y * kk:y * kk + k])
    for x in range(1, k+1):
        acc[y * kk + x] = s = s - pos[y * kk + x - 1] + pos[y * kk + x + k - 1]
    for x in range(k+1, kk):
        acc[y * kk + x] = s = s - pos[y * kk + x - 1] + pos[y * kk + x - k - 1]

#for i in range(kk):
#    print(acc[i*kk:i*kk+kk])
#print('--')

# reuse memory.
ans = pos

for x in range(kk):
    s = ans[x] = sum(acc[x:kk*k:kk])
    for y in range(1, k+1):
        ans[y * kk + x] = s = s - acc[y * kk + x - kk] + acc[y * kk + x + k - kk]
    for y in range(k+1, kk):
        ans[y * kk + x] = s = s - acc[y * kk + x - kk] + acc[y * kk + x - k - kk]

#for i in range(kk):
#    print(ans[i*kk:i*kk+kk])
print(max(ans))

Submission status: Wrong Answer

Solution code:
from operator import sub

N, K = map(int, input().split())

numA = [[0] * K for i in range(K)]
numB = [[0] * K for i in range(K)]

for i in range(N):
    x, y, c = input().split()
    x = int(x)
    y = int(y)

    dx = x % (2 * K)
    dy = y % (2 * K)

    if c == 'B':
        if (dx // K == 0 and dy // K == 0) or (dx // K == 1 and dy // K == 1):
            numA[x % K][y % K] += 1
        else:
            numB[x % K][y % K] += 1
    else:
        if (dx // K == 1 and dy // K == 0) or (dx // K == 0 and dy // K == 1):
            numA[x % K][y % K] += 1
        else:
            numB[x % K][y % K] += 1


sumXA = list(map(sum, numA))
sumYA = list(map(sum, zip(*numA)))

sumXB = list(map(sum, numB))
sumYB = list(map(sum, zip(*numB)))

sumA = sum(sumXA)
sumB = sum(sumXB)

Arr = [[0] * K for i in range(K)]
numOKA = sumA
numOKB = sumB
for x in range(K):

    tempOKA = numOKA
    tempOKB = numOKB
    for y in range(K):
        Arr[x][y] = max(tempOKA + sumB - tempOKB, sumA - tempOKA + tempOKB)
        tempOKA -= sumYA[y]
        tempOKB -= sumYB[y]

    sumYA = list(map(sub, sumYA, numA[x]))
    sumYA = list(map(sub, sumYA, numA[x]))
    numOKA -= sumXA[x]

    sumYB = list(map(sub, sumYB, numB[x]))
    sumYB = list(map(sub, sumYB, numB[x]))
    numOKB -= sumXB[x]

print(max(max(Arr)))

Submission status: Wrong Answer

Solution code:
import sys
import copy
from collections import deque

def white_or_black(k, x, y):
    if x % (2 * k) < k:
        if y % (2 * k) < k:
            return "W"
        else:
            return "B"
    else:
        if y % (2 * k) < k:
            return "B"
        else:
            return "W"


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, K] = [int(input_raw[0]), int(input_raw[1])]
    pos_list = []
    for i in range(N):
        input_raw = read_func().strip().split()
        [x, y, c] = [int(input_raw[0]), int(input_raw[1]), (input_raw[2])]
        pos_list.append((x,y,c))

    max_count = 0
    max_k = 0
    for i in range(K * 2):
            count = 0
            for pos in pos_list:
                count += 1 if pos[2] == white_or_black(K, pos[0] + i, pos[1]) else 0
            if count > max_count:
                max_k = i
                max_count = count;
    pos_list = [(pos[0] + max_k, pos[1], pos[2]) for pos in pos_list]

    for i in range(K * 2):
            count = 0
            for pos in pos_list:
                count += 1 if pos[2] == white_or_black(K, pos[0], pos[1] + i) else 0
            if count > max_count:
                max_k = i
                max_count = count;
    print max_count

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter
import numpy as np

n, k = list(map(int,input().split()))
table = np.zeros((2*k,2*k),dtype='int32')
for i in range(n):
    t = input().split()
    if(t[2]=='B'):
        table[int(t[0])%(2*k)][int(t[1])%(2*k)] += 1
    else:
        table[(int(t[0])+k)%(2*k)][int(t[1])%(2*k)] += 1
        
#print(table)

n_table = np.zeros((3*k,3*k),dtype='int32')
n_table[0:2*k,0:2*k] = table
n_table[2*k:3*k,0:2*k] = table[0:k,0:2*k]
n_table[0:2*k,2*k:3*k] = table[0:2*k,0:k]
n_table[2*k:3*k,2*k:3*k] = table[0:k,0:k]

#print(n_table)
i_table = np.zeros((3*k,3*k),dtype='int32')

for i in range(0,3*k):
    i_table[i][0] = n_table[i][0]
    for j in range(1,3*k):
        i_table[i][j] += i_table[i][j-1] + n_table[i][j] 
for i in range(1,3*k):
    for j in range(0,3*k):
        i_table[i][j] += i_table[i-1][j]  

#print(i_table)
ans = 0
hope1 = i_table[k-1][k-1]
hope2 = i_table[2*k-1][2*k-1] - i_table[2*k-1][k-1] - i_table[k-1][2*k-1] + i_table[k-1][k-1]
ans = max(ans,hope1+hope2)
for i in range(1,k):
    hope1 = i_table[k-1][i+k-1] - i_table[k-1][i-1]
    hope2 = i_table[2*k-1][i+2*k-1] - i_table[2*k-1][i+k-1] - i_table[k-1][i+2*k-1] + i_table[k-1][i+k-1]
    ans = max(ans,hope1+hope2)
for i in range(1,k):        
        hope1 = i_table[i+k-1][k-1]- i_table[i-1][k-1]
        hope2 = i_table[i+2*k-1][2*k-1] - i_table[i+2*k-1][k-1] - i_table[i+k-1][2*k-1] + i_table[i+k-1][k-1]
        ans = max(ans,hope1+hope2)

for i in range(1,k):
    for j in range(1,k):        
        hope1 = i_table[i+k-1][j+k-1] - i_table[i+k-1][j-1] - i_table[i-1][j+k-1] + i_table[i-1][j-1]
        hope2 = i_table[i+2*k-1][j+2*k-1] - i_table[i+2*k-1][j+k-1] - i_table[i+k-1][j+2*k-1] + i_table[i+k-1][j+k-1]
        ans = max(ans,hope1+hope2)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import heapq
import math
import sys

def hanten(c):
    if c == 'B':
        return 'W'
    else:
        return 'B'

tmp = input()
N, K = [int(a) for a in tmp.split()]
all_num = {'B': 0, 'W': 0}
requirements = [[0 for x in range(K)] for y in range(K)]
for n in range(N):
    tmp = input()
    x, y, c = [a for a in tmp.split()]
    x, y = int(x), int(y)
    x %= (K*2)
    y %= (K*2)
    x_pole = x // K
    y_pole = y // K
    assert x_pole <= 1 and y_pole <= 1
    if x_pole + y_pole == 1:
        c = hanten(c)
    requirements[x % K][y % K] += (1 if c == 'B' else -1)
    all_num[c] += 1

shitas = [sum([requirements[x][y] for y in range(K)]) for x in range(K)]
migis = [[0 for y in range(K)] for x in range(K+1)]
for x_ in range(K):
    x = K - x_ - 1
    for y in range(K):
        migis[x][y] = migis[x+1][y] + requirements[x][y]

c = 'B'
num = all_num[c]
max_ret = 0
max_ret = max([max_ret, num, N-num])
x, y = 0, 0
while x < K:
    y_num = num
    while y < K:
        y_num -= migis[x][y]
        y += 1
        max_ret = max([max_ret, y_num, N-y_num])
    num -= shitas[x]
    x += 1
    max_ret = max([max_ret, num, N-num])
print(max_ret)

Submission status: Wrong Answer

Solution code:
from operator import sub

N, K = map(int, input().split())

numA = [[0] * K for i in range(K)]
numB = [[0] * K for i in range(K)]

for i in range(N):
    x, y, c = input().split()
    x = int(x)
    y = int(y)

    dx = x % (2 * K)
    dy = y % (2 * K)

    if c == 'B':
        if (dx // K == 0 and dy // K == 0) or (dx // K == 1 and dy // K == 1):
            numA[x % K][y % K] += 1
        else:
            numB[x % K][y % K] += 1
    else:
        if (dx // K == 1 and dy // K == 0) or (dx // K == 0 and dy // K == 1):
            numA[x % K][y % K] += 1
        else:
            numB[x % K][y % K] += 1


sumXA = list(map(sum, numA))
sumYA = list(map(sum, zip(*numA)))

sumXB = list(map(sum, numB))
sumYB = list(map(sum, zip(*numB)))

sumA = sum(sumXA)
sumB = sum(sumXB)

Arr = [[0] * K for i in range(K)]
numOKA = sumA
numOKB = sumB
for x in range(K):

    for y in range(K):

        Arr[x][y] = max(numOKA + sumB - numOKB, sumA - numOKA + numOKB)
        numOKA -= sumYA[y]
        numOKB -= sumYB[y]

    sumYA = list(map(sub, sumYA, numA[x]))
    sumYA = list(map(sub, sumYA, numA[x]))
    numOKA -= sumXA[x]

    sumYB = list(map(sub, sumYB, numB[x]))
    sumYB = list(map(sub, sumYB, numB[x]))
    numOKB -= sumXB[x]

print(max(max(Arr)))

Submission status: Wrong Answer

Solution code:

import numpy as np

N, K = list(map(int, input().split()))

def transfer(xy, c):
    x, y = xy
    if c ^ (y//K%2 == 1):
        x = x+K
    return x%(2*K), y%K

def judge(x0, y0, condition):
    x_, y_ = condition
    x = (x_ - x0) % (2*K)
    y = (y_ - y0) % (2*K)
    return x<K ^ y<K

conditions = []
for i in range(N):
    x, y, c = input().split()
    x, y = transfer(list(map(int, [x, y])), c=="B")
    condition = (x, y)
    conditions.append(condition)

conditions = sorted(conditions, key=lambda x : x[0])

def count():
    arr = np.array([0 for i in range(2*K + 1)])
    for condition in conditions:
        x1, y = condition
        x0 = x1-K+1

        arr[x0%(2*K)] += 1
        arr[x1+1] -= 1
        if x0 < 0:
            arr[0] += 1
            arr[-1] -= 1
    max_ = 0
    sum_ = 0
    for z in arr:
        sum_ += z
        max_ = max(max_, z)
    return max_

maximum = 0

def updatecondition():
    for i, condition in enumerate(conditions):
        x, y = condition[0], (condition[1]+1)%K
        if y == 0:
            x = (x+K)%(2*K)
        conditions[i] = (x, y)


for i in range(2*K):
    updatecondition()
    maximum = max(maximum, count())
    if maximum is N:
        break

print(maximum)

Submission status: Wrong Answer

Solution code:
import sys
import copy
from collections import deque

def white_or_black(k, x, y):
    if x % (2 * k) <= k:
        if y % (2 * k) <= k:
            return "W"
        else:
            return "B"
    else:
        if y % (2 * k) <= k:
            return "B"
        else:
            return "W"


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, K] = [int(input_raw[0]), int(input_raw[1])]
    pos_list = []
    for i in range(N):
        input_raw = read_func().strip().split()
        [x, y, c] = [int(input_raw[0]), int(input_raw[1]), (input_raw[2])]
        pos_list.append((x,y,c))

    max_count = 0
    max_pos_list = []
    for i in range(K * 2):
            new_pos_list = [(pos[0] + i, pos[1], pos[2]) for pos in pos_list]
            count = 0
            for new_pos in new_pos_list:
                count += 1 if new_pos[2] == white_or_black(K, new_pos[0], new_pos[1]) else 0
            if count > max_count:
                max_pos_list = copy.deepcopy(new_pos_list)
                max_count = count;
    pos_list = copy.deepcopy(max_pos_list)
    for i in range(K * 2):
            new_pos_list = [(pos[0], pos[1] + i, pos[2]) for pos in pos_list]
            count = 0
            for new_pos in new_pos_list:
                count += 1 if new_pos[2] == white_or_black(K, new_pos[0], new_pos[1]) else 0
            if count > max_count:
                max_pos_list = copy.deepcopy(new_pos_list)
                max_count = count;
    print max_count

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, K = [int(e) for e in input().split()]
xyc = [input().split() for i in range(N)]
xyc = [(int(x), int(y), c == "W") for x, y, c in xyc]

def calc0(K, xyc):
    rmax = 0
    for i in range(K):
        for j in range(K):
            rcount = 0
            for x, y, c in xyc:
                rcount += ((x + j) // K + (y + i) // K) % 2 == c
            rmax = max(rmax, rcount)
    return rmax

def calc(K, xyc):
    rmax = 0
    for i in range(K):
        xs = [(x + c * K + (y + i) // K * K) for x, y, c in xyc]
        ts = [0 for x in range(K * 2)]
        for x in xs:
            ts[x % (K * 2)] += 1
        #print(ts)
        rcount = sum(ts[i] for i in range(K))
        rmax = max(rmax, rcount)
        rcs = [rcount]
        for j in range(K):
            rcount += ts[K + j] - ts[j]
            rcs.append(rcount)
            rmax = max(rmax, rcount)
        #print(rcs)
    return rmax

#import random
#n = 20
#K = 10
#xyc = [(random.randint(0, 10000), random.randint(0, 10000), random.randint(0, 10000) % 2) for i in range(n)]

#print(calc0(K, xyc))
print(calc(K, xyc))