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)]
use std::f64::*;
use std::cmp::*;
use std::collections::*;

macro_rules! dump{
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

const INF: i64 = 0x3f3f3f3f3f3f3f3f;
const MOD: i64 = 1000000007;
const NOTHING: usize = std::usize::MAX;
const DX: [usize; 4] = [1, 0, std::usize::MAX, 0];
const DY: [usize; 4] = [0, 1, 0, std::usize::MAX];

fn main() {
    let (n, k): (usize, usize) = readln();
    let mut ws: Vec<Vec<usize>> = vec![vec![0;2 * k]; 2 * k];
    let mut bs: Vec<Vec<usize>> = vec![vec![0;2 * k]; 2 * k];
    let mut b: usize = 0;
    let mut w: usize = 0;

    for _ in 0..n {
        let (x, y, s): (usize, usize, String) = readln();
        if s == "W" {
            ws[x % (2 * k)][y % (2 * k)] += 1;
            w += 1;
        } else {
            bs[x % (2 * k)][y % (2 * k)] += 1;
            b += 1;
        }
    }

    for i in 0..2 * k {
        for j in 0..2 * k {
            if i > 0 {
                ws[i][j] += ws[i - 1][j];
                bs[i][j] += bs[i - 1][j];
            }

            if j > 0 {
                ws[i][j] += ws[i][j - 1];
                bs[i][j] += bs[i][j - 1];
            }
            if i > 0 && j > 0 {
                ws[i][j] -= ws[i - 1][j - 1];
                bs[i][j] -= bs[i - 1][j - 1];
            }
        }
    }

    fn sum(m: &Vec<Vec<usize>>, x0: usize, y0: usize, x1: usize, y1: usize) -> usize {
        let mut res = 0;
        if x1 > 0 && y1 > 0 {
            res += m[x1 - 1][y1 - 1]
        };
        if x0 > 0 && y0 > 0 {
            res += m[x0 - 1][y0 - 1];
        }
        if x0 > 0 && y1 > 0 {
            res -= m[x0 - 1][y1 - 1];
        }
        if y0 > 0 && x1 > 0 {
            res -= m[x1 - 1][y0 - 1];
        }
        res
    }

    fn query(m: &Vec<Vec<usize>>, rx: usize, ry: usize, k: usize) -> usize {
        let mut res = 0;
        res += sum(m, 0, 0, rx, ry);
        res += sum(m, 0, ry + k, rx, 2 * k);
        res += sum(m, rx, ry, rx + k, ry + k);
        res += sum(m, rx + k, 0, 2 * k, ry);
        res += sum(m, rx + k, ry + k, 2 * k, 2 * k);
        res
    }
    let mut res: usize = 0;
    for rx in 0..k {
        for ry in 0..k {
            let res_w = query(&ws, rx, ry, k);
            let res_b = query(&bs, rx, ry, k);
            res = max(res, res_w + b - res_b);
            res = max(res, res_b + w - res_w);
        }
    }

    println!("{}", res);
}

trait Read {
    fn read(s: &str) -> Self;
}

fn readln<T: Read>() -> T {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    T::read(buf.trim_right())
}

macro_rules! read_impl{
    ($($t:ty)*) => ($(
        impl Read for $t {
            fn read(s: &str) -> $t{
                s.parse().unwrap()
            }
        }
    )*)
}
read_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }

impl Read for String {
    fn read(s: &str) -> Self {
        s.to_string()
    }
}

impl Read for Vec<char> {
    fn read(s: &str) -> Self {
        s.chars().collect()
    }
}

impl<T: Read> Read for Vec<T> {
    fn read(s: &str) -> Self {
        s.split_whitespace().map(T::read).collect()
    }
}

impl<A: Read, B: Read> Read for (A, B) {
    fn read(s: &str) -> Self {
        let tokens: Vec<_> = s.split_whitespace().collect();
        (A::read(tokens[0]), B::read(tokens[1]))
    }
}

impl<A: Read, B: Read, C: Read> Read for (A, B, C) {
    fn read(s: &str) -> Self {
        let tokens: Vec<_> = s.split_whitespace().collect();
        (A::read(tokens[0]), B::read(tokens[1]), C::read(tokens[2]))
    }
}

impl<A: Read, B: Read, C: Read, D: Read> Read for (A, B, C, D) {
    fn read(s: &str) -> Self {
        let tokens: Vec<_> = s.split_whitespace().collect();
        (A::read(tokens[0]), B::read(tokens[1]), C::read(tokens[2]), D::read(tokens[3]))
    }
}