Problem p03460 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define piii pair<pii, pii>
#define precise cout << fixed << setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
const int MAX = 2005;
struct query {
  int x, y, c;
};
query q[MAX * 10];
int zlicz[MAX][MAX][2];
int pref[MAX * 2][MAX * 2][2];
int ans = 0;

int get(int x1, int y1, int x2, int y2, int t) {
  return pref[x2][y2][t] - pref[x2][y1 - 1][t] - pref[x1 - 1][y2][t] +
         pref[x1 - 1][y1 - 1][t];
}

int32_t main() {
  BOOST;
  int n, k;
  cin >> n >> k;
  k *= 2;
  int white = 0, black = 0;
  for (int i = 1; i <= n; i++) {
    int a, b;
    char c;
    cin >> a >> b >> c;
    int c2;
    if (c == 'B')
      c2 = 0, black++;
    else
      c2 = 1, white++;
    q[i] = {a, b, c2};
    zlicz[(q[i].x) % k + 1][(q[i].y) % k + 1][q[i].c]++;
  }

  int xd1 = 0, xd2 = 0;
  for (int i = 1; i <= k * 2; i++)
    for (int j = 1; j <= k * 2; j++)
      for (int l = 0; l < 2; l++) {
        xd1 = 0, xd2 = 0;
        if (i > k)
          xd1 = k;
        if (j > k)
          xd2 = k;
        if (i - xd1 > k || i - xd1 < 0 || j - xd2 < 0 || j - xd2 > k) {
          while (true) {
            int a = 0;
            a++;
          }
        }
        pref[i][j][l] = pref[i][j - 1][l] + pref[i - 1][j][l] -
                        pref[i - 1][j - 1][l] + zlicz[i - xd1][j - xd2][l];
      }

  int sum = 0;
  int ile = k / 2;
  int x, y;
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
      for (int l = 0; l < 2; l++) {
        sum = 0;
        x = i, y = j;
        sum += get(x, y, x + ile - 1, y + ile - 1, l);
        sum += get(x + ile, y, x + ile * 2 - 1, y + ile - 1, l ^ 1);
        sum += get(x, y + ile, x + ile - 1, y + ile * 2 - 1, l ^ 1);
        sum += get(x + ile, y + ile, x + 2 * ile - 1, y + 2 * ile - 1, l);
        ans = max(ans, sum);
      }

  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define mp make_pair
#define pb push_back
#define ld long double
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define piii pair<pii, pii>
#define precise cout << fixed << setprecision(10)
#define st first
#define nd second
#define ins insert
#define vi vector<int>
#define BOOST                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
const int MAX = 2005;
struct query {
  int x, y, c;
};
query q[MAX * 100];
int zlicz[MAX][MAX][2];
int pref[MAX * 2][MAX * 2][2];
int ans = 0;

int get(int x1, int y1, int x2, int y2, int t) {
  return pref[x2][y2][t] - pref[x2][y1 - 1][t] - pref[x1 - 1][y2][t] +
         pref[x1 - 1][y1 - 1][t];
}

int32_t main() {
  BOOST;
  int n, k;
  cin >> n >> k;
  k *= 2;
  int white = 0, black = 0;
  for (int i = 1; i <= n; i++) {
    int a, b;
    char c;
    cin >> a >> b >> c;
    int c2;
    if (c == 'B')
      c2 = 0, black++;
    else
      c2 = 1, white++;
    q[i] = {a, b, c2};
    zlicz[(q[i].x) % k + 1][(q[i].y) % k + 1][q[i].c]++;
  }

  int xd1 = 0, xd2 = 0;
  for (int i = 1; i <= k * 2; i++)
    for (int j = 1; j <= k * 2; j++)
      for (int l = 0; l < 2; l++) {
        xd1 = 0, xd2 = 0;
        if (i > k)
          xd1 = k;
        if (j > k)
          xd2 = k;
        if (i - xd1 > k || i - xd1 < 0 || j - xd2 < 0 || j - xd2 > k) {
          while (true) {
            int a = 0;
            a++;
          }
        }
        pref[i][j][l] = pref[i][j - 1][l] + pref[i - 1][j][l] -
                        pref[i - 1][j - 1][l] + zlicz[i - xd1][j - xd2][l];
      }

  int sum = 0;
  int ile = k / 2;
  int x, y;
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++)
      for (int l = 0; l < 2; l++) {
        sum = 0;
        x = i, y = j;
        sum += get(x, y, x + ile - 1, y + ile - 1, l);
        sum += get(x + ile, y, x + ile * 2 - 1, y + ile - 1, l ^ 1);
        sum += get(x, y + ile, x + ile - 1, y + ile * 2 - 1, l ^ 1);
        sum += get(x + ile, y + ile, x + 2 * ile - 1, y + 2 * ile - 1, l);
        ans = max(ans, sum);
      }

  cout << ans;
  return 0;
}