Problem p03460 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

using namespace std;

typedef pair<int, char> P;

int ans[2000][2000];
int ans_[2000][2000];
vector<P> blocks_x[2000];
vector<P> blocks_y[2000];

int main() {
  int N, K;

  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    blocks_x[x % (2 * K)].push_back(P(y % (2 * K), c));
    blocks_y[y % (2 * K)].push_back(P(x % (2 * K), c));
    if (x % (2 * K) < K && y % (2 * K) < K && c == 'W')
      ans[0][0]++;
    if (x % (2 * K) < K && y % (2 * K) >= K && c == 'B')
      ans[0][0]++;
    if (x % (2 * K) >= K && y % (2 * K) < K && c == 'B')
      ans[0][0]++;
    if (x % (2 * K) >= K && y % (2 * K) >= K && c == 'W')
      ans[0][0]++;

    if (x % (2 * K) < K && y % (2 * K) < K && c == 'B')
      ans_[0][0]++;
    if (x % (2 * K) < K && y % (2 * K) >= K && c == 'W')
      ans_[0][0]++;
    if (x % (2 * K) >= K && y % (2 * K) < K && c == 'W')
      ans_[0][0]++;
    if (x % (2 * K) >= K && y % (2 * K) >= K && c == 'B')
      ans_[0][0]++;
  }
  for (int i = 0; i < K; i++) {
    if (i != 0) {
      ans[i][0] = ans[i - 1][0];
      for (int k = 0; k < blocks_x[i - 1].size(); k++) {
        if (blocks_x[i - 1][k].first < K && blocks_x[i - 1][k].second == 'W')
          ans[i][0]--;
        if (blocks_x[i - 1][k].first < K && blocks_x[i - 1][k].second == 'B')
          ans[i][0]++;
        if (blocks_x[i - 1][k].first >= K && blocks_x[i - 1][k].second == 'W')
          ans[i][0]++;
        if (blocks_x[i - 1][k].first >= K && blocks_x[i - 1][k].second == 'B')
          ans[i][0]--;
      }
      for (int k = 0; k < blocks_x[i - 1 + K].size(); k++) {
        if (blocks_x[i - 1 + K][k].first < K &&
            blocks_x[i - 1 + K][k].second == 'W')
          ans[i][0]++;
        if (blocks_x[i - 1 + K][k].first < K &&
            blocks_x[i - 1 + K][k].second == 'B')
          ans[i][0]--;
        if (blocks_x[i - 1 + K][k].first >= K &&
            blocks_x[i - 1 + K][k].second == 'W')
          ans[i][0]--;
        if (blocks_x[i - 1 + K][k].first >= K &&
            blocks_x[i - 1 + K][k].second == 'B')
          ans[i][0]++;
      }
    }
    for (int j = 1; j < K; j++) {
      ans[i][j] = ans[i][j - 1];
      for (int k = 0; k < blocks_y[j - 1].size(); k++) {
        if (blocks_y[j - 1][k].first < i) {
          if (blocks_y[j - 1][k].second == 'W')
            ans[i][j]++;
          if (blocks_y[j - 1][k].second == 'B')
            ans[i][j]--;
        }
        if (blocks_y[j - 1][k].first >= i && blocks_y[j - 1][k].first < i + K) {
          if (blocks_y[j - 1][k].second == 'W')
            ans[i][j]--;
          if (blocks_y[j - 1][k].second == 'B')
            ans[i][j]++;
        }
        if (blocks_y[j - 1][k].first >= i + K) {
          if (blocks_y[j - 1][k].second == 'W')
            ans[i][j]++;
          if (blocks_y[j - 1][k].second == 'B')
            ans[i][j]--;
        }
      }
      for (int k = 0; k < blocks_y[j - 1 + K].size(); k++) {
        if (blocks_y[j - 1 + K][k].first < i) {
          if (blocks_y[j - 1 + K][k].second == 'W')
            ans[i][j]--;
          if (blocks_y[j - 1 + K][k].second == 'B')
            ans[i][j]++;
        }
        if (blocks_y[j - 1 + K][k].first >= i &&
            blocks_y[j - 1 + K][k].first < i + K) {
          if (blocks_y[j - 1 + K][k].second == 'W')
            ans[i][j]++;
          if (blocks_y[j - 1 + K][k].second == 'B')
            ans[i][j]--;
        }
        if (blocks_y[j - 1 + K][k].first >= i + K) {
          if (blocks_y[j - 1 + K][k].second == 'W')
            ans[i][j]--;
          if (blocks_y[j - 1 + K][k].second == 'B')
            ans[i][j]++;
        }
      }
    }
  }

  for (int i = 0; i < K; i++) {
    if (i != 0) {
      ans_[i][0] = ans_[i - 1][0];
      for (int k = 0; k < blocks_x[i - 1].size(); k++) {
        if (blocks_x[i - 1][k].first < K && blocks_x[i - 1][k].second == 'B')
          ans_[i][0]--;
        if (blocks_x[i - 1][k].first < K && blocks_x[i - 1][k].second == 'W')
          ans_[i][0]++;
        if (blocks_x[i - 1][k].first >= K && blocks_x[i - 1][k].second == 'B')
          ans_[i][0]++;
        if (blocks_x[i - 1][k].first >= K && blocks_x[i - 1][k].second == 'W')
          ans_[i][0]--;
      }
      for (int k = 0; k < blocks_x[i - 1 + K].size(); k++) {
        if (blocks_x[i - 1 + K][k].first < K &&
            blocks_x[i - 1 + K][k].second == 'B')
          ans_[i][0]++;
        if (blocks_x[i - 1 + K][k].first < K &&
            blocks_x[i - 1 + K][k].second == 'W')
          ans_[i][0]--;
        if (blocks_x[i - 1 + K][k].first >= K &&
            blocks_x[i - 1 + K][k].second == 'B')
          ans_[i][0]--;
        if (blocks_x[i - 1 + K][k].first >= K &&
            blocks_x[i - 1 + K][k].second == 'W')
          ans_[i][0]++;
      }
    }
    for (int j = 1; j < K; j++) {
      ans_[i][j] = ans_[i][j - 1];
      for (int k = 0; k < blocks_y[j - 1].size(); k++) {
        if (blocks_y[j - 1][k].first < i) {
          if (blocks_y[j - 1][k].second == 'B')
            ans_[i][j]++;
          if (blocks_y[j - 1][k].second == 'W')
            ans_[i][j]--;
        }
        if (blocks_y[j - 1][k].first >= i && blocks_y[j - 1][k].first < i + K) {
          if (blocks_y[j - 1][k].second == 'B')
            ans_[i][j]--;
          if (blocks_y[j - 1][k].second == 'W')
            ans_[i][j]++;
        }
        if (blocks_y[j - 1][k].first >= i + K) {
          if (blocks_y[j - 1][k].second == 'B')
            ans_[i][j]++;
          if (blocks_y[j - 1][k].second == 'W')
            ans_[i][j]--;
        }
      }
      for (int k = 0; k < blocks_y[j - 1 + K].size(); k++) {
        if (blocks_y[j - 1 + K][k].first < i) {
          if (blocks_y[j - 1 + K][k].second == 'B')
            ans_[i][j]--;
          if (blocks_y[j - 1 + K][k].second == 'W')
            ans_[i][j]++;
        }
        if (blocks_y[j - 1 + K][k].first >= i &&
            blocks_y[j - 1 + K][k].first < i + K) {
          if (blocks_y[j - 1 + K][k].second == 'B')
            ans_[i][j]++;
          if (blocks_y[j - 1 + K][k].second == 'W')
            ans_[i][j]--;
        }
        if (blocks_y[j - 1 + K][k].first >= i + K) {
          if (blocks_y[j - 1 + K][k].second == 'B')
            ans_[i][j]--;
          if (blocks_y[j - 1 + K][k].second == 'W')
            ans_[i][j]++;
        }
      }
    }
  }

  int cnt = 0;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      cnt = max(cnt, ans[i][j]);
    }
  }

  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      cnt = max(cnt, ans_[i][j]);
    }
  }
  cout << cnt << endl;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

