Problem p03458 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int n, k;
int posb[4001][4001] = {};

int calc(int i, int j) {
  int a, b, c, d;
  if (i <= -1 * k || j <= -1 * k) {
    a = 0;
    b = 0;
    c = 0;
    d = 0;
  } else {
    d = posb[i + k - 1][j + k - 1];
    if (i > 0 && j > 0) {
      a = posb[i - 1][j - 1];
      b = posb[i - 1][j + k - 1];
      c = posb[i + k - 1][j - 1];
    } else {
      a = 0;
      if (i <= 0 && j <= 0) {
        b = 0;
        c = 0;
      } else {
        if (j > 0) {
          b = 0;
          c = posb[i + k - 1][j - 1];
        } else {
          b = posb[i - 1][j + k - 1];
          c = 0;
        }
      }
    }
  }
  return d + a - b - c;
}

int main() {
  cin >> n >> k;
  int x[10000];
  int y[10000];
  for (int i = 0; i < n; i++) {
    int a, b;
    char c;
    cin >> a >> b >> c;
    if (c == 'B') {
      x[i] = a % (2 * k);
      y[i] = b % (2 * k);
    } else {
      x[i] = (a) % (2 * k);
      y[i] = (b + k) % (2 * k);
    }
  }
  for (int i = 0; i < n; i++) {
    posb[x[i]][y[i]]++;
    posb[x[i] + 2 * k][y[i] + 2 * k]++;
    posb[x[i] + 2 * k][y[i]]++;
    posb[x[i]][y[i] + 2 * k]++;
  }
  for (int i = 0; i < 4 * k; i++) {
    for (int j = 0; j < 4 * k - 1; j++) {
      posb[j + 1][i] += posb[j][i];
    }
  }

  for (int i = 0; i < 4 * k; i++) {
    for (int j = 0; j < 4 * k - 1; j++) {
      posb[i][j + 1] += posb[i][j];
    }
  }

  int ans = 0;
  for (int i = 0; i < 2 * k; i++) {
    for (int j = 0; j < 2 * k; j++) {
      int tmp =
          calc(i, j) + calc(i + k, j + k); // + calc(i - k, j + k) + calc(i + k,
                                           // j - k) + calc(i - k, j - k);
      if (ans < tmp)
        ans = tmp;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int n, k;
int posb[4001][4001] = {};

int calc(int i, int j) {
  int a, b, c, d;
  if (i <= -1 * k || j <= -1 * k) {
    a = 0;
    b = 0;
    c = 0;
    d = 0;
  } else {
    d = posb[i + k - 1][j + k - 1];
    if (i > 0 && j > 0) {
      a = posb[i - 1][j - 1];
      b = posb[i - 1][j + k - 1];
      c = posb[i + k - 1][j - 1];
    } else {
      a = 0;
      if (i <= 0 && j <= 0) {
        b = 0;
        c = 0;
      } else {
        if (j > 0) {
          b = 0;
          c = posb[i + k - 1][j - 1];
        } else {
          b = posb[i - 1][j + k - 1];
          c = 0;
        }
      }
    }
  }
  return d + a - b - c;
}

int main() {
  cin >> n >> k;
  int x[100000];
  int y[100000];
  for (int i = 0; i < n; i++) {
    int a, b;
    char c;
    cin >> a >> b >> c;
    if (c == 'B') {
      x[i] = a % (2 * k);
      y[i] = b % (2 * k);
    } else {
      x[i] = (a) % (2 * k);
      y[i] = (b + k) % (2 * k);
    }
  }
  for (int i = 0; i < n; i++) {
    posb[x[i]][y[i]]++;
    posb[x[i] + 2 * k][y[i] + 2 * k]++;
    posb[x[i] + 2 * k][y[i]]++;
    posb[x[i]][y[i] + 2 * k]++;
  }
  for (int i = 0; i < 4 * k; i++) {
    for (int j = 0; j < 4 * k - 1; j++) {
      posb[j + 1][i] += posb[j][i];
    }
  }

  for (int i = 0; i < 4 * k; i++) {
    for (int j = 0; j < 4 * k - 1; j++) {
      posb[i][j + 1] += posb[i][j];
    }
  }

  int ans = 0;
  for (int i = 0; i < 2 * k; i++) {
    for (int j = 0; j < 2 * k; j++) {
      int tmp =
          calc(i, j) + calc(i + k, j + k); // + calc(i - k, j + k) + calc(i + k,
                                           // j - k) + calc(i - k, j - k);
      if (ans < tmp)
        ans = tmp;
    }
  }
  cout << ans << endl;
}