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:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

k = 0
l = 0
acc = None

def count_up(y1, x1, y2, x2):
    global acc, l
    if y1 > y2 or x1 > x2:
        return 0
    y1 = max(y1 + 1, 1)
    x1 = max(x1 + 1, 1)
    y2 = min(y2 + 1, l)
    x2 = min(x2 + 1, l)
    return acc[y2][x2] + acc[y1 - 1][x1 - 1] - acc[y1 - 1][x2] - acc[y2][x1 - 1]

def main():
    global acc, k, l
    n, k = map(int, sys.stdin.readline().split())
    xycs = (map(lambda x: int(x) if x != 'B' and x != 'W' else x,
                 line.split()) for line in sys.stdin.readlines())

    l = k * 2
    yxs = (((y + k) % l, x % l) if c == 'W' else (y % l, x % l) for x, y, c in xycs)

    acc = [[0] * (l + 1) for _ in range(l + 1)]
    for y, x in yxs:
        acc[y + 1][x + 1] += 1
    for j in range(l):
        for i in range(l):
            acc[j + 1][i + 1] += acc[j + 1][i] + acc[j][i + 1] - acc[j][i]

    max_cnt = 0
    for j in range(k):
        for i in range(k):
            cnt = count_up(j, i, j + k - 1, i + k - 1) + count_up(0, 0, j - 1, i - 1) + count_up(0, i + k, j - 1, l - 1) + count_up(j + k, 0, l - 1, i - 1) + count_up(j + k, i + k, l - 1, l - 1)
            max_cnt = max(max_cnt, cnt)

    print(max_cnt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

k = 0
l = 0
acc = None

def count_up(y1, x1, y2, x2):
    global acc, l
    if y1 >= y2 or x1 >= x2:
        return 0
    y1 = max(y1 + 1, 1)
    x1 = max(x1 + 1, 1)
    y2 = min(y2 + 1, l)
    x2 = min(x2 + 1, l)
    return acc[y2][x2] + acc[y1 - 1][x1 - 1] - acc[y1 - 1][x2] - acc[y2][x1 - 1]

def main():
    global acc, k, l
    n, k = map(int, sys.stdin.readline().split())
    xycs = (map(lambda x: int(x) if x != 'B' and x != 'W' else x,
                 line.split()) for line in sys.stdin.readlines())

    l = k * 2
    yxs = (((y + k) % l, x % l) if c == 'W' else (y % l, x % l) for x, y, c in xycs)

    acc = [[0] * (l + 1) for _ in range(l + 1)]
    for y, x in yxs:
        acc[y + 1][x + 1] += 1
    for j in range(l):
        for i in range(l):
            acc[j + 1][i + 1] += acc[j + 1][i] + acc[j][i + 1] - acc[j][i]

    max_cnt = 0
    for j in range(k):
        for i in range(k):
            cnt = count_up(j, i, j + k - 1, i + k - 1) + count_up(0, 0, j - 1, i - 1) + count_up(0, i + 1, j - 1, i + k) + count_up(j + 1, 0, l - 1, i - 1) + count_up(j + k, i + k, l - 1, l - 1)
            max_cnt = max(max_cnt, cnt)

    print(max_cnt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

k = 0
l = 0
acc = None

def count_up(y1, x1, y2, x2):
    global acc, l
    if y1 > y2 or x1 > x2:
        return 0
    y1 = max(y1 + 1, 1)
    x1 = max(x1 + 1, 1)
    y2 = min(y2 + 1, l)
    x2 = min(x2 + 1, l)
    return acc[y2][x2] + acc[y1 - 1][x1 - 1] - acc[y1 - 1][x2] - acc[y2][x1 - 1]

def main():
    global acc, k, l
    n, k = map(int, sys.stdin.readline().split())
    xycs = (map(lambda x: int(x) if x != 'B' and x != 'W' else x,
                 line.split()) for line in sys.stdin.readlines())

    l = k * 2
    yxs = (((y + k) % l, x % l) if c == 'W' else (y % l, x % l) for x, y, c in xycs)

    acc = [[0] * (l + 1) for _ in range(l + 1)]
    for y, x in yxs:
        acc[y + 1][x + 1] += 1
    for j in range(l):
        for i in range(l):
            acc[j + 1][i + 1] += acc[j + 1][i] + acc[j][i + 1] - acc[j][i]

    max_cnt = 0
    for j in range(k):
        for i in range(k):
            cnt = count_up(j, i, j + k - 1, i + k - 1) + count_up(0, 0, j - 1, i - 1) + count_up(0, i + 1, j - 1, i + k) + count_up(j + 1, 0, l - 1, i - 1) + count_up(j + k, i + k, l - 1, l - 1)
            max_cnt = max(max_cnt, cnt)

    print(max_cnt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

k = 0
l = 0
acc = None

def count_up(y1, x1, y2, x2):
    global acc, l
    if y1 > y2 or x1 > x2:
        return 0
    y1 = min(max(y1 + 1, 1), l)
    x1 = min(max(x1 + 1, 1), l)
    y2 = max(min(y2 + 1, l), 1)
    x2 = max(min(x2 + 1, l), 1)
    return acc[y2][x2] + acc[y1 - 1][x1 - 1] - acc[y1 - 1][x2] - acc[y2][x1 - 1]

def main():
    global acc, k, l
    n, k = map(int, sys.stdin.readline().split())
    xycs = (map(lambda x: int(x) if x != 'B' and x != 'W' else x,
                 line.split()) for line in sys.stdin.readlines())

    l = k * 2
    yxs = (((y + k) % l, x % l) if c == 'W' else (y % l, x % l) for x, y, c in xycs)

    acc = [[0] * (l + 1) for _ in range(l + 1)]
    for y, x in yxs:
        acc[y + 1][x + 1] += 1
    for j in range(l):
        for i in range(l):
            acc[j + 1][i + 1] += acc[j + 1][i] + acc[j][i + 1] - acc[j][i]

    max_cnt = 0
    for j in range(k):
        for i in range(k):
            cnt = count_up(j, i, j + k - 1, i + k - 1) + count_up(0, 0, j - 1, i - 1) + count_up(0, i + 1, j - 1, i + k) + count_up(j + 1, 0, l - 1, i - 1) + count_up(j + k, i + k, l - 1, l - 1)
            max_cnt = max(max_cnt, cnt)

    print(max_cnt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

k = 0
l = 0
acc = None

def count_up(y1, x1, y2, x2):
    global acc, l
    y1 = max(y1 + 1, 1)
    x1 = max(x1 + 1, 1)
    y2 = min(y2 + 1, l)
    x2 = min(x2 + 1, l)
    if y1 != y2 and x1 != x2:
        return acc[y2][x2] - acc[y1 - 1][x2] - acc[y2][x1 - 1] +  acc[y1 - 1][x1 - 1]
    else:
        return 0

def main():
    global acc, k, l
    n, k = map(int, sys.stdin.readline().split())
    xycs = (map(lambda x: int(x) if x != 'B' and x != 'W' else x,
                 line.split()) for line in sys.stdin.readlines())

    l = k * 2
    yxs = (((y + k) % l, x % l) if c == 'W' else (y % l, x % l) for x, y, c in xycs)

    acc = [[0] * (l + 1) for _ in range(l + 1)]
    for y, x in yxs:
        acc[y + 1][x + 1] += 1
    for j in range(l):
        for i in range(l):
            acc[j + 1][i + 1] += acc[j + 1][i] + acc[j][i + 1] - acc[j][i]

    max_cnt = 0
    for j in range(k):
        for i in range(k):
            cnt = count_up(0, 0, j - 1, i - 1) + count_up(0, i + k, j - 1, l - 1) + count_up(j, i, j + k - 1, i + k - 1) + count_up(j + k, 0, l - 1, i - 1) + count_up(j + k, i + k, l - 1, l - 1)
            max_cnt = max(max_cnt, cnt)

    print(max_cnt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

k = 0
l = 0
acc = None

def count_up(y1, x1, y2, x2):
    global acc, l
    y1 = max(y1 + 1, 1)
    x1 = max(x1 + 1, 1)
    y2 = min(y2 + 1, l)
    x2 = min(x2 + 1, l)
    return acc[y2][x2] - acc[y1 - 1][x2] - acc[y2][x1 - 1] +  acc[y1 - 1][x1 - 1]

def main():
    global acc, k, l
    n, k = map(int, sys.stdin.readline().split())
    xycs = (map(lambda x: int(x) if x != 'B' and x != 'W' else x,
                 line.split()) for line in sys.stdin.readlines())

    l = k * 2
    yxs = (((y + k) % l, x % l) if c == 'W' else (y % l, x % l) for x, y, c in xycs)

    acc = [[0] * (l + 1) for _ in range(l + 1)]
    for y, x in yxs:
        acc[y + 1][x + 1] += 1
    for j in range(l):
        for i in range(l):
            acc[j + 1][i + 1] += acc[j + 1][i] + acc[j][i + 1] - acc[j][i]

    max_cnt = 0
    for j in range(k):
        for i in range(k):
            cnt = count_up(0, 0, j - 1, i - 1) + count_up(0, i + k, j - 1, l - 1) + count_up(j, i, j + k - 1, i + k - 1) + count_up(j + k, 0, l - 1, i - 1) + count_up(j + k, i + k, l - 1, l - 1)
            max_cnt = max(max_cnt, cnt)

    print(max_cnt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
M=[[0 for i in range(2*K)] for j in range(2*K)]

for i in range(N):
  a,b,c = map(str,input().split())
  if c == 'W':
    a=(int(a)+K)%(2*K)
    b=int(b)%(2*K)
  else:
    a=int(a)%(2*K)
    b=int(b)%(2*K)
  M[a][b]+=1

for i in range(1,2*K):
  M[i][0]+=M[i-1][0]
  M[0][i]+=M[0][i-1]
  
for i in range(1,2*K):
  for j in range(1,2*K):
    M[i][j]+=(M[i-1][j]+M[i][j-1]-M[i-1][j-1])

def solve(x,y):
  res=0
  if x==0 and y==0:
    res=M[2*K-1][2*K-1]-M[K-1][2*K-1]-M[2*K-1][K-1]+M[K-1][K-1]
  elif x==0:
    res=M[K-1][K-1+y]-M[K-1][y-1]+M[2*K-1][y-1]-M[K-1][y-1]+M[2*K-1][2*K-1]-M[K-1][2*K-1]-M[2*K-1][K-1+y]+M[K-1][K-1+y]
  elif y==0:
    res=M[K-1+x][K-1]-M[x-1][K-1]+M[x-1][2*K-1]-M[x-1][K-1]+M[2*K-1][2*K-1]-M[2*K-1][K-1]-M[K-1+x][2*K-1]+M[K-1+x][K-1]
  else:
    res=M[2*K-1][2*K-1]-((M[x-1][K-1+y]-M[x-1][y-1])+(M[K-1+x][y-1]-M[x-1][y-1])+(M[K-1+x][2*K-1]-M[x-1][2*K-1]-M[K-1+x][K-1+y]+M[x-1][K-1+y])+(M[2*K-1][K-1+y]-M[K-1+x][K-1+y]-M[2*K-1][y-1]+M[K-1+x][y-1]))
  return max(res,N-res)
    
ans=0
for i in range(K):
  for j in range(K):
    ans=max(ans,solve(i,j))
print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
M=[[0 for i in range(2*K)] for j in range(2*K)]

for i in range(N):
  a,b,c = map(str,input().split())
  if c == 'W':
    a=(int(a)+K)%(2*K)
    b=int(b)%(2*K)
  else:
    a=int(a)%(2*K)
    b=int(b)%(2*K)
  M[a][b]+=1

for i in range(1,2*K):
  M[i][0]+=M[i-1][0]
  M[0][i]+=M[0][i-1]
  
for i in range(1,2*K):
  for j in range(1,2*K):
    M[i][j]+=(M[i-1][j]+M[i][j-1]-M[i-1][j-1])

def solve(x,y):
  if x==0 and y==0:
    return M[2*K-1][2*K-1]-M[K-1][2*K-1]-M[2*K-1][K-1]+M[K-1][K-1]
  elif x==0:
    return M[K-1][K-1+y]-M[K-1][y-1]+M[2*K-1][y-1]-M[K-1][y-1]+M[2*K-1][2*K-1]-M[K-1][2*K-1]-M[2*K-1][K-1+y]+M[K-1][K-1+y]
  elif y==0:
    return M[K-1+x][K-1]-M[x-1][K-1]+M[x-1][2*K-1]-M[x-1][K-1]+M[2*K-1][2*K-1]-M[2*K-1][K-1]-M[K-1+x][2*K-1]+M[K-1+x][K-1]
  else:
    return M[2*K-1][2*K-1]-((M[x-1][K-1+y]-M[x-1][y-1])+(M[K-1+x][y-1]-M[x-1][y-1])+(M[K-1+x][2*K-1]-M[x-1][2*K-1]-M[K-1+x][K-1+y]+M[x-1][K-1+y])+(M[2*K-1][K-1+y]-M[K-1+x][K-1+y]-M[2*K-1][y-1]+M[K-1+x][y-1]))

ans=0
for i in range(K):
  for j in range(K):
    ans=max(ans,solve(i,j))
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

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

count = [[[0 for i in range(K)] for j in range(K)] for k in range(2)]

Ksq = K * 2
for l in L:
    x = int(l[0]) % Ksq
    y = int(l[1]) % Ksq
    c = l[2]

    for i in range(K):
        for j in range(K):
            if (x + j) % Ksq < K:
                if (y + i) % Ksq < K:
                    if c == 'W':
                        count[0][i][j] += 1
                    else:
                        count[1][i][j] += 1
                else:
                    if c == 'B':
                        count[0][i][j] += 1
                    else:
                        count[1][i][j] += 1
            else:
                if (y + i) % Ksq < K:
                    if c == 'B':
                        count[0][i][j] += 1
                    else:
                        count[1][i][j] += 1
                else:
                    if c == 'W':
                        count[0][i][j] += 1
                    else:
                        count[1][i][j] += 1

print(max(max(max(count))))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

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

count = [[[0 for i in range(K)] for j in range(K)] for k in range(2)]

Ksq = K * 2
for l in L:
    x = int(l[0]) % Ksq
    y = int(l[1]) % Ksq
    c = l[2]

    for i in range(K):
        for j in range(K):
            if (x + j) % Ksq < K:
                if (y + i) % Ksq < K:
                    if c == 'W':
                        count[0][i][j] += 1
                else:
                    if c == 'B':
                        count[0][i][j] += 1
            else:
                if (y + i) % Ksq < K:
                    if c == 'B':
                        count[0][i][j] += 1
                else:
                    if c == 'W':
                        count[0][i][j] += 1

for l in L:
    x = int(l[0]) % Ksq
    y = int(l[1]) % Ksq
    c = l[2]

    for i in range(K):
        for j in range(K):
            if (x + j) % Ksq < K:
                if (y + i) % Ksq < K:
                    if c == 'B':
                        count[1][i][j] += 1
                else:
                    if c == 'W':
                        count[1][i][j] += 1
            else:
                if (y + i) % Ksq < K:
                    if c == 'W':
                        count[1][i][j] += 1
                else:
                    if c == 'B':
                        count[1][i][j] += 1

print(max(max(max(count))))

Submission status: Wrong Answer

Solution code:
X=0
Y=1
C=2
item = input().split(' ')
item[0] = int(item[0])
item[1] = int(item[1])
hope = []
able = 0
for i in range(0,item[0]):
    hope.append(input().split(' '))
    hope[i][X] = int(hope[i][X]) % (item[1]*2)
    hope[i][Y] = int(hope[i][Y]) % (item[1]*2)

hope.sort(key=lambda x:(x[0],x[1]))

rehope = []
for i in range(0,len(hope)):
    if i+1 >= len(hope):
        rehope.append(hope[i])
        break
    if hope[i][X] == hope[i+1][X] and hope[i][Y] == hope[i+1][Y] and hope[i][C] != hope[i+1][C]:
        able += 1
        i += 1
    else:
        rehope.append(hope[i])

max = 0
for i in range(item[1]):
    for j in range(item[1]):
        temp = 0
        for k in range(0, len(rehope)):
            if ((rehope[k][X] < item[1] - i and rehope[k][Y] < item[1] - j) or (rehope[k][X] >= item[1] - i and rehope[k][Y] >= item[1] - j)) and rehope[k][C] == 'W' :
                temp += 1
            elif rehope[k][C] == 'B':
                temp += 1
        if max < temp:
            max = temp
a = able + max
print(a)

Submission status: Wrong Answer

Solution code:
def blackSum(x,y,x2,y2):
    return bs[y2][x2] - bs[y2][x-1] - bs[y-1][x2] + bs[y-1][x-1]

n,k = [int(i) for i in input().split()]
black = [[0 for i in range(2*k+2)] for j in range(2*k+2)]
for i in range(n):
    xi,yi,ci = list(input().split())
    x = int(xi)%(2*k)
    if ci == "W":
        y = (int(yi) + k)%(2*k)
    else:
        y = int(yi)%(2*k)
    black[y+2][x+2] += 1
bs = [[0 for i in range(2*k+2)] for j in range(2*k+2)]
for i in range(1,2*k+2):
    for j in range(1,2*k+2):
        bs[i][j] = black[i][j] + bs[i-1][j] + bs[i][j-1] - bs[i-1][j-1]
max = 0
#左上の黒の位置を指定する。
for i in range(2,k+2):
    for j in range(2,k+2):
        sum = blackSum(j,i,j+k-1,i+k-1) + \
              blackSum(1,1,j-1,i-1) + \
              blackSum(1,i+k,j-1,2*k+1) + \
              blackSum(j+k,1,2*k+1,i-1) + \
              blackSum(j+k,i+k,2*k+1,2*k+1)
        if sum>max:
            max=sum
print(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()
    k2 = k*2
    a = [[0]*(k2) for _ in range(k2)]
    for _ in range(n):
        x,y,c = LS()
        x = int(x)
        y = int(y)
        if c == 'W':
            y += k
        x %= k2
        y %= k2
        if x >= k and y >= k:
            x -= k
            y -= k
        a[x][y] += 1
        if x < k:
            a[x+k][y] -= 1
        if y < k:
            a[x][y+k] -= 1
        if x < k and y < k:
            a[x+k][y+k] += 1
        a[x][y] += 1
        a[max(0,x-k)][max(0,y-k)] += 1
        a[x][max(0,y-k)] -= 1
        a[max(0,x-k)][y] -= 1

        if x < k:
            x += k
        else:
            x -= k
        if y < k:
            y += k
        else:
            y -= k

        a[x][y] += 1
        if x < k:
            a[x+k][y] -= 1
        if y < k:
            a[x][y+k] -= 1
        if x < k and y < k:
            a[x+k][y+k] += 1

        if x < k or y < k and not (x < k and y < k):
            a[x][y] += 1
            a[max(0,x-k)][max(0,y-k)] += 1
            a[x][max(0,y-k)] -= 1
            a[max(0,x-k)][y] -= 1


    rui = Ruiwa(a)

    return max(max(c) for c in 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()
    k2 = k*2
    a = [[0]*(k2) for _ in range(k2)]
    for _ in range(n):
        x,y,c = LS()
        x = int(x)
        y = int(y)
        if c == 'W':
            y += k
        x %= k2
        y %= k2
        if x >= k and y >= k:
            x -= k
            y -= k
        a[x][y] += 1
        if x < k:
            a[x+k][y] -= 1
        if y < k:
            a[x][y+k] -= 1
        if x < k and y < k:
            a[x+k][y+k] += 1
        a[x][y] += 1
        a[max(0,x-k)][max(0,y-k)] += 1
        a[x][max(0,y-k)] -= 1
        a[max(0,x-k)][y] -= 1

        if x < k:
            x += k
        else:
            x -= k
        if y < k:
            y += k
        else:
            y -= k

        a[x][y] += 1
        if x < k:
            a[x+k][y] -= 1
        if y < k:
            a[x][y+k] -= 1
        if x < k and y < k:
            a[x+k][y+k] += 1

        if x < k or y < k:
            a[x][y] += 1
            a[max(0,x-k)][max(0,y-k)] += 1
            a[x][max(0,y-k)] -= 1
            a[max(0,x-k)][y] -= 1


    rui = Ruiwa(a)

    return max(max(c) for c in 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()
    k2 = k*2
    a = [[0]*(k2) for _ in range(k2)]
    for _ in range(n):
        x,y,c = LS()
        x = int(x)
        y = int(y)
        if c == 'W':
            y += k
        x %= k2
        y %= k2
        a[x][y] += 1
        if x < k:
            a[x+k][y] -= 1
        if y < k:
            a[x][y+k] -= 1
        if x < k and y < k:
            a[x+k][y+k] += 1

        if x < k:
            x += k
        else:
            x -= k
        if y < k:
            y += k
        else:
            y -= k

        a[x][y] += 1
        if x < k:
            a[x+k][y] -= 1
        if y < k:
            a[x][y+k] -= 1
        if x < k and y < k:
            a[x+k][y+k] += 1

    rui = Ruiwa(a)

    return max(max(c) for c in 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()
    k2 = k*2
    a = [[0]*(k2+1) for _ in range(k2+1)]
    for _ in range(n):
        x,y,c = LS()
        x = int(x)
        y = int(y)
        if c == 'W':
            y += k
        x %= k2
        y %= k2
        a[x][y] += 1
        if x < k:
            a[x+k][y] -= 1
        if y < k:
            a[x][y+k] -= 1
        if x < k and y < k:
            a[x+k][y+k] += 1

        if x < k:
            x += k
        else:
            x -= k
        if y < k:
            y += k
        else:
            y -= k

        a[x][y] += 1
        if x < k:
            a[x+k][y] -= 1
        if y < k:
            a[x][y+k] -= 1
        if x < k and y < k:
            a[x+k][y+k] += 1

    rui = Ruiwa(a)

    return max(max(c) for c in rui.R)


print(main())

Submission status: Wrong Answer

Solution code:
def blackSum(x,y,x2,y2):
    return bs[y2][x2] - bs[y2][x-1] - bs[y-1][x2] + bs[y-1][x-1]

n,k = [int(i) for i in input().split()]
black = [[0 for i in range(2*k+1)] for j in range(2*k+1)]
for i in range(n):
    xi,yi,ci = list(input().split())
    x = int(xi)%(2*k)
    if ci == "W":
        y = (int(yi) + k)%(2*k)
    else:
        y = int(yi)%(2*k)
    black[y+1][x+1] += 1
bs = [[0 for i in range(2*k+1)] for j in range(2*k+1)]
for i in range(1,2*k+1):
    for j in range(1,2*k+1):
        bs[i][j] = black[i][j] + bs[i-1][j] + bs[i][j-1] - bs[i-1][j-1]
max = 0
#左上の黒の位置を指定する。
for i in range(1,k+1):
    for j in range(1,k+1):
        sum = blackSum(j,i,j+k-1,i+k-1) + blackSum(1,1,j-1,i-1) + blackSum(1,i+k,j-1,2*k) + blackSum(j+k,1,2*k,i-1) + blackSum(j+k,i+k,2*k,2*k)
        if sum>max:
            max=sum
print(max)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x, y, c = [None] * N, [None] * N, [None] * N
KK = 2 * K
table = [[0] * (KK+1) for i in range(KK+1)]

for i in range(N):
    x[i], y[i], c[i] = input().split()
    x[i], y[i] = int(x[i]), int(y[i])
    if c[i] == 'W':
        x[i] += K
        c[i] = 'B'
    x[i] %= KK
    y[i] %= KK
    table[y[i]+1][x[i]+1] += 1

for i in range(KK+1):
    for j in range(KK):
        table[i][j+1] += table[i][j]
for i in range(KK):
    for j in range(KK+1):
        table[i+1][j] += table[i][j]

def s(x1, x2, y1, y2):
    global table, KK
    MOD = KK + 1
    x1 %= MOD
    y1 %= MOD
    x2 %= MOD
    y2 %= MOD
    x11 = x1
    x21 = 0
    y11 = y1
    y21 = 0
    if x1 < x2:
        x12 = x2
        x22 = 0
    else:
        x12 = KK
        x22 = x2 + 1
    if y1 < y2:
        y12 = y2
        y22 = 0
    else:
        y12 = KK
        y22 = y2 + 1
    f = lambda _x1, _x2, _y1, _y2: \
            table[_y2][_x2] - table[_y2][_x1] \
            - table[_y1][_x2] + table[_y1][_x1]
    # print('x11: %d, x12: %d, y11: %d, y12: %d' % (x11, x12, y11, y12))
    # a1 = f(x11, x12, y11, y12)
    # print('x21: %d, x22: %d, y11: %d, y12: %d' % (x21, x22, y11, y12))
    # a2 = f(x21, x22, y11, y12)
    # print('x11: %d, x12: %d, y21: %d, y22: %d' % (x11, x12, y21, y22))
    # a3 = f(x11, x12, y21, y22)
    # print('x21: %d, x22: %d, y21: %d, y22: %d' % (x21, x22, y21, y22))
    # a4 = f(x21, x22, y21, y22)
    # print(a1, a2, a3, a4)
    return f(x11, x12, y11, y12) + f(x21, x22, y11, y12) \
            + f(x11, x12, y21, y22) + f(x21, x22, y21, y22)

ans = 0
for i in range(KK):
    for j in range(K):
        tmp = s(i, i+K, j, j+K)
        ni, nj = i + K, j + K
        if ni >= KK:
            ni = ni % KK + 1
        if nj >= KK:
            nj = nj % KK + 1
        tmp += s(ni, ni+K, nj, nj+K)
        # print('tmp: %d' % tmp)
        ans = max((ans, tmp))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x, y, c = [None] * N, [None] * N, [None] * N
KK = 2 * K
table = [[0] * (KK+1) for i in range(KK+1)]

for i in range(N):
    x[i], y[i], c[i] = input().split()
    x[i], y[i] = int(x[i]), int(y[i])
    if c[i] == 'W':
        x[i] += K
        c[i] = 'B'
    x[i] %= KK
    y[i] %= KK
    table[y[i]+1][x[i]+1] += 1

for i in range(KK+1):
    for j in range(KK):
        table[i][j+1] += table[i][j]
for i in range(KK):
    for j in range(KK+1):
        table[i+1][j] += table[i][j]

def s(x1, x2, y1, y2):
    global table, KK
    MOD = KK + 1
    x1 %= MOD
    y1 %= MOD
    x2 %= MOD
    y2 %= MOD
    x11 = x1
    x21 = 0
    y11 = y1
    y21 = 0
    if x1 < x2:
        x12 = x2
        x22 = 0
    else:
        x12 = KK
        x22 = x2 + 1
    if y1 < y2:
        y12 = y2
        y22 = 0
    else:
        y12 = KK
        y22 = y2 + 1
    f = lambda _x1, _x2, _y1, _y2: \
            table[_y2][_x2] - table[_y2][_x1] \
            - table[_y1][_x2] + table[_y1][_x1]
    # print('x11: %d, x12: %d, y11: %d, y12: %d' % (x11, x12, y11, y12))
    # a1 = f(x11, x12, y11, y12)
    # print('x21: %d, x22: %d, y11: %d, y12: %d' % (x21, x22, y11, y12))
    # a2 = f(x21, x22, y11, y12)
    # print('x11: %d, x12: %d, y21: %d, y22: %d' % (x11, x12, y21, y22))
    # a3 = f(x11, x12, y21, y22)
    # print('x21: %d, x22: %d, y21: %d, y22: %d' % (x21, x22, y21, y22))
    # a4 = f(x21, x22, y21, y22)
    # print(a1, a2, a3, a4)
    return f(x11, x12, y11, y12) + f(x21, x22, y11, y12) \
            + f(x11, x12, y21, y22) + f(x21, x22, y21, y22)

ans = 0
for i in range(KK):
    for j in range(KK):
        tmp = s(i, i+K, j, j+K)
        tmp += s(i+K, i+KK, j+K, j+KK)
        # print('tmp: %d' % tmp)
        ans = max((ans, tmp))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
x, y, c = [None] * N, [None] * N, [None] * N
KK = 2 * K
table = [[0] * (KK+1) for i in range(KK+1)]

for i in range(N):
    x[i], y[i], c[i] = input().split()
    x[i], y[i] = int(x[i]), int(y[i])
    if c[i] == 'W':
        x[i] += K
        c[i] = 'B'
    x[i] %= KK
    y[i] %= KK
    table[y[i]+1][x[i]+1] += 1

for i in range(KK+1):
    for j in range(KK):
        table[i][j+1] += table[i][j]
for i in range(KK):
    for j in range(KK+1):
        table[i+1][j] += table[i][j]

def s(x1, x2, y1, y2):
    global table, KK
    MOD = KK + 1
    x1 %= MOD
    y1 %= MOD
    x2 %= MOD
    y2 %= MOD
    x11 = x1
    x21 = 0
    y11 = y1
    y21 = 0
    if x1 < x2:
        x12 = x2
        x22 = 0
    else:
        x12 = KK
        x22 = x2 + 1
    if y1 < y2:
        y12 = y2
        y22 = 0
    else:
        y12 = KK
        y22 = y2 + 1
    f = lambda _x1, _x2, _y1, _y2: \
            table[_y2][_x2] - table[_y2][_x1] \
            - table[_y1][_x2] + table[_y1][_x1]
    # print('x11: %d, x12: %d, y11: %d, y12: %d' % (x11, x12, y11, y12))
    # a1 = f(x11, x12, y11, y12)
    # print('x21: %d, x22: %d, y11: %d, y12: %d' % (x21, x22, y11, y12))
    # a2 = f(x21, x22, y11, y12)
    # print('x11: %d, x12: %d, y21: %d, y22: %d' % (x11, x12, y21, y22))
    # a3 = f(x11, x12, y21, y22)
    # print('x21: %d, x22: %d, y21: %d, y22: %d' % (x21, x22, y21, y22))
    # a4 = f(x21, x22, y21, y22)
    # print(a1, a2, a3, a4)
    return f(x11, x12, y11, y12) + f(x21, x22, y11, y12) \
            + f(x11, x12, y21, y22) + f(x21, x22, y21, y22)

ans = 0
for i in range(KK):
    for j in range(K):
        tmp = s(i, i+K, j, j+K)
        tmp += s(i+K, i+KK, j+K, j+KK)
        # print('tmp: %d' % tmp)
        ans = max((ans, tmp))
print(ans)