Problem p03460 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

static const ll INF = 1e15;

struct CumulativeSum2D {
  int h;
  int w;
  std::vector<std::vector<ll>> &grid_;
  CumulativeSum2D(std::vector<std::vector<ll>> &grid) : grid_(grid) {
    h = grid_.size() - 1;
    w = grid_[0].size() - 1;
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        grid_[i + 1][j + 1] += grid_[i][j + 1] + grid_[i + 1][j] - grid_[i][j];
  }

  ll get_from_size(int left, int top, int width, int height) {
    return grid_[top + height][left + width] - grid_[top][left + width] -
           grid_[top + height][left] + grid_[top][left];
  }

  ll get_from_ltrb(int left, int top, int right, int bottom) {
    return get_from_size(left, top, right - left + 1, bottom - top + 1);
  }
};

int main(void) {
  ll n, k;
  cin >> n >> k;
  int k2 = 2 * k;
  vector<vector<ll>> black(k2 * 2, vector<ll>(k2 * 2, 0));
  vector<vector<ll>> white(k2 * 2, vector<ll>(k2 * 2, 0));
  rep(i, n) {
    ll x, y;
    char c;
    cin >> x >> y >> c;
    if (c == 'W') {
      white[x % k2 + 1][y % k2 + 1]++;
      white[x % k2 + k2 + 1][y % k2 + 1]++;
      white[x % k2 + 1][y % k2 + k2 + 1]++;
      white[x % k2 + k2 + 1][y % k2 + k2 + 1]++;
    } else {
      black[x % k2 + 1][y % k2 + 1]++;
      black[x % k2 + k2 + 1][y % k2 + 1]++;
      black[x % k2 + 1][y % k2 + k2 + 1]++;
      black[x % k2 + k2 + 1][y % k2 + k2 + 1]++;
    }
  }
  CumulativeSum2D b(black);
  CumulativeSum2D w(white);

  ll ans = 0;
  for (int y = 0; y < k2; y++) {
    for (int x = 0; x < k2; x++) {
      ll r = 0;
      r += w.get_from_size(x, y, k, k);
      r += w.get_from_size(x + k, y + k, k, k);
      r += b.get_from_size(x + k, y, k, k);
      r += b.get_from_size(x, y + k, k, k);
      ans = max(ans, r);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

static const ll INF = 1e15;

struct CumulativeSum2D {
  int h;
  int w;
  std::vector<std::vector<ll>> &grid_;
  CumulativeSum2D(std::vector<std::vector<ll>> &grid) : grid_(grid) {
    h = grid_.size() - 1;
    w = grid_[0].size() - 1;
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        grid_[i + 1][j + 1] += grid_[i][j + 1] + grid_[i + 1][j] - grid_[i][j];
  }

  ll get_from_size(int left, int top, int width, int height) {
    return grid_[top + height][left + width] - grid_[top][left + width] -
           grid_[top + height][left] + grid_[top][left];
  }

  ll get_from_ltrb(int left, int top, int right, int bottom) {
    return get_from_size(left, top, right - left + 1, bottom - top + 1);
  }
};

int main(void) {
  ll n, k;
  cin >> n >> k;
  int k2 = 2 * k;
  vector<vector<ll>> black(k2 * 2 + 1, vector<ll>(k2 * 2 + 1, 0));
  vector<vector<ll>> white(k2 * 2 + 1, vector<ll>(k2 * 2 + 1, 0));
  rep(i, n) {
    ll x, y;
    char c;
    cin >> x >> y >> c;
    if (c == 'W') {
      white[x % k2 + 1][y % k2 + 1]++;
      white[x % k2 + k2 + 1][y % k2 + 1]++;
      white[x % k2 + 1][y % k2 + k2 + 1]++;
      white[x % k2 + k2 + 1][y % k2 + k2 + 1]++;
    } else {
      black[x % k2 + 1][y % k2 + 1]++;
      black[x % k2 + k2 + 1][y % k2 + 1]++;
      black[x % k2 + 1][y % k2 + k2 + 1]++;
      black[x % k2 + k2 + 1][y % k2 + k2 + 1]++;
    }
  }
  CumulativeSum2D b(black);
  CumulativeSum2D w(white);

  ll ans = 0;
  for (int y = 0; y < k2; y++) {
    for (int x = 0; x < k2; x++) {
      ll r = 0;
      r += w.get_from_size(x, y, k, k);
      r += w.get_from_size(x + k, y + k, k, k);
      r += b.get_from_size(x + k, y, k, k);
      r += b.get_from_size(x, y + k, k, k);
      ans = max(ans, r);
    }
  }
  cout << ans << endl;
  return 0;
}