Problem p03458 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...)                                             \
  for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...)                                          \
  for (int i = (b - 1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;

struct sq {
  int y;
  int x;
  bool white;
};
int pl(sq e, int k, int i) {
  int K = 2 * k;
  return ((((e.y + K - i) % K >= k ? k : 0) + (e.white ? k : 0) + e.x) % K);
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int K = 2 * k;
  vector<sq> squ(n);
  for (auto &e : squ) {
    char col;
    cin >> e.y >> e.x >> col;
    e.y %= K;
    e.x %= K;
    e.white = (col == 'W');
  }
  sort(all(squ),
       [](sq a, sq b) { return (a.y != b.y ? a.y < b.y : a.x < b.x); });

  vector<int> check(K);
  int mae = 0, ushiro = 0;
  while (squ[ushiro].y < k)
    ushiro++;
  for (auto &e : squ)
    check[pl(e, k, 0)]++;
  for (int i : check)
    eprintf("%d ", i);
  eprintf("\n");
  int ans = 0;
  int tmp = 0;
  rep(i, k) tmp += check[i];
  chmax(ans, tmp);
  rep(i, K) {
    tmp += check[(i + k) % K] - check[i % K];
    chmax(ans, tmp);
  }
  rep(i, 1, k + 1) {
    while (mae < n && squ[mae].y < i) {
      check[pl(squ[mae], k, i - 1)]--;
      check[pl(squ[mae], k, i)]++;
      mae++;
    }
    while (ushiro < n && squ[ushiro].y < i + k) {
      check[pl(squ[ushiro], k, i - 1)]--;
      check[pl(squ[ushiro], k, i)]++;
      ushiro++;
    }
    eprintf("%d: %d %d\n", i, mae, ushiro);
    for (int i : check)
      eprintf("%d ", i);
    eprintf("\n");
    tmp = 0;
    rep(i, k) tmp += check[i];
    chmax(ans, tmp);
    rep(i, K) {
      tmp += check[(i + k) % K] - check[i % K];
      chmax(ans, tmp);
    }
  }

  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...)                                             \
  for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...)                                          \
  for (int i = (b - 1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;

struct sq {
  int y;
  int x;
  bool white;
};
int pl(sq e, int k, int i) {
  int K = 2 * k;
  return ((((e.y + K - i) % K >= k ? k : 0) + (e.white ? k : 0) + e.x) % K);
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  int K = 2 * k;
  vector<sq> squ(n);
  for (auto &e : squ) {
    char col;
    cin >> e.y >> e.x >> col;
    e.y %= K;
    e.x %= K;
    e.white = (col == 'W');
  }
  sort(all(squ),
       [](sq a, sq b) { return (a.y != b.y ? a.y < b.y : a.x < b.x); });

  vector<int> check(K);
  int mae = 0, ushiro = 0;
  while (ushiro < n && squ[ushiro].y < k)
    ushiro++;
  for (auto &e : squ)
    check[pl(e, k, 0)]++;
  for (int i : check)
    eprintf("%d ", i);
  eprintf("\n");
  int ans = 0;
  int tmp = 0;
  rep(i, k) tmp += check[i];
  chmax(ans, tmp);
  rep(i, K) {
    tmp += check[(i + k) % K] - check[i % K];
    chmax(ans, tmp);
  }
  rep(i, 1, k + 1) {
    while (mae < n && squ[mae].y < i) {
      check[pl(squ[mae], k, i - 1)]--;
      check[pl(squ[mae], k, i)]++;
      mae++;
    }
    while (ushiro < n && squ[ushiro].y < i + k) {
      check[pl(squ[ushiro], k, i - 1)]--;
      check[pl(squ[ushiro], k, i)]++;
      ushiro++;
    }
    eprintf("%d: %d %d\n", i, mae, ushiro);
    for (int i : check)
      eprintf("%d ", i);
    eprintf("\n");
    tmp = 0;
    rep(i, k) tmp += check[i];
    chmax(ans, tmp);
    rep(i, K) {
      tmp += check[(i + k) % K] - check[i % K];
      chmax(ans, tmp);
    }
  }

  cout << ans << "\n";
  return 0;
}