Problem p03458 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
#define repi(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, a) repi(i, 0, a)
#define all(u) u.begin(), u.end()
#define pl pair<ll, ll>
#define fi first
#define se second
#define inf 1000000007

ll N, K;
vector<ll> x(100010, 0);
vector<ll> y(100010, 0);
vector<char> c(100010);
vector<vector<ll>> sum(6010, vector<ll>(6010, 0));

ll getsum(ll x1, ll x2, ll y1, ll y2) {
  if (x1 < 0)
    x1 = 0;
  if (y1 < 0)
    y1 = 0;
  if (x2 >= 2 * K)
    x2 = 2 * K - 1;
  if (y2 >= 2 * K)
    y2 = 2 * K - 1;
  if (x1 >= 2 * K || y1 >= 2 * K)
    return 0;
  if (x2 < 0 || y2 < 0)
    return 0;
  if (x1 > 0 && y1 > 0)
    return sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] +
           sum[x1 - 1][y1 - 1];
  if (x1 > 0)
    return sum[x2][y2] - sum[x1 - 1][y2];
  if (y1 > 0)
    return sum[x2][y2] - sum[x2][y1 - 1];
  else
    return sum[x2][y2];
}

int main() {
  cin >> N >> K;
  rep(i, N) { cin >> x[i] >> y[i] >> c[i]; }
  rep(i, N) {
    if (c[i] == 'W') {
      y[i] += K;
    }
    x[i] = x[i] % (K * 2);
    y[i] = y[i] % (K * 2);
    sum[x[i]][y[i]]++;
  }

  rep(i, 2010) {
    rep(j, 2010) {
      if (i > 0 && j > 0)
        sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
      else if (i > 0)
        sum[i][j] += sum[i - 1][j];
      else if (j > 0)
        sum[i][j] += sum[i][j - 1];
    }
  }

  ll ans = 0;
  rep(i, 2 * K) {
    rep(j, 2 * K) {
      ll temp = 0;
      temp += getsum(i - 3 * K + 1, i - 2 * K, j - K + 1, j) +
              getsum(i - K + 1, i, j - K + 1, j) +
              getsum(i + K + 1, i + 2 * K, j - K + 1, j);
      temp += getsum(i - 3 * K + 1, i - 2 * K, j + K + 1, j + 2 * K) +
              getsum(i - K + 1, i, j + K + 1, j + 2 * K) +
              getsum(i + K + 1, i + 2 * K, j + K + 1, j + 2 * K);
      temp += getsum(i - 3 * K + 1, i - 2 * K, j - 3 * K + 1, j - 2 * K) +
              getsum(i - K + 1, i, j - 3 * K + 1, j - 2 * K) +
              getsum(i + K + 1, i + 2 * K, j - 3 * K + 1, j - 2 * K);
      temp += getsum(i - 2 * K + 1, i - K, j - 2 * K + 1, j - K) +
              getsum(i + 1, i + K, j - 2 * K + 1, j - K) +
              getsum(i + 2 * K + 1, i + 3 * K, j - 2 * K + 1, j - K);
      temp += getsum(i - 2 * K + 1, i - K, j + 1, j + K) +
              getsum(i + 1, i + K, j + 1, j + K) +
              getsum(i + 2 * K + 1, i + 3 * K, j + 1, j + K);
      temp += getsum(i - 2 * K + 1, i - K, j + 2 * K + 1, j + 3 * K) +
              getsum(i + 1, i + K, j + 2 * K + 1, j + 3 * K) +
              getsum(i + 2 * K + 1, i + 3 * K, j + 2 * K + 1, j + 3 * K);
      ans = max(ans, temp);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long int ll;
#define repi(i, a, b) for (ll i = a; i < b; i++)
#define rep(i, a) repi(i, 0, a)
#define all(u) u.begin(), u.end()
#define pl pair<ll, ll>
#define fi first
#define se second
#define inf 1000000007

ll N, K;
vector<ll> x(100010, 0);
vector<ll> y(100010, 0);
vector<char> c(100010);
vector<vector<ll>> sum(2010, vector<ll>(2010, 0));

ll getsum(ll x1, ll x2, ll y1, ll y2) {
  if (x1 < 0)
    x1 = 0;
  if (y1 < 0)
    y1 = 0;
  if (x2 >= 2 * K)
    x2 = 2 * K - 1;
  if (y2 >= 2 * K)
    y2 = 2 * K - 1;
  if (x1 >= 2 * K || y1 >= 2 * K)
    return 0;
  if (x2 < 0 || y2 < 0)
    return 0;
  if (x1 > 0 && y1 > 0)
    return sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] +
           sum[x1 - 1][y1 - 1];
  if (x1 > 0)
    return sum[x2][y2] - sum[x1 - 1][y2];
  if (y1 > 0)
    return sum[x2][y2] - sum[x2][y1 - 1];
  else
    return sum[x2][y2];
}

int main() {
  cin >> N >> K;
  rep(i, N) { cin >> x[i] >> y[i] >> c[i]; }
  rep(i, N) {
    if (c[i] == 'W') {
      y[i] += K;
    }
    x[i] = x[i] % (K * 2);
    y[i] = y[i] % (K * 2);
    sum[x[i]][y[i]]++;
  }

  rep(i, 2010) {
    rep(j, 2010) {
      if (i > 0 && j > 0)
        sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
      else if (i > 0)
        sum[i][j] += sum[i - 1][j];
      else if (j > 0)
        sum[i][j] += sum[i][j - 1];
    }
  }

  ll ans = 0;
  rep(i, 2 * K) {
    rep(j, 2 * K) {
      ll temp = 0;
      temp += getsum(i - 3 * K + 1, i - 2 * K, j - K + 1, j) +
              getsum(i - K + 1, i, j - K + 1, j) +
              getsum(i + K + 1, i + 2 * K, j - K + 1, j);
      temp += getsum(i - 3 * K + 1, i - 2 * K, j + K + 1, j + 2 * K) +
              getsum(i - K + 1, i, j + K + 1, j + 2 * K) +
              getsum(i + K + 1, i + 2 * K, j + K + 1, j + 2 * K);
      temp += getsum(i - 3 * K + 1, i - 2 * K, j - 3 * K + 1, j - 2 * K) +
              getsum(i - K + 1, i, j - 3 * K + 1, j - 2 * K) +
              getsum(i + K + 1, i + 2 * K, j - 3 * K + 1, j - 2 * K);
      temp += getsum(i - 2 * K + 1, i - K, j - 2 * K + 1, j - K) +
              getsum(i + 1, i + K, j - 2 * K + 1, j - K) +
              getsum(i + 2 * K + 1, i + 3 * K, j - 2 * K + 1, j - K);
      temp += getsum(i - 2 * K + 1, i - K, j + 1, j + K) +
              getsum(i + 1, i + K, j + 1, j + K) +
              getsum(i + 2 * K + 1, i + 3 * K, j + 1, j + K);
      temp += getsum(i - 2 * K + 1, i - K, j + 2 * K + 1, j + 3 * K) +
              getsum(i + 1, i + K, j + 2 * K + 1, j + 3 * K) +
              getsum(i + 2 * K + 1, i + 3 * K, j + 2 * K + 1, j + 3 * K);
      ans = max(ans, temp);
    }
  }
  cout << ans << endl;
  return 0;
}