Problem p03458 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using LL = int64_t;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 5;
LL sum[4005][4005][2] = {0}, node[4005][4005][2] = {0};

LL sum1(int a, int b, int c, int d) {
  return sum[a][b][0] + sum[c][d][0] - sum[a][d][0] - sum[c][b][0];
}
LL sum2(int a, int b, int c, int d) {
  return sum[a][b][1] + sum[c][d][1] - sum[a][d][1] - sum[c][b][1];
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  LL n, k, cnt = 1;
  cin >> n >> k;
  k *= 2;
  LL x, y;
  char c;
  for (int i = 1; i <= n; i++) {
    cin >> x >> y >> c;
    x %= k;
    y %= k;
    if (c == 'B') {
      node[x][y][0]++;
      node[x + k][y][0]++;
      node[x][y + k][0]++;
      node[x + k][y + k][0]++;
    } else {
      node[x][y][1]++;
      node[x + k][y][1]++;
      node[x][y + k][1]++;
      node[x + k][y + k][1]++;
    }
  }
  for (int i = 1; i <= 2 * k; i++) {
    for (int j = 1; j <= 2 * k; j++) {
      sum[i][j][0] = sum[i - 1][j][0] + sum[i][j - 1][0] -
                     sum[i - 1][j - 1][0] + node[i][j][0];
      sum[i][j][1] = sum[i - 1][j][1] + sum[i][j - 1][1] -
                     sum[i - 1][j - 1][1] + node[i][j][1];
    }
  }
  LL ans = 0;
  for (int x = 0; x <= k; x++) {
    for (int y = 0; y <= k; y++) {
      LL temp = sum1(x, y, x + k / 2, y + k / 2) +
                sum1(x + k / 2, y + k / 2, x + k, y + k) +
                sum2(x + k / 2, y, x + k, y + k / 2) +
                sum2(x, y + k / 2, x + k / 2, y + k);
      ans = max(ans, temp);
      // cout<<ans<<endl;
    }
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using LL = int64_t;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 5;
int sum[4005][4005][2] = {0}, node[4005][4005][2] = {0};

LL sum1(int a, int b, int c, int d) {
  return sum[a][b][0] + sum[c][d][0] - sum[a][d][0] - sum[c][b][0];
}
LL sum2(int a, int b, int c, int d) {
  return sum[a][b][1] + sum[c][d][1] - sum[a][d][1] - sum[c][b][1];
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  LL n, k, cnt = 1;
  cin >> n >> k;
  k *= 2;
  LL x, y;
  char c;
  for (int i = 1; i <= n; i++) {
    cin >> x >> y >> c;
    x %= k;
    y %= k;
    if (c == 'B') {
      node[x][y][0]++;
      node[x + k][y][0]++;
      node[x][y + k][0]++;
      node[x + k][y + k][0]++;
    } else {
      node[x][y][1]++;
      node[x + k][y][1]++;
      node[x][y + k][1]++;
      node[x + k][y + k][1]++;
    }
  }
  for (int i = 1; i <= 2 * k; i++) {
    for (int j = 1; j <= 2 * k; j++) {
      sum[i][j][0] = sum[i - 1][j][0] + sum[i][j - 1][0] -
                     sum[i - 1][j - 1][0] + node[i][j][0];
      sum[i][j][1] = sum[i - 1][j][1] + sum[i][j - 1][1] -
                     sum[i - 1][j - 1][1] + node[i][j][1];
    }
  }
  LL ans = 0;
  for (int x = 0; x <= k; x++) {
    for (int y = 0; y <= k; y++) {
      LL temp = sum1(x, y, x + k / 2, y + k / 2) +
                sum1(x + k / 2, y + k / 2, x + k, y + k) +
                sum2(x + k / 2, y, x + k, y + k / 2) +
                sum2(x, y + k / 2, x + k / 2, y + k);
      ans = max(ans, temp);
      // cout<<ans<<endl;
    }
  }
  cout << ans;
  return 0;
}