Problem p03458 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int N, K;
int x, y;
char c;

int field[2005][2005];
int sum[2005][2005];

int cnt_req(int x1, int y1, int x2, int y2) {
  // if(x2 < x1 || y2 < y1) return 0;
  // cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl;
  int a, b, c;
  if (x1 == 0 || y1 == 0)
    a = 0;
  else
    a = sum[y1 - 1][x1 - 1];
  if (x1 == 0)
    b = 0;
  else
    b = sum[y2][x1 - 1];
  if (y1 == 0)
    c = 0;
  else
    c = sum[y1 - 1][x2];
  return sum[y2][x2] - b - c + a;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> K;

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

  for (int i = 0; i < 2 * K; i++) {
    for (int j = 0; j < 2 * K; j++) {
      int a, b, c;
      if (i - 1 < 0 || j - 1 < 0)
        a = 0;
      else
        a = sum[i - 1][j - 1];
      if (i - 1 < 0)
        b = 0;
      else
        b = sum[i - 1][j];
      if (j - 1 < 0)
        c = 0;
      else
        c = sum[i][j - 1];
      sum[i][j] = field[i][j] + c + b - a;
    }
  }

  int ans = 0;

  for (int i = 0; i < 2 * K; i++) {
    for (int j = 0; j < 2 * K; j++) {
      int ax = i, ay = j, bx = i + K - 1, by = j + K - 1;
      int cnt = 0;
      for (int ii = -5; ii <= 5; ii++) {
        for (int jj = -5; jj <= 5; jj++) {
          int sx = ax + K * ii + K * jj, sy = ay + K * ii - K * jj;
          int tx = bx + K * ii + K * jj, ty = by + K * ii - K * jj;
          if ((sx < 2 * K && sy < 2 * K && sx >= 0 && sy >= 0) ||
              (tx >= 0 && ty >= 0 && tx < 2 * K && ty < 2 * K) ||
              (tx >= 0 && sy >= 0 && tx < 2 * K && sy < 2 * K) ||
              (sx >= 0 && ty >= 0 && sx < 2 * K && ty < 2 * K)) {
            cnt += cnt_req(max(0, sx), max(0, sy), min(2 * K - 1, tx),
                           min(2 * K - 1, ty));
          }
        }
      }
      ans = max(ans, cnt);
    }
  }

  cout << ans << endl;

  return 0;
}


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

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int N, K;
int x, y;
char c;

int field[2005][2005];
int sum[2005][2005];

int cnt_req(int x1, int y1, int x2, int y2) {
  // if(x2 < x1 || y2 < y1) return 0;
  // cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl;
  int a, b, c;
  if (x1 == 0 || y1 == 0)
    a = 0;
  else
    a = sum[y1 - 1][x1 - 1];
  if (x1 == 0)
    b = 0;
  else
    b = sum[y2][x1 - 1];
  if (y1 == 0)
    c = 0;
  else
    c = sum[y1 - 1][x2];
  return sum[y2][x2] - b - c + a;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> K;

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

  for (int i = 0; i < 2 * K; i++) {
    for (int j = 0; j < 2 * K; j++) {
      int a, b, c;
      if (i - 1 < 0 || j - 1 < 0)
        a = 0;
      else
        a = sum[i - 1][j - 1];
      if (i - 1 < 0)
        b = 0;
      else
        b = sum[i - 1][j];
      if (j - 1 < 0)
        c = 0;
      else
        c = sum[i][j - 1];
      sum[i][j] = field[i][j] + c + b - a;
    }
  }

  int ans = 0;

  for (int i = 0; i < 2 * K; i++) {
    for (int j = 0; j < 2 * K; j++) {
      int ax = i, ay = j, bx = i + K - 1, by = j + K - 1;
      int cnt = 0;
      for (int ii = -3; ii <= 3; ii++) {
        for (int jj = -3; jj <= 3; jj++) {
          int sx = ax + K * ii + K * jj, sy = ay + K * ii - K * jj;
          int tx = bx + K * ii + K * jj, ty = by + K * ii - K * jj;
          if ((sx < 2 * K && sy < 2 * K && sx >= 0 && sy >= 0) ||
              (tx >= 0 && ty >= 0 && tx < 2 * K && ty < 2 * K) ||
              (tx >= 0 && sy >= 0 && tx < 2 * K && sy < 2 * K) ||
              (sx >= 0 && ty >= 0 && sx < 2 * K && ty < 2 * K)) {
            cnt += cnt_req(max(0, sx), max(0, sy), min(2 * K - 1, tx),
                           min(2 * K - 1, ty));
          }
        }
      }
      ans = max(ans, cnt);
    }
  }

  cout << ans << endl;

  return 0;
}