Problem p03460 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i <= (y); ++i)
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int a[2][2010][2010];
int sum[2][2010][2010];
#define All(x) (sum[x][2 * k][2 * k])
inline int Get(int w, int x1, int y1, int x2, int y2) {
  if (x1 > x2 || y1 > y2)
    return 0;
  return sum[w][x2][y2] - sum[w][x2][y1 - 1] - sum[w][x1 - 1][y2] +
         sum[w][x1 - 1][y1 - 1];
}
int main() {
  freopen("in.txt", "r", stdin);
  int n = read(), k = read();
  char s[10];
  rep(i, 1, n) {
    int x = read(), y = read();
    x = x % (2 * k) + 1;
    y = y % (2 * k) + 1;
    //	printf("%d %d\n", x, y);
    scanf("%s", s);
    if (s[0] == 'B')
      ++a[0][x][y];
    else
      ++a[1][x][y];
  }
  rep(w, 0, 1) {
    rep(i, 1, 2 * k) rep(j, 1, 2 * k) {
      sum[w][i][j] = sum[w][i - 1][j] + sum[w][i][j - 1] -
                     sum[w][i - 1][j - 1] + a[w][i][j];
    }
  }
  // printf("%d %d\n", All(0), All(1));
  int res = 0;
  rep(i, 1, k) rep(j, 1, k) {
    int ans[2];
    ans[0] = ans[1] = 0;
    rep(w, 0, 1) {
      ans[w] += Get(w, i, j, i + k - 1, j + k - 1);
      ans[w] += Get(w, i + k, 1, 2 * k, j - 1);
      ans[w] += Get(w, i + k, j + k, 2 * k, 2 * k);
      ans[w] += Get(w, 1, j + k, i - 1, 2 * k);
      ans[w] += Get(w, 1, 1, i - 1, j - 1);
    }
    res = max(res, All(0) - ans[0] + ans[1]);
    res = max(res, All(1) - ans[1] + ans[0]);
    // printf("%d %d %d %d\n", i, j, All(0) - ans[0] + ans[1], All(1) - ans[1] +
    // ans[0]);
  }
  printf("%d\n", res);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (int i = (x); i <= (y); ++i)
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int a[2][2010][2010];
int sum[2][2010][2010];
#define All(x) (sum[x][2 * k][2 * k])
inline int Get(int w, int x1, int y1, int x2, int y2) {
  if (x1 > x2 || y1 > y2)
    return 0;
  return sum[w][x2][y2] - sum[w][x2][y1 - 1] - sum[w][x1 - 1][y2] +
         sum[w][x1 - 1][y1 - 1];
}
int main() {
  //	freopen("in.txt", "r", stdin);
  int n = read(), k = read();
  char s[10];
  rep(i, 1, n) {
    int x = read(), y = read();
    x = x % (2 * k) + 1;
    y = y % (2 * k) + 1;
    //	printf("%d %d\n", x, y);
    scanf("%s", s);
    if (s[0] == 'B')
      ++a[0][x][y];
    else
      ++a[1][x][y];
  }
  rep(w, 0, 1) {
    rep(i, 1, 2 * k) rep(j, 1, 2 * k) {
      sum[w][i][j] = sum[w][i - 1][j] + sum[w][i][j - 1] -
                     sum[w][i - 1][j - 1] + a[w][i][j];
    }
  }
  // printf("%d %d\n", All(0), All(1));
  int res = 0;
  rep(i, 1, k) rep(j, 1, k) {
    int ans[2];
    ans[0] = ans[1] = 0;
    rep(w, 0, 1) {
      ans[w] += Get(w, i, j, i + k - 1, j + k - 1);
      ans[w] += Get(w, i + k, 1, 2 * k, j - 1);
      ans[w] += Get(w, i + k, j + k, 2 * k, 2 * k);
      ans[w] += Get(w, 1, j + k, i - 1, 2 * k);
      ans[w] += Get(w, 1, 1, i - 1, j - 1);
    }
    res = max(res, All(0) - ans[0] + ans[1]);
    res = max(res, All(1) - ans[1] + ans[0]);
    // printf("%d %d %d %d\n", i, j, All(0) - ans[0] + ans[1], All(1) - ans[1] +
    // ans[0]);
  }
  printf("%d\n", res);
  return 0;
}