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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

typedef pair< int, int > point;
typedef pair< point, point > rect;

rect new_rect(int x1, int y1, int x2, int y2)
{
  point p1 = point(x1, y1);
  point p2 = point(x2, y2);
  return rect(p1, p2);
}

vector< rect > points(int cx, int cy, int K)
{
  vector< rect > res;
  int x1 = max(0, cx - K);
  int y1 = max(0, cy - K);
  int x2 = cx;
  int y2 = cy;
  res.push_back(new_rect(x1, y1, x2, y2));

  x2 = min(2 * K, cx + K), y2 = min(2 * K, cy + K);
  x1 = cx, y1 = cy;
  res.push_back(new_rect(x1, y1, x2, y2));

  if (K < cy)
  {
    x2 = min(2 * K, cx + K), y2 = cy - K;
    x1 = cx, y1 = max(0, y2 - K);
    res.push_back(new_rect(x1, y1, x2, y2));
  }

  if (cx < K)
  {
    x2 = min(2 * K, cx + 2 * K), y2 = cy;
    x1 = cx + K, y1 = max(0, cy - K);
    res.push_back(new_rect(x1, y1, x2, y2));
  }

  if (K < cx)
  {
    x2 = cx - K, y2 = min(2 * K, cy + K);
    x1 = max(0, x2 - K), y1 = cy;
    res.push_back(new_rect(x1, y1, x2, y2));
  }

  if (K < cx && K < cy)
  {
    x2 = cx - K, y2 = cy - K;
    x1 = max(0, x2 - K), y1 = max(0, y2 - K);
    res.push_back(new_rect(x1, y1, x2, y2));
  }

  return res;
}

int white[2001][2001];
int checker(int N, int K, vector< pair< point, char > > C)
{
  memset(white, 0, sizeof(white));

  for (pair< point, char > pc : C)
  {
    point p = pc.first;
    int x = p.first, y = p.second;
    char c = pc.second;

    if (c == 'B')
      x += K;

    int tx = (x % (2 * K)) + 1;
    int ty = (x % (2 * K)) + 1;
    white[tx][ty] += 1;
  }

  for (int ky = 0; ky < 2 * K; ky++)
    for (int kx = 0; kx < 2 * K; kx++)
      white[ky + 1][kx + 1] += white[ky + 1][kx];

  for (int ky = 0; ky < 2 * K; ky++)
    for (int kx = 0; kx < 2 * K; kx++)
      white[ky + 1][kx + 1] += white[ky][kx + 1];

  int max_count = 0;
  for (int ky = K; ky < 2 * K; ky++)
  {
    for (int kx = 0; kx < 2 * K; kx++)
    {
      int count = 0;
      for (rect r : points(kx, ky, K))
      {
        int x1 = r.first.first;
        int y1 = r.first.second;
        int x2 = r.second.first;
        int y2 = r.second.second;

        count += white[y2][x2] - white[y2][x1] - white[y1][x2] + white[y1][x1];
      }
      max_count = max(max_count, count);
      if (max_count == N)
        break;
    }
  }
  return max_count;
}

int main(int argc, char** argv)
{
  int N, K;
  cin >> N >> K;
  vector< pair< point, char > > C;
  for (int n = 0; n < N; n++)
  {
    int x, y;
    char c;
    cin >> x >> y >> c;
    C.push_back(pair< point, char >(point(x, y), c));
  }
  int ans = checker(N, K, C);

  cout << ans << endl;

  return 0;
}