Problem p03458 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
// #define MOD 998244353
#define MEM_SIZE 1000100
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()

template <typename T> void DEBUG(T e) {
  if (DEBUG_OUT == false)
    return;
  std::cout << e << " ";
}
template <typename T> void DEBUG(const std::vector<T> &v) {
  if (DEBUG_OUT == false)
    return;
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) {
  if (DEBUG_OUT == false)
    return;
  for (const auto &v : vv) {
    DEBUG(v);
  }
}
template <class T, class... Ts> void DEBUG(T d, Ts... e) {
  if (DEBUG_OUT == false)
    return;
  DEBUG(d);
  DEBUG(e...);
}
template <class T> void corner(bool flg, T hoge) {
  if (flg) {
    cout << hoge << endl;
    abort();
  }
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

int mat_sum(vector<vector<int>> &mat, int x1, int y1, int x2, int y2) {
  return mat[y2][x2] - mat[y2][x1] - mat[y1][x2] + mat[y1][x1];
}
void solve(void) {
  // https://atcoder.jp/contests/abc086/submissions/2001424
  vector<vector<int>> bb(4010, vector<int>(4010, 0));
  vector<vector<int>> ww(4010, vector<int>(4010, 0));
  vector<vector<int>> bbcc(4010, vector<int>(4010, 0));
  vector<vector<int>> wwcc(4010, vector<int>(4010, 0));
  int n, k;
  cin >> n >> k;
  int k2 = k * 2;
  for (int i = 0; i < n; i++) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    x %= k2;
    y %= k2;
    if (c == 'B') {
      bb[y][x]++;
      bb[y + k2][x]++;
      bb[y][x + k2]++;
      bb[y + k2][x + k2]++;
    } else {
      ww[y][x]++;
      ww[y + k2][x]++;
      ww[y][x + k2]++;
      ww[y + k2][x + k2]++;
    }
  }
  for (int y = 0; y < k2 * 2; y++) {
    for (int x = 0; x < k2 * 2; x++) {
      bbcc[y + 1][x + 1] +=
          bb[y][x] + bbcc[y + 1][x] + bbcc[y][x + 1] - bbcc[y][x];
      wwcc[y + 1][x + 1] +=
          ww[y][x] + wwcc[y + 1][x] + wwcc[y][x + 1] - wwcc[y][x];
    }
  }
  int res = 0;
  for (int x1 = 0; x1 < k2; x1++) {
    for (int y1 = 0; y1 < k2; y1++) {
      int x2 = x1 + k;
      int y2 = y1 + k;
      int sum = mat_sum(bbcc, x1, y1, x2, y2) +
                mat_sum(bbcc, x1 + k, y1 + k, x2 + k, y2 + k);
      sum +=
          mat_sum(wwcc, x2, y1, x2 + k, y2) + mat_sum(wwcc, x1, y2, x2, y2 + k);
      chmax(res, sum);
    }
  }
  cout << res << endl;

  return;
}

int32_t main(int32_t argc, const char *argv[]) {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(11);
  solve();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// #define int ll
// #define ll long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
// #define MOD 998244353
#define MEM_SIZE 1000100
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()

template <typename T> void DEBUG(T e) {
  if (DEBUG_OUT == false)
    return;
  std::cout << e << " ";
}
template <typename T> void DEBUG(const std::vector<T> &v) {
  if (DEBUG_OUT == false)
    return;
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void DEBUG(const std::vector<std::vector<T>> &vv) {
  if (DEBUG_OUT == false)
    return;
  for (const auto &v : vv) {
    DEBUG(v);
  }
}
template <class T, class... Ts> void DEBUG(T d, Ts... e) {
  if (DEBUG_OUT == false)
    return;
  DEBUG(d);
  DEBUG(e...);
}
template <class T> void corner(bool flg, T hoge) {
  if (flg) {
    cout << hoge << endl;
    abort();
  }
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

int mat_sum(vector<vector<int>> &mat, int x1, int y1, int x2, int y2) {
  return mat[y2][x2] - mat[y2][x1] - mat[y1][x2] + mat[y1][x1];
}
void solve(void) {
  // https://atcoder.jp/contests/abc086/submissions/2001424
  vector<vector<int>> bb(4010, vector<int>(4010, 0));
  vector<vector<int>> ww(4010, vector<int>(4010, 0));
  vector<vector<int>> bbcc(4010, vector<int>(4010, 0));
  vector<vector<int>> wwcc(4010, vector<int>(4010, 0));
  int n, k;
  cin >> n >> k;
  int k2 = k * 2;
  for (int i = 0; i < n; i++) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    x %= k2;
    y %= k2;
    if (c == 'B') {
      bb[y][x]++;
      bb[y + k2][x]++;
      bb[y][x + k2]++;
      bb[y + k2][x + k2]++;
    } else {
      ww[y][x]++;
      ww[y + k2][x]++;
      ww[y][x + k2]++;
      ww[y + k2][x + k2]++;
    }
  }
  for (int y = 0; y < k2 * 2; y++) {
    for (int x = 0; x < k2 * 2; x++) {
      bbcc[y + 1][x + 1] +=
          bb[y][x] + bbcc[y + 1][x] + bbcc[y][x + 1] - bbcc[y][x];
      wwcc[y + 1][x + 1] +=
          ww[y][x] + wwcc[y + 1][x] + wwcc[y][x + 1] - wwcc[y][x];
    }
  }
  int res = 0;
  for (int x1 = 0; x1 < k2; x1++) {
    for (int y1 = 0; y1 < k2; y1++) {
      int x2 = x1 + k;
      int y2 = y1 + k;
      int sum = mat_sum(bbcc, x1, y1, x2, y2) +
                mat_sum(bbcc, x1 + k, y1 + k, x2 + k, y2 + k);
      sum +=
          mat_sum(wwcc, x2, y1, x2 + k, y2) + mat_sum(wwcc, x1, y2, x2, y2 + k);
      chmax(res, sum);
    }
  }
  cout << res << endl;

  return;
}

int32_t main(int32_t argc, const char *argv[]) {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(11);
  solve();

  return 0;
}