typedef pair<int, char> P;

int ans[1000][1000];
int ans_[1000][1000];
vector<P> blocks_x[2000];
vector<P> blocks_y[2000];

int main() {
  int N, K;

  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    blocks_x[x % (2 * K)].push_back(P(y % (2 * K), c));
    blocks_y[y % (2 * K)].push_back(P(x % (2 * K), c));
    if (x % (2 * K) < K && y % (2 * K) < K && c == 'W')
      ans[0][0]++;
    if (x % (2 * K) < K && y % (2 * K) >= K && c == 'B')
      ans[0][0]++;
    if (x % (2 * K) >= K && y % (2 * K) < K && c == 'B')
      ans[0][0]++;
    if (x % (2 * K) >= K && y % (2 * K) >= K && c == 'W')
      ans[0][0]++;

    if (x % (2 * K) < K && y % (2 * K) < K && c == 'B')
      ans_[0][0]++;
    if (x % (2 * K) < K && y % (2 * K) >= K && c == 'W')
      ans_[0][0]++;
    if (x % (2 * K) >= K && y % (2 * K) < K && c == 'W')
      ans_[0][0]++;
    if (x % (2 * K) >= K && y % (2 * K) >= K && c == 'B')
      ans_[0][0]++;
  }
  for (int i = 0; i < K; i++) {
    if (i != 0) {
      ans[i][0] = ans[i - 1][0];
      for (int k = 0; k < blocks_x[i - 1].size(); k++) {
        if (blocks_x[i - 1][k].first < K && blocks_x[i - 1][k].second == 'W')
          ans[i][0]--;
        if (blocks_x[i - 1][k].first < K && blocks_x[i - 1][k].second == 'B')
          ans[i][0]++;
        if (blocks_x[i - 1][k].first >= K && blocks_x[i - 1][k].second == 'W')
          ans[i][0]++;
        if (blocks_x[i - 1][k].first >= K && blocks_x[i - 1][k].second == 'B')
          ans[i][0]--;
      }
      for (int k = 0; k < blocks_x[i - 1 + K].size(); k++) {
        if (blocks_x[i - 1 + K][k].first < K &&
            blocks_x[i - 1 + K][k].second == 'W')
          ans[i][0]++;
        if (blocks_x[i - 1 + K][k].first < K &&
            blocks_x[i - 1 + K][k].second == 'B')
          ans[i][0]--;
        if (blocks_x[i - 1 + K][k].first >= K &&
            blocks_x[i - 1 + K][k].second == 'W')
          ans[i][0]--;
        if (blocks_x[i - 1 + K][k].first >= K &&
            blocks_x[i - 1 + K][k].second == 'B')
          ans[i][0]++;
      }
    }
    for (int j = 1; j < K; j++) {
      ans[i][j] = ans[i][j - 1];
      for (int k = 0; k < blocks_y[j - 1].size(); k++) {
        if (blocks_y[j - 1][k].first < i) {
          if (blocks_y[j - 1][k].second == 'W')
            ans[i][j]++;
          if (blocks_y[j - 1][k].second == 'B')
            ans[i][j]--;
        }
        if (blocks_y[j - 1][k].first >= i && blocks_y[j - 1][k].first < i + K) {
          if (blocks_y[j - 1][k].second == 'W')
            ans[i][j]--;
          if (blocks_y[j - 1][k].second == 'B')
            ans[i][j]++;
        }
        if (blocks_y[j - 1][k].first >= i + K) {
          if (blocks_y[j - 1][k].second == 'W')
            ans[i][j]++;
          if (blocks_y[j - 1][k].second == 'B')
            ans[i][j]--;
        }
      }
      for (int k = 0; k < blocks_y[j - 1 + K].size(); k++) {
        if (blocks_y[j - 1 + K][k].first < i) {
          if (blocks_y[j - 1 + K][k].second == 'W')
            ans[i][j]--;
          if (blocks_y[j - 1 + K][k].second == 'B')
            ans[i][j]++;
        }
        if (blocks_y[j - 1 + K][k].first >= i &&
            blocks_y[j - 1 + K][k].first < i + K) {
          if (blocks_y[j - 1 + K][k].second == 'W')
            ans[i][j]++;
          if (blocks_y[j - 1 + K][k].second == 'B')
            ans[i][j]--;
        }
        if (blocks_y[j - 1 + K][k].first >= i + K) {
          if (blocks_y[j - 1 + K][k].second == 'W')
            ans[i][j]--;
          if (blocks_y[j - 1 + K][k].second == 'B')
            ans[i][j]++;
        }
      }
    }
  }

  for (int i = 0; i < K; i++) {
    if (i != 0) {
      ans_[i][0] = ans_[i - 1][0];
      for (int k = 0; k < blocks_x[i - 1].size(); k++) {
        if (blocks_x[i - 1][k].first < K && blocks_x[i - 1][k].second == 'B')
          ans_[i][0]--;
        if (blocks_x[i - 1][k].first < K && blocks_x[i - 1][k].second == 'W')
          ans_[i][0]++;
        if (blocks_x[i - 1][k].first >= K && blocks_x[i - 1][k].second == 'B')
          ans_[i][0]++;
        if (blocks_x[i - 1][k].first >= K && blocks_x[i - 1][k].second == 'W')
          ans_[i][0]--;
      }
      for (int k = 0; k < blocks_x[i - 1 + K].size(); k++) {
        if (blocks_x[i - 1 + K][k].first < K &&
            blocks_x[i - 1 + K][k].second == 'B')
          ans_[i][0]++;
        if (blocks_x[i - 1 + K][k].first < K &&
            blocks_x[i - 1 + K][k].second == 'W')
          ans_[i][0]--;
        if (blocks_x[i - 1 + K][k].first >= K &&
            blocks_x[i - 1 + K][k].second == 'B')
          ans_[i][0]--;
        if (blocks_x[i - 1 + K][k].first >= K &&
            blocks_x[i - 1 + K][k].second == 'W')
          ans_[i][0]++;
      }
    }
    for (int j = 1; j < K; j++) {
      ans_[i][j] = ans_[i][j - 1];
      for (int k = 0; k < blocks_y[j - 1].size(); k++) {
        if (blocks_y[j - 1][k].first < i) {
          if (blocks_y[j - 1][k].second == 'B')
            ans_[i][j]++;
          if (blocks_y[j - 1][k].second == 'W')
            ans_[i][j]--;
        }
        if (blocks_y[j - 1][k].first >= i && blocks_y[j - 1][k].first < i + K) {
          if (blocks_y[j - 1][k].second == 'B')
            ans_[i][j]--;
          if (blocks_y[j - 1][k].second == 'W')
            ans_[i][j]++;
        }
        if (blocks_y[j - 1][k].first >= i + K) {
          if (blocks_y[j - 1][k].second == 'B')
            ans_[i][j]++;
          if (blocks_y[j - 1][k].second == 'W')
            ans_[i][j]--;
        }
      }
      for (int k = 0; k < blocks_y[j - 1 + K].size(); k++) {
        if (blocks_y[j - 1 + K][k].first < i) {
          if (blocks_y[j - 1 + K][k].second == 'B')
            ans_[i][j]--;
          if (blocks_y[j - 1 + K][k].second == 'W')
            ans_[i][j]++;
        }
        if (blocks_y[j - 1 + K][k].first >= i &&
            blocks_y[j - 1 + K][k].first < i + K) {
          if (blocks_y[j - 1 + K][k].second == 'B')
            ans_[i][j]++;
          if (blocks_y[j - 1 + K][k].second == 'W')
            ans_[i][j]--;
        }
        if (blocks_y[j - 1 + K][k].first >= i + K) {
          if (blocks_y[j - 1 + K][k].second == 'B')
            ans_[i][j]--;
          if (blocks_y[j - 1 + K][k].second == 'W')
            ans_[i][j]++;
        }
      }
    }
  }

  int cnt = 0;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      cnt = max(cnt, ans[i][j]);
    }
  }

  for (int i = 0; i < K; i++) {
    for (int j = 0; j < K; j++) {
      cnt = max(cnt, ans_[i][j]);
    }
  }
  cout << cnt << endl;
}