Problem p03460 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<P, ll> PPI;
typedef pair<ll, P> PIP;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vp;
#define PQ(T) priority_queue<T, vector<T>, greater<T>>
#define PQ2(T) priority_queue<T>
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL << 29;
const ll mod = 1e9 + 7;
#define REP(i, a, b) for (ll(i) = a; (i) < (ll)(b); ++(i))
#define rep(i, n) REP(i, 0, n)
#define rep1(i, n) REP(i, 1, n + 1)
#define repd(i, n, d) for (ll(i) = 0; (i) < (ll)(n); (i) += (d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset((m), (v), sizeof(m))
#define chmin(x, y) ((x) = min((x), (y)))
#define chmax(x, y) ((x) = max((x), (y)))
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)), (x).end())
#define DEBUG(x)                                                               \
  cerr << "line (" << __LINE__ << ")  " << #x << ": " << x << endl;
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  rep(i, n) os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}

#define N 4020
ll d[2][N][N], e[2][N][N];
int n, K;

ll f(ll i, ll j, ll k) {
  return e[i][j + K / 2][k + K / 2] - e[i][j + K / 2][k] - e[i][j][k + K / 2] +
         e[i][j][k];
}

int main() {
  scanf("%d %d", &n, &K);
  K *= 2;
  rep(i, n) {
    int x, y;
    char c;
    scanf("%d %d %c", &x, &y, &c);
    d[c == 'W'][x % K][y % K]++;
  }
  rep(i, 2) rep(j, K * 2 + 10) rep(k, K * 2 + 10) {
    e[i][j + 1][k + 1] =
        e[i][j + 1][k] + e[i][j][k + 1] - e[i][j][k] + d[i][j % K][k % K];
  }
  ll res = 0;
  rep(j, K + 10) rep(k, K + 10) {
    ll sum = f(0, j, k) + f(0, j + K / 2, k + K / 2) + f(1, j + K / 2, k) +
             f(1, j, k + K / 2);
    chmax(res, sum);
  }
  cout << res << endl;
  /*
  rep(i, 2){
          rep(j, K*2){
                  rep(k, K*2) cerr<<e[i][j][k]<<" ";
                  cerr<<endl;
          }
          cerr<<endl;cerr<<endl;
  }
  rep(i, 2){
          rep(j, K){
                  rep(k, K) cerr<<d[i][j][k]<<" ";
                  cerr<<endl;
          }
          cerr<<endl;cerr<<endl;
  }
  */
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef pair<P, ll> PPI;
typedef pair<ll, P> PIP;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vp;
#define PQ(T) priority_queue<T, vector<T>, greater<T>>
#define PQ2(T) priority_queue<T>
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL << 29;
const ll mod = 1e9 + 7;
#define REP(i, a, b) for (ll(i) = a; (i) < (ll)(b); ++(i))
#define rep(i, n) REP(i, 0, n)
#define rep1(i, n) REP(i, 1, n + 1)
#define repd(i, n, d) for (ll(i) = 0; (i) < (ll)(n); (i) += (d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))
#define mset(m, v) memset((m), (v), sizeof(m))
#define chmin(x, y) ((x) = min((x), (y)))
#define chmax(x, y) ((x) = max((x), (y)))
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)), (x).end())
#define DEBUG(x)                                                               \
  cerr << "line (" << __LINE__ << ")  " << #x << ": " << x << endl;
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  int n = v.size();
  rep(i, n) os << v[i] << (i == n - 1 ? "" : " ");
  return os;
}

#define N 4020
int d[2][N][N], e[2][N][N];
int n, K;

ll f(ll i, ll j, ll k) {
  return e[i][j + K / 2][k + K / 2] - e[i][j + K / 2][k] - e[i][j][k + K / 2] +
         e[i][j][k];
}

int main() {
  scanf("%d %d", &n, &K);
  K *= 2;
  rep(i, n) {
    int x, y;
    char c;
    scanf("%d %d %c", &x, &y, &c);
    d[c == 'W'][x % K][y % K]++;
  }
  rep(i, 2) rep(j, K * 2 + 10) rep(k, K * 2 + 10) {
    e[i][j + 1][k + 1] =
        e[i][j + 1][k] + e[i][j][k + 1] - e[i][j][k] + d[i][j % K][k % K];
  }
  ll res = 0;
  rep(j, K + 10) rep(k, K + 10) {
    ll sum = f(0, j, k) + f(0, j + K / 2, k + K / 2) + f(1, j + K / 2, k) +
             f(1, j, k + K / 2);
    chmax(res, sum);
  }
  cout << res << endl;
  /*
  rep(i, 2){
          rep(j, K*2){
                  rep(k, K*2) cerr<<e[i][j][k]<<" ";
                  cerr<<endl;
          }
          cerr<<endl;cerr<<endl;
  }
  rep(i, 2){
          rep(j, K){
                  rep(k, K) cerr<<d[i][j][k]<<" ";
                  cerr<<endl;
          }
          cerr<<endl;cerr<<endl;
  }
  */
  return 0;
}