Problem p03460 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int dx[] = {0, 0, 1, -1};
constexpr int dy[] = {1, -1, 0, 0};

int sumW[100][100];
int sumB[100][100];

int Wsum(int i, int j, int k, int l) {
  int res = sumW[i][j];

  if (k > 0)
    res -= sumW[k - 1][j];

  if (l > 0)
    res -= sumW[i][l - 1];

  if (k > 0 && l > 0)
    res += sumW[k - 1][l - 1];

  return res;
}

int Bsum(int i, int j, int k, int l) {
  int res = sumB[i][j];

  if (k > 0)
    res -= sumB[k - 1][j];

  if (l > 0)
    res -= sumB[i][l - 1];

  if (k > 0 && l > 0)
    res += sumB[k - 1][l - 1];

  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k, x[112345], y[112345];
  char c[112345];

  cin >> n >> k;
  REP(i, n)
  cin >> x[i] >> y[i] >> c[i];

  int res = 0;

  memset(sumW, 0, sizeof(sumW));
  memset(sumB, 0, sizeof(sumB));

  REP(i, n) {
    if (c[i] == 'W') {
      sumW[x[i] % (k * 2)][y[i] % (k * 2)]++;
      sumW[x[i] % (k * 2)][y[i] % (k * 2) + k * 2]++;
      sumW[x[i] % (k * 2) + k * 2][y[i] % (k * 2)]++;
      sumW[x[i] % (k * 2) + k * 2][y[i] % (k * 2) + k * 2]++;
    }
    if (c[i] == 'B') {
      sumB[x[i] % (k * 2)][y[i] % (k * 2)]++;
      sumB[x[i] % (k * 2)][y[i] % (k * 2) + k * 2]++;
      sumB[x[i] % (k * 2) + k * 2][y[i] % (k * 2)]++;
      sumB[x[i] % (k * 2) + k * 2][y[i] % (k * 2) + k * 2]++;
    }
  }

  REP(i, k * 4) {
    for (int j = 1; j < k * 4; j++) {
      sumW[i][j] += sumW[i][j - 1];
      sumB[i][j] += sumB[i][j - 1];
    }
  }

  for (int i = 1; i < k * 4; i++) {
    REP(j, k * 4) {
      sumW[i][j] += sumW[i - 1][j];
      sumB[i][j] += sumB[i - 1][j];
    }
  }

  for (int i = 0; i < k * 2; i++) {
    for (int j = 0; j < k * 2; j++) {
      int r = 0;

      r += Wsum(i + k - 1, j + k - 1, i, j);
      r += Wsum(i + k * 2 - 1, j + k * 2 - 1, i + k, j + k);
      r += Bsum(i + k * 2 - 1, j + k - 1, i + k, j);
      r += Bsum(i + k - 1, j + k * 2 - 1, i, j + k);

      res = max(res, r);
    }
  }

  cout << res << endl;

  /*
  REP(i, k * 4)
  {
      REP(j, k * 4)
      cout << sumW[i][j] << " ";

      cout << endl;
  }
  cout << endl;
  REP(i, k * 4)
  {
      REP(j, k * 4)
      cout << sumB[i][j] << " ";

      cout << endl;
  }
  */

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int dx[] = {0, 0, 1, -1};
constexpr int dy[] = {1, -1, 0, 0};

int sumW[4100][4100];
int sumB[4100][4100];

int Wsum(int i, int j, int k, int l) {
  int res = sumW[i][j];

  if (k > 0)
    res -= sumW[k - 1][j];

  if (l > 0)
    res -= sumW[i][l - 1];

  if (k > 0 && l > 0)
    res += sumW[k - 1][l - 1];

  return res;
}

int Bsum(int i, int j, int k, int l) {
  int res = sumB[i][j];

  if (k > 0)
    res -= sumB[k - 1][j];

  if (l > 0)
    res -= sumB[i][l - 1];

  if (k > 0 && l > 0)
    res += sumB[k - 1][l - 1];

  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, k, x[112345], y[112345];
  char c[112345];

  cin >> n >> k;
  REP(i, n)
  cin >> x[i] >> y[i] >> c[i];

  int res = 0;

  memset(sumW, 0, sizeof(sumW));
  memset(sumB, 0, sizeof(sumB));

  REP(i, n) {
    if (c[i] == 'W') {
      sumW[x[i] % (k * 2)][y[i] % (k * 2)]++;
      sumW[x[i] % (k * 2)][y[i] % (k * 2) + k * 2]++;
      sumW[x[i] % (k * 2) + k * 2][y[i] % (k * 2)]++;
      sumW[x[i] % (k * 2) + k * 2][y[i] % (k * 2) + k * 2]++;
    }
    if (c[i] == 'B') {
      sumB[x[i] % (k * 2)][y[i] % (k * 2)]++;
      sumB[x[i] % (k * 2)][y[i] % (k * 2) + k * 2]++;
      sumB[x[i] % (k * 2) + k * 2][y[i] % (k * 2)]++;
      sumB[x[i] % (k * 2) + k * 2][y[i] % (k * 2) + k * 2]++;
    }
  }

  REP(i, k * 4) {
    for (int j = 1; j < k * 4; j++) {
      sumW[i][j] += sumW[i][j - 1];
      sumB[i][j] += sumB[i][j - 1];
    }
  }

  for (int i = 1; i < k * 4; i++) {
    REP(j, k * 4) {
      sumW[i][j] += sumW[i - 1][j];
      sumB[i][j] += sumB[i - 1][j];
    }
  }

  for (int i = 0; i < k * 2; i++) {
    for (int j = 0; j < k * 2; j++) {
      int r = 0;

      r += Wsum(i + k - 1, j + k - 1, i, j);
      r += Wsum(i + k * 2 - 1, j + k * 2 - 1, i + k, j + k);
      r += Bsum(i + k * 2 - 1, j + k - 1, i + k, j);
      r += Bsum(i + k - 1, j + k * 2 - 1, i, j + k);

      res = max(res, r);
    }
  }

  cout << res << endl;

  /*
  REP(i, k * 4)
  {
      REP(j, k * 4)
      cout << sumW[i][j] << " ";

      cout << endl;
  }
  cout << endl;
  REP(i, k * 4)
  {
      REP(j, k * 4)
      cout << sumB[i][j] << " ";

      cout << endl;
  }
  */

  return 0;
}