Problem p03458 (C++)

Buggy submission (status: Memory Limit Exceeded):
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
typedef long double ld;
#define all(a) (a).begin(), (a).end()
#define EPS (1e-13)
#define bit(n, k) ((n >> k) & 1)
const ll Mod = 1e9 + 7;
const ll mod = 998244353;
struct H {
  ll x, y;
  bool operator<(const H &b) const {
    if (x != b.x)
      return x < b.x;
    return y < b.y;
  }
  bool operator>(const H &b) const {
    if (x != b.x)
      return x > b.x;
    return y > b.y;
  }
  bool operator==(const H &b) const { return x == b.x && y == b.y; }
  bool operator!=(const H &b) const { return (*this) != b; }
};
struct P {
  ll pos, cost;
  bool operator<(const P &b) const { return cost < b.cost; }
  bool operator>(const P &b) const { return cost > b.cost; }
};
struct B {
  ll to, cost;
};
struct E {
  ll from, to, cost;
  bool operator<(const E &b) const { return cost < b.cost; }
  bool operator>(const E &b) const { return cost > b.cost; }
};
template <typename T, typename U> void chmin(T &a, U b) {
  if (a > b)
    a = b;
}
template <typename T, typename U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}
template <typename T> T max_0(T a) {
  if (a < 0)
    return 0;
  return a;
}
template <typename T> T min_0(T a) {
  if (a > 0)
    return 0;
  return a;
}
ll read() {
  ll u;
  ll k = scanf("%lld", &u);
  return u;
}
ll gcd(ll i, ll j) {
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcd(j % i, i);
}
ll mod_pow(ll x, ll n, ll p) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % p;
    x = x * x % p;
    n >>= 1;
  }
  return res;
} // x^n%p
ll bitcount(ll x) {
  int sum = 0;
  for (int i = 0; i < 20; i++)
    if ((1ll << i) & x)
      sum++;
  return sum;
}
/*ll fact_[200000];
ll comb(ll x, ll y, ll mod = Mod) {
        if (!fact_[0]) {
                fact_[0] = 1;
                for (int i = 1; i < 200000; i++) {
                        fact_[i] = fact_[i - 1] * i % mod;
                }
        }
        if (x < y) return 0;
        return mod_pow(fact_[x], mod, mod) *
                mod_pow(fact_[x - y], mod - 2, mod) % mod *
                mod_pow(fact_[y], mod - 2, mod) % mod;
}//xCy*/
const ll Inf = 3023372036854775807;
const int inf = 1500000000;
// #define int long long
//----------------------------------------------------
int n, k;
int b[5000][5000], w[5000][5000];
int bb[5000][5000], wb[5000][5000];
int wsum(int a, int b, int c, int d) {
  return wb[c][d] - wb[c][b] - wb[a][d] + wb[a][b];
}
int bsum(int a, int b, int c, int d) {
  return bb[c][d] - bb[c][b] - bb[a][d] + bb[a][b];
}
signed main() {
  cin >> n >> k;
  int k2 = 2 * k;
  for (int i = 0; i < n; i++) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    x %= k2;
    y %= k2;
    if (c == 'B') {
      b[x][y]++;
      b[x + k2][y]++;
      b[x][y + k2]++;
      b[x + k2][y + k2]++;
    } else {
      w[x][y]++;
      w[x + k2][y]++;
      w[x][y + k2]++;
      w[x + k2][y + k2]++;
    }
  }
  for (int i = 0; i < k2 * 2; i++) {
    for (int j = 0; j < k2 * 2; j++) {
      bb[i + 1][j + 1] += b[i][j] + bb[i + 1][j] + bb[i][j + 1] - bb[i][j];
      wb[i + 1][j + 1] += w[i][j] + wb[i + 1][j] + wb[i][j + 1] - wb[i][j];
    }
  }
  int ans = 0;
  for (int i = 0; i < k2; i++) {
    for (int j = 0; j < k2; j++) {
      int x = i + k, y = j + k;
      int sum = bsum(i, j, x, y) + bsum(x, y, x + k, y + k);
      sum += wsum(i + k, j, x + k, y) + wsum(i, j + k, x, y + k);
      ans = max(ans, sum);
    }
  }
  cout << ans << endl;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
typedef long double ld;
#define all(a) (a).begin(), (a).end()
#define EPS (1e-13)
#define bit(n, k) ((n >> k) & 1)
const ll Mod = 1e9 + 7;
const ll mod = 998244353;
struct H {
  ll x, y;
  bool operator<(const H &b) const {
    if (x != b.x)
      return x < b.x;
    return y < b.y;
  }
  bool operator>(const H &b) const {
    if (x != b.x)
      return x > b.x;
    return y > b.y;
  }
  bool operator==(const H &b) const { return x == b.x && y == b.y; }
  bool operator!=(const H &b) const { return (*this) != b; }
};
struct P {
  ll pos, cost;
  bool operator<(const P &b) const { return cost < b.cost; }
  bool operator>(const P &b) const { return cost > b.cost; }
};
struct B {
  ll to, cost;
};
struct E {
  ll from, to, cost;
  bool operator<(const E &b) const { return cost < b.cost; }
  bool operator>(const E &b) const { return cost > b.cost; }
};
template <typename T, typename U> void chmin(T &a, U b) {
  if (a > b)
    a = b;
}
template <typename T, typename U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}
template <typename T> T max_0(T a) {
  if (a < 0)
    return 0;
  return a;
}
template <typename T> T min_0(T a) {
  if (a > 0)
    return 0;
  return a;
}
ll read() {
  ll u;
  ll k = scanf("%lld", &u);
  return u;
}
ll gcd(ll i, ll j) {
  if (i > j)
    swap(i, j);
  if (i == 0)
    return j;
  return gcd(j % i, i);
}
ll mod_pow(ll x, ll n, ll p) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % p;
    x = x * x % p;
    n >>= 1;
  }
  return res;
} // x^n%p
ll bitcount(ll x) {
  int sum = 0;
  for (int i = 0; i < 20; i++)
    if ((1ll << i) & x)
      sum++;
  return sum;
}
/*ll fact_[200000];
ll comb(ll x, ll y, ll mod = Mod) {
        if (!fact_[0]) {
                fact_[0] = 1;
                for (int i = 1; i < 200000; i++) {
                        fact_[i] = fact_[i - 1] * i % mod;
                }
        }
        if (x < y) return 0;
        return mod_pow(fact_[x], mod, mod) *
                mod_pow(fact_[x - y], mod - 2, mod) % mod *
                mod_pow(fact_[y], mod - 2, mod) % mod;
}//xCy*/
const ll Inf = 3023372036854775807;
const int inf = 1500000000;
// #define int long long
//----------------------------------------------------
int n, k;
int b[4002][4002], w[4002][4002];
int bb[4002][4002], wb[4002][4002];
int wsum(int a, int b, int c, int d) {
  return wb[c][d] - wb[c][b] - wb[a][d] + wb[a][b];
}
int bsum(int a, int b, int c, int d) {
  return bb[c][d] - bb[c][b] - bb[a][d] + bb[a][b];
}
signed main() {
  cin >> n >> k;
  int k2 = 2 * k;
  for (int i = 0; i < n; i++) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    x %= k2;
    y %= k2;
    if (c == 'B') {
      b[x][y]++;
      b[x + k2][y]++;
      b[x][y + k2]++;
      b[x + k2][y + k2]++;
    } else {
      w[x][y]++;
      w[x + k2][y]++;
      w[x][y + k2]++;
      w[x + k2][y + k2]++;
    }
  }
  for (int i = 0; i < k2 * 2; i++) {
    for (int j = 0; j < k2 * 2; j++) {
      bb[i + 1][j + 1] += b[i][j] + bb[i + 1][j] + bb[i][j + 1] - bb[i][j];
      wb[i + 1][j + 1] += w[i][j] + wb[i + 1][j] + wb[i][j + 1] - wb[i][j];
    }
  }
  int ans = 0;
  for (int i = 0; i < k2; i++) {
    for (int j = 0; j < k2; j++) {
      int x = i + k, y = j + k;
      int sum = bsum(i, j, x, y) + bsum(x, y, x + k, y + k);
      sum += wsum(i + k, j, x + k, y) + wsum(i, j + k, x, y + k);
      ans = max(ans, sum);
    }
  }
  cout << ans << endl;
}