Problem p03460 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int MAXK = 5000;

int K;
int h[2][MAXK][MAXK];
int v[2][MAXK][MAXK];
int sq[2][MAXK][MAXK];
int w[MAXK][MAXK];
int b[MAXK][MAXK];

int get_h(int i, int j, int c) {
  i = (i + 8 * K) % (2 * K);
  j = (j + 8 * K) % (2 * K);
  int rc = (j + K - 1) % (2 * K);
  int lc = (j + 2 * K - 1) % (2 * K);
  if (rc > lc)
    return h[c][i][rc] - h[c][i][lc];
  return h[c][i][rc] + (h[c][i][2 * K - 1] - h[c][i][lc]);
}

int get_v(int i, int j, int c) {
  i = (i + 8 * K) % (2 * K);
  j = (j + 8 * K) % (2 * K);
  int rr = (i + K - 1) % (2 * K);
  int lr = (i + 2 * K - 1) % (2 * K);
  if (rr > lr)
    return v[c][rr][j] - v[c][lr][j];
  return v[c][rr][j] + (v[c][2 * K - 1][j] - v[c][lr][j]);
}

int main() {
  // freopen(".in", "r", stdin);
  int N;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    if (c == 'W')
      w[x % (2 * K)][y % (2 * K)]++;
    else
      b[x % (2 * K)][y % (2 * K)]++;
  }

  for (int i = 0; i < 2 * K; i++) {
    h[0][i][0] = w[i][0];
    h[1][i][0] = b[i][0];
    v[0][0][i] = w[0][i];
    v[1][0][i] = b[0][i];
    for (int j = 1; j < 2 * K; j++) {
      h[0][i][j] = h[0][i][j - 1] + w[i][j];
      h[1][i][j] = h[1][i][j - 1] + b[i][j];
      v[0][j][i] = v[0][j - 1][i] + w[j][i];
      v[1][j][i] = v[1][j - 1][i] + b[j][i];
    }
  }

  for (int i = 0; i < 2 * K; i++) {
    if (i == 0) {
      for (int q = 0; q < K; q++) {
        sq[0][0][0] += get_h(q, 0, 0);
        sq[1][0][0] += get_h(q, 0, 1);
      }
    } else {
      for (int c = 0; c < 2; c++)
        sq[c][i][0] =
            sq[c][i - 1][0] + get_h(i + K - 1, 0, c) - get_h(i - 1, 0, c);
    }

    for (int j = 1; j < 2 * K; j++) {
      for (int c = 0; c < 2; c++)
        sq[c][i][j] =
            sq[c][i][j - 1] + get_v(i, j + K - 1, c) - get_v(i, j - 1, c);
    }
  }

  int ans = 0;
  for (int i = 0; i < 2 * K; i++) {
    for (int j = 0; j < 2 * K; j++) {
      int res = sq[0][i][j] + sq[0][(i + K) % (2 * K)][(j + K) % (2 * K)] +
                sq[1][i][(j + K) % (2 * K)] + sq[1][(i + K) % (2 * K)][j];

      ans = max(ans, res);
    }
  }

  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int MAXK = 2005;

int K;
int h[2][MAXK][MAXK];
int v[2][MAXK][MAXK];
int sq[2][MAXK][MAXK];
int w[MAXK][MAXK];
int b[MAXK][MAXK];

int get_h(int i, int j, int c) {
  i = (i + 8 * K) % (2 * K);
  j = (j + 8 * K) % (2 * K);
  int rc = (j + K - 1) % (2 * K);
  int lc = (j + 2 * K - 1) % (2 * K);
  if (rc > lc)
    return h[c][i][rc] - h[c][i][lc];
  return h[c][i][rc] + (h[c][i][2 * K - 1] - h[c][i][lc]);
}

int get_v(int i, int j, int c) {
  i = (i + 8 * K) % (2 * K);
  j = (j + 8 * K) % (2 * K);
  int rr = (i + K - 1) % (2 * K);
  int lr = (i + 2 * K - 1) % (2 * K);
  if (rr > lr)
    return v[c][rr][j] - v[c][lr][j];
  return v[c][rr][j] + (v[c][2 * K - 1][j] - v[c][lr][j]);
}

int main() {
  // freopen(".in", "r", stdin);
  int N;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    if (c == 'W')
      w[x % (2 * K)][y % (2 * K)]++;
    else
      b[x % (2 * K)][y % (2 * K)]++;
  }

  for (int i = 0; i < 2 * K; i++) {
    h[0][i][0] = w[i][0];
    h[1][i][0] = b[i][0];
    v[0][0][i] = w[0][i];
    v[1][0][i] = b[0][i];
    for (int j = 1; j < 2 * K; j++) {
      h[0][i][j] = h[0][i][j - 1] + w[i][j];
      h[1][i][j] = h[1][i][j - 1] + b[i][j];
      v[0][j][i] = v[0][j - 1][i] + w[j][i];
      v[1][j][i] = v[1][j - 1][i] + b[j][i];
    }
  }

  for (int i = 0; i < 2 * K; i++) {
    if (i == 0) {
      for (int q = 0; q < K; q++) {
        sq[0][0][0] += get_h(q, 0, 0);
        sq[1][0][0] += get_h(q, 0, 1);
      }
    } else {
      for (int c = 0; c < 2; c++)
        sq[c][i][0] =
            sq[c][i - 1][0] + get_h(i + K - 1, 0, c) - get_h(i - 1, 0, c);
    }

    for (int j = 1; j < 2 * K; j++) {
      for (int c = 0; c < 2; c++)
        sq[c][i][j] =
            sq[c][i][j - 1] + get_v(i, j + K - 1, c) - get_v(i, j - 1, c);
    }
  }

  int ans = 0;
  for (int i = 0; i < 2 * K; i++) {
    for (int j = 0; j < 2 * K; j++) {
      int res = sq[0][i][j] + sq[0][(i + K) % (2 * K)][(j + K) % (2 * K)] +
                sq[1][i][(j + K) % (2 * K)] + sq[1][(i + K) % (2 * K)][j];

      ans = max(ans, res);
    }
  }

  cout << ans;
  return 0;
}