Problem p03460 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define con continue
#define bk break
#define LL long long
#define db double
#define pt cout << '\n'
#define ms(a, b) memset((a), (b), sizeof(a))
#define mp make_pair
#define endl '\n'
LL inf = 2147483647;
const long long ll = 9223372036854775807, ninf = 1000000000;
const double eps = 1e-6;
const long long nll = 1000000000000000000;
#define sz size()
#define len(X) strlen(X)
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ULL unsigned long long
#define de cout << "test" << endl;
#define st system("pause");
#define F first
#define S second
class MagicNumberThree {

public:
  int countSubsequences(string s) {}
};

int f[5005][5005] = {};
int sum[5005][5005] = {};
int main() {
  int a, b;
  while (cin >> a >> b) {
    int ans = 0;
    int t = 2 * b;
    for (int i = 0; i < b; i++) {
      int x, y;
      char z;
      cin >> x >> y >> z;
      if (z == 'W')
        x += b;
      x %= t;
      y %= t;

      f[x][y]++;
      f[x][y + b]--;
      f[x + b][y]--;
      f[x + b][y + b]++;

      f[x + b][y + b]++;
      f[x + 2 * b][y + b]--;
      f[x + b][y + 2 * b]--;
      f[x + 2 * b][y + 2 * b]++;
    }
    for (int i = 1; i < 4 * b; i++) {
      for (int j = 0; j < 4 * b; j++) {
        f[i][j] += f[i - 1][j];
      }
    }
    for (int i = 0; i < 4 * b; i++) {
      for (int j = 1; j < 4 * b; j++) {
        f[i][j] += f[i][j - 1];
      }
    }
    for (int i = 0; i < 4 * b; i++) {
      for (int j = 0; j < 4 * b; j++) {
        sum[i % t][j % t] += f[i][j];
      }
    }
    for (int i = 0; i < t; i++) {
      for (int j = 0; j < t; j++) {
        ans = max(ans, sum[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define con continue
#define bk break
#define LL long long
#define db double
#define pt cout << '\n'
#define ms(a, b) memset((a), (b), sizeof(a))
#define mp make_pair
#define endl '\n'
LL inf = 2147483647;
const long long ll = 9223372036854775807, ninf = 1000000000;
const double eps = 1e-6;
const long long nll = 1000000000000000000;
#define sz size()
#define len(X) strlen(X)
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ULL unsigned long long
#define de cout << "test" << endl;
#define st system("pause");
#define F first
#define S second
class MagicNumberThree {

public:
  int countSubsequences(string s) {}
};

int f[5005][5005] = {};
int sum[5005][5005] = {};
int main() {
  int a, b;
  while (cin >> a >> b) {
    int ans = 0;
    int t = 2 * b;
    for (int i = 0; i < a; i++) {
      int x, y;
      char z;
      cin >> x >> y >> z;
      if (z == 'W')
        x += b;
      x %= t;
      y %= t;

      f[x][y]++;
      f[x][y + b]--;
      f[x + b][y]--;
      f[x + b][y + b]++;

      f[x + b][y + b]++;
      f[x + 2 * b][y + b]--;
      f[x + b][y + 2 * b]--;
      f[x + 2 * b][y + 2 * b]++;
    }
    for (int i = 1; i < 4 * b; i++) {
      for (int j = 0; j < 4 * b; j++) {
        f[i][j] += f[i - 1][j];
      }
    }
    for (int i = 0; i < 4 * b; i++) {
      for (int j = 1; j < 4 * b; j++) {
        f[i][j] += f[i][j - 1];
      }
    }
    for (int i = 0; i < 4 * b; i++) {
      for (int j = 0; j < 4 * b; j++) {
        sum[i % t][j % t] += f[i][j];
      }
    }
    for (int i = 0; i < t; i++) {
      for (int j = 0; j < t; j++) {
        ans = max(ans, sum[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}