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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}
macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;

struct CumSum2D(Vec<Vec<i64>>);
impl CumSum2D {
    fn new(data: &Vec<Vec<i64>>) -> CumSum2D {
        let h = data.len();
        let w = data[0].len();
        let mut cs = vec![vec![0; w + 1]; h + 1];
        for i in 0..h {
            for j in 0..w {
                cs[i + 1][j + 1] = data[i][j] + cs[i][j + 1] + cs[i + 1][j] - cs[i][j];
            }
        }
        CumSum2D(cs)
    }
    fn sum_up(&self, x: usize, y: usize) -> i64 {
        let x = std::cmp::min(x, self.0.len());
        let y = std::cmp::min(y, self.0[0].len());
        self.0[x][y]
    }
    fn sum(&self, xrange: std::ops::Range<usize>, yrange: std::ops::Range<usize>) -> i64 {
        if xrange.end <= xrange.start || yrange.end <= yrange.start {
            0
        } else {
            self.sum_up(xrange.end, yrange.end)
                - self.sum_up(xrange.start, yrange.end)
                - self.sum_up(xrange.end, yrange.start)
                + self.sum_up(xrange.start, yrange.start)
        }
    }
}

fn main() {
    let mut sc = Scanner::new();
    let q: usize = sc.cin();
    let k: usize = sc.cin();
    let mut b = vec![vec![0; 2 * k]; 2 * k];
    let mut w = vec![vec![0; 2 * k]; 2 * k];
    for _ in 0..q {
        let x: usize = sc.cin();
        let y: usize = sc.cin();
        let col: String = sc.cin();
        if col == "B" {
            b[x % (2 * k)][y % (2 * k)] += 1;
        } else {
            w[x % (2 * k)][y % (2 * k)] += 1;
        }
    }
    let cs_b = CumSum2D::new(&b);
    let cs_w = CumSum2D::new(&w);

    let mut ans = 0;
    for s in 0..k + 1 {
        for t in 0..k + 1 {
            let a =
                  cs_b.sum(0..t, 0..s)
                + cs_w.sum(0..t, s..s+k)
                + cs_b.sum(0..t, s+k..k+k)
                + cs_w.sum(t..t+k, 0..s)
                + cs_b.sum(t..t+k, s..s+k)
                + cs_w.sum(t..t+k, s+k..k+k)
                + cs_b.sum(t+k..k+k, 0..s)
                + cs_w.sum(t+k..k+k, s..s+k)
                + cs_b.sum(t+k..k+k, s+k..k+k);
            ans = max!(ans, a);
            let a =
                  cs_w.sum(0..t, 0..s)
                + cs_b.sum(0..t, s..s+k)
                + cs_w.sum(0..t, s+k..k+k)
                + cs_b.sum(t..t+k, 0..s)
                + cs_w.sum(t..t+k, s..s+k)
                + cs_b.sum(t..t+k, s+k..k+k)
                + cs_w.sum(t+k..k+k, 0..s)
                + cs_b.sum(t+k..k+k, s..s+k)
                + cs_w.sum(t+k..k+k, s+k..k+k);
            ans = max!(ans, a);
        }
    }
    put!(ans);
}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> { self.cin::<String>().chars().collect() }
    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.cin()).collect() }
}