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

Solution in Go (1.6) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

var hopes [2][2001][2001]int

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	k := getNextInt(scanner)

	cmap := map[string]int{
		"W": 0,
		"B": 1,
	}

	for i := 0; i < n; i++ {
		x := getNextInt(scanner) % (k << 1)
		y := getNextInt(scanner) % (k << 1)
		c := getNextString(scanner)
		hopes[cmap[c]][y+1][x+1]++
	}

	for i := 0; i < 2; i++ {
		for y := 0; y <= k<<1; y++ {
			for x := 0; x < k<<1; x++ {
				hopes[i][y][x+1] += hopes[i][y][x]
			}
		}
	}
	for i := 0; i < 2; i++ {
		for x := 0; x <= k<<1; x++ {
			for y := 0; y < k<<1; y++ {
				hopes[i][y+1][x] += hopes[i][y][x]
			}
		}
	}

	ans := 0
	var wb [2][3][3]int

	for y := 0; y < k; y++ {
		for x := 0; x < k; x++ {
			for i := 0; i < 2; i++ {
				wb[i][0][0] = count(i, 0, 0, y, x)
				wb[i][0][1] = count(i, 0, x, y, x+k)
				wb[i][0][2] = count(i, 0, x+k, y, k*2)
				wb[i][1][0] = count(i, y, 0, y+k, x)
				wb[i][1][1] = count(i, y, x, y+k, x+k)
				wb[i][1][2] = count(i, y, x+k, y+k, k*2)
				wb[i][2][0] = count(i, y+k, 0, k*2, x)
				wb[i][2][1] = count(i, y+k, x, k*2, x+k)
				wb[i][2][2] = count(i, y+k, x+k, k*2, k*2)
			}

			for i := 0; i < 2; i++ {
				done := 0
				for yy := 0; yy < 3; yy++ {
					for xx := 0; xx < 3; xx++ {
						done += wb[i^((yy+xx)%2)][yy][xx]
					}
				}

				if ans < done {
					ans = done
				}
			}
		}
	}

	fmt.Fprintln(writer, ans)
	writer.Flush()
}

func count(i, y1, x1, y2, x2 int) int {
	return hopes[i][y2][x2] - hopes[i][y1][x2] - hopes[i][y2][x1] + hopes[i][y1][x1]
}