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 <bits/stdc++.h>
using namespace std;
struct P
{
    //string r;
    int x,y,z;

    bool operator<(const P &a)const{
       // if(y!=a.y)
       // return y>a.y;
       return x>a.x;
//        return z>a.z;
    }
};

vector<int> v;
//bitset<4001000> b;
int a,c,i,b,k,d,n,m,e;//dy[15]={0,1,0,-1,-1,1,-1,1},dx[15]={1,0,-1,0,1,1,-1,-1};//
int l[310100];
int o[4111][4300][3];
int dx[10]={0,1,0,-1},dy[10]={1,0,-1,0},dz[10]={0,0,0,0,1,-1};

long long x,y,z,mod=1000000007;
P u[1111222];
int j[1111][5552];

stack<int> s;
queue<P> q;
queue<int> q1;
bool qw[3333];
deque<int> de;
//'1'==49;
//'A'==65;
//'a'==97;
//unordered_
map<int,int > p,p1;
//list<int> l;
string r,r1,r2,r3,r4,r5;
//char r[333][333];

bool as(int a,int b)
{
    return a>b;
}

int f(int a,int b,int i)
{
    if(a>=0&&b>=0)
    return o[a][b][i];
    return 0;
}
//queue<P> q;

int main()
{
    scanf("%d %d",&a,&b);
    for(int t=1;t<=a;t++)
    {
        scanf("%d %d",&n,&m);
        cin>>r;
        if(r[0]=='B') i=0;
        else i=1;
        n%=2*b;
        m%=2*b;
        o[n][m][i]=1;
    }
    for(int t=0;t<4*b;t++)
        for(int w=0;w<4*b;w++)
        for(int h=0;h<2;h++)
        o[t][w][h]=o[t][w][h]+o[t-1][w][h]+o[t][w-1][h]-o[t-1][w-1][h];
    //for(int t=0;t<2*b;t++,puts(""))
    //    for(int w=0;w<2*b;w++)
    //    printf("%d ",o[t][w][0]);



    k=o[b-1][b-1][0]+o[b-1][2*b-1][1]-o[b-1][b-1][1]+o[2*b-1][b-1][1]-o[b-1][b-1][1]+o[2*b-1][2*b-1][0]-o[2*b-1][b-1][0]-o[b-1][2*b-1][0]+o[b-1][b-1][0];
    for(int n=0;n<=b;n++)
        for(int m=0;m<=b;m++)
    if(n%b||m%b)
    {
        int i=0;
        i=f(n-1,m-1,0);
        i+=f(n+b-1,m-1,1)-f(n-1,m-1,1);
        i+=f(2*b-1,m-1,0)-f(n+b-1,m-1,0);

        i+=f(n-1,m+b-1,1)-f(n-1,m-1,1);
        i+=f(n-1+b,m-1+b,0)-f(n-1,m+b-1,0)-f(n+b-1,m-1,0)+f(n-1,m-1,0);
        i+=f(2*b-1,m+b-1,1)-f(2*b-1,m-1,1)-f(n+b-1,m+b-1,1)+f(n+b-1,m-1,1);

        i+=f(n-1,2*b-1,0)-f(n-1,m+b-1,0);
        i+=f(n+b-1,2*b-1,1)-f(n-1,2*b-1,1)-f(n+b-1,m+b-1,1)+f(n-1,m+b-1,1);
        i+=f(2*b-1,2*b-1,0)-f(2*b-1,m+b-1,0)-f(n+b-1,2*b-1,0)+f(n+b-1,m+b-1,0);
        k=max(k,i);
    }
    printf("%d",k);
}