prithivMLmods/Coder-Stat huggingface dataset:
Problem p03457 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
const int N = 10005;
using namespace std;
typedef long long ll;
int x[N], y[N], t[N];

int main() {
  int n, c, flag = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d%d", &t[i], &x[i], &y[i]);
  for (int i = 1; i <= n; i++) {
    if (i == 1) {
      c = x[1] + y[1]; // c是距离
      if (t[i] % c != 0 || t[i] < c || t[i] / c % 2 != 1) {
        flag = 1;
        break;
      }
    }

    else {
      c = abs(x[i] - x[i - 1]) + abs(y[i] - y[i - 1]);
      int mid = t[i] - t[i - 1];
      if (mid % c != 0 || mid < c || mid / c % 2 != 1) {
        flag = 1;
        break;
      }
    }
  }
  if (flag)
    printf("No\n");
  else
    printf("Yes\n");
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
const int N = 100005;
using namespace std;
typedef long long ll;
int x[N], y[N], t[N];

int main() {
  int n, c, flag = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d%d", &t[i], &x[i], &y[i]);
  for (int i = 1; i <= n; i++) {
    if (i == 1) {
      c = x[1] + y[1]; // c是距离
      if (t[i] % c != 0 || t[i] < c || t[i] / c % 2 != 1) {
        flag = 1;
        break;
      }
    }

    else {
      c = abs(x[i] - x[i - 1]) + abs(y[i] - y[i - 1]);
      int mid = t[i] - t[i - 1];
      if (mid % c != 0 || mid < c || mid / c % 2 != 1) {
        flag = 1;
        break;
      }
    }
  }
  if (flag)
    printf("No\n");
  else
    printf("Yes\n");
}
Problem p03457 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int N;
  int t[11000], x[11000], y[11000];
  cin >> N;
  t[0] = x[0] = y[0] = 0;

  for (int i = 1; i < N + 1; i++)
    cin >> t[i] >> x[i] >> y[i];
  bool can = true;
  int dist = 0;
  int dt = 0;
  for (int i = 1; i < N + 1; i++) {
    dt = t[i] - t[i - 1];
    dist = abs(x[i] - x[i - 1]) + abs(y[i] - y[i - 1]);
    if (dt < dist) {
      can = false;
      break;
    }
    if (dist % 2 != dt % 2) {
      can = false;
      break;
    }
  }
  if (can)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int N;
  int t[110000], x[110000], y[110000];
  cin >> N;
  t[0] = x[0] = y[0] = 0;

  for (int i = 1; i < N + 1; i++)
    cin >> t[i] >> x[i] >> y[i];
  bool can = true;
  int dist = 0;
  int dt = 0;
  for (int i = 1; i < N + 1; i++) {
    dt = t[i] - t[i - 1];
    dist = abs(x[i] - x[i - 1]) + abs(y[i] - y[i - 1]);
    if (dt < dist) {
      can = false;
      break;
    }
    if (dist % 2 != dt % 2) {
      can = false;
      break;
    }
  }
  if (can)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p03457 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int abs(int x, int y) {
  if (x > y) {
    return x - y;
  } else {
    return y - x;
  }
}

int dist(int x1, int y1, int x2, int y2) { return abs(x1, x2) + abs(y1, y2); }

int main() {
  int N;
  cin >> N;
  vector<int> t(N), x(N), y(N);

  for (int i = 0; i < N; i++) {
    cin >> t.at(i) >> x.at(i) >> y.at(i);
  }

  for (int i = 0; i < N; i++) {
    if (t.at(i + 1) - t.at(i) <
        dist(x.at(i), y.at(i), x.at(i + 1), y.at(i + 1))) {
      cout << "No" << endl;
      break;
    } else if ((t.at(i + 1) + t.at(i) + x.at(i + 1) + x.at(i) + y.at(i + 1) +
                y.at(i)) %
                   2 ==
               1) {
      cout << "No" << endl;
      break;
    }
    if (i == N - 1) {
      cout << "Yes" << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int abs(int x, int y) {
  if (x > y) {
    return x - y;
  } else {
    return y - x;
  }
}

int dist(int x1, int y1, int x2, int y2) { return abs(x1, x2) + abs(y1, y2); }

int main() {
  int N;
  cin >> N;
  vector<int> t(N), x(N), y(N);

  for (int i = 0; i < N; i++) {
    cin >> t.at(i) >> x.at(i) >> y.at(i);
  }

  int p;

  if (t.at(0) < x.at(0) + y.at(0)) {
    cout << "No" << endl;
    p = 0;
  } else if ((t.at(0) + x.at(0) + y.at(0)) % 2 == 1) {
    cout << "No" << endl;
    p = 0;
  } else if (N == 1) {
    cout << "Yes" << endl;
    p = 0;
  } else {
    p = 1;
  }

  if (p == 1) {

    for (int i = 0; i < N - 1; i++) {

      if (t.at(i + 1) - t.at(i) <
          dist(x.at(i), y.at(i), x.at(i + 1), y.at(i + 1))) {
        cout << "No" << endl;
        break;
      } else if ((t.at(i + 1) + t.at(i) + x.at(i + 1) + x.at(i) + y.at(i + 1) +
                  y.at(i)) %
                     2 ==
                 1) {
        cout << "No" << endl;
        break;
      }

      if (i == N - 2) {
        cout << "Yes" << endl;
      }
    }
  }
}
Problem p03457 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;
  int t[11000], x[11000], y[11000];
  t[0] = x[0] = y[0] = 0;
  for (int i = 0; i < N; i++)
    cin >> t[i + 1] >> x[i + 1] >> y[i + 1];

  bool flag = true;
  for (int i = 0; i < N && flag; i++) {
    int time = t[i + 1] - t[i];
    int dir = abs(x[i + 1] - x[i]) + abs(y[i + 1] - y[i]);
    if (time < dir)
      flag = false;
    if (dir % 2 != time % 2)
      flag = false;
  }

  if (flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int N;
  cin >> N;
  int t[110000], x[110000], y[110000];
  t[0] = x[0] = y[0] = 0;
  for (int i = 0; i < N; i++)
    cin >> t[i + 1] >> x[i + 1] >> y[i + 1];

  bool flag = true;
  for (int i = 0; i < N && flag; i++) {
    int time = t[i + 1] - t[i];
    int dir = abs(x[i + 1] - x[i]) + abs(y[i + 1] - y[i]);
    if (time < dir)
      flag = false;
    if (dir % 2 != time % 2)
      flag = false;
  }

  if (flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p03457 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> t(n);
  vector<int> x(n);
  vector<int> y(n);
  rep(i, n) cin >> t[i + 1] >> x[i + 1] >> y[i + 1];
  bool can = true;
  rep(i, n) {
    int dt = t[i + 1] - t[i];
    int dist = abs(x[i + 1] - x[i]) + abs(y[i + 1] - y[i]);
    if (dt < dist)
      can = false;
    if (dist % 2 != dt % 2)
      can = false;
  }
  if (can)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> t(n + 1);
  vector<int> x(n + 1);
  vector<int> y(n + 1);
  rep(i, n) cin >> t[i + 1] >> x[i + 1] >> y[i + 1];
  bool can = true;
  rep(i, n) {
    int dt = t[i + 1] - t[i];
    int dist = abs(x[i + 1] - x[i]) + abs(y[i + 1] - y[i]);
    if (dt < dist)
      can = false;
    if (dist % 2 != dt % 2)
      can = false;
  }
  if (can)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
Problem p03457 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, t[10005] = {0}, x[10005] = {0}, y[10005] = {0}, a = 0;
  cin >> n;
  t[0] = 0;
  x[0] = 0;
  y[0] = 0;
  int t1, x1, y1;
  for (int i = 1; i <= n; i++)
    cin >> t[i] >> x[i] >> y[i];
  for (int i = 1; i <= n; i++) {
    t1 = t[i] - t[i - 1];
    x1 = x[i] - x[i - 1];
    y1 = y[i] - y[i - 1];

    if (t1 < x1 + y1) {
      cout << "No";
      return 0;
    }
    a = t1 - x1 - y1;
    if (a % 2 != 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, t[100005] = {0}, x[100005] = {0}, y[100005] = {0}, a = 0;
  cin >> n;
  t[0] = 0;
  x[0] = 0;
  y[0] = 0;
  int t1, x1, y1;
  for (int i = 1; i <= n; i++)
    cin >> t[i] >> x[i] >> y[i];
  for (int i = 1; i <= n; i++) {
    t1 = t[i] - t[i - 1];
    x1 = x[i] - x[i - 1];
    y1 = y[i] - y[i - 1];

    if (t1 < x1 + y1) {
      cout << "No";
      return 0;
    }
    a = t1 - x1 - y1;
    if (a % 2 != 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  return 0;
}
Problem p03458 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int pt[1001][1001];

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> x(n), y(n);
  vector<char> c(n);
  int p, q, r, s;
  for (int i = 0; i < n; i++) {
    bool cl = 0;
    cin >> x[i] >> y[i] >> c[i];
    p = x[i] / k;
    q = y[i] / k;
    r = x[i] % k;
    s = y[i] % k;
    if (c[i] == 'B')
      cl = 1;
    if ((p + q) % 2)
      cl = !cl;
    if (cl) {
      pt[k][k]++;
      pt[r][k]--;
      pt[k][s]--;
      pt[r][s] += 2;
    } else {
      pt[r][k]++;
      pt[k][s]++;
      pt[r][s] -= 2;
    }
  }
  int ans = 0;
  for (int j = k; j >= 0; j--)
    for (int i = k; i >= 0; i--) {
      pt[i][j] = pt[i + 1][j] + pt[i][j + 1] - pt[i + 1][j + 1] + pt[i][j];
      ans = max(ans, max(pt[i][j], n - pt[i][j]));
    }

  cout << ans << endl;
  return 0;
}


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

using namespace std;

int pt[1005][1005];

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> x(n), y(n);
  vector<char> c(n);
  int p, q, r, s;
  for (int i = 0; i < n; i++) {
    bool cl = 0;
    cin >> x[i] >> y[i] >> c[i];
    p = x[i] / k;
    q = y[i] / k;
    r = x[i] % k;
    s = y[i] % k;
    if (c[i] == 'B')
      cl = 1;
    if ((p + q) % 2)
      cl = !cl;
    if (cl) {
      pt[k][k]++;
      pt[r][k]--;
      pt[k][s]--;
      pt[r][s] += 2;
    } else {
      pt[r][k]++;
      pt[k][s]++;
      pt[r][s] -= 2;
    }
  }
  int ans = 0;
  for (int j = k; j >= 0; j--)
    for (int i = k; i >= 0; i--) {
      pt[i][j] = pt[i + 1][j] + pt[i][j + 1] - pt[i + 1][j + 1] + pt[i][j];
      ans = max(ans, max(pt[i][j], n - pt[i][j]));
    }

  cout << ans << endl;
  return 0;
}
Problem p03458 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>

using namespace std;

int v[1001][1001];
int n, k;
int getsize(int x1, int y1, int x2, int y2) {
  return v[x2][y2] - v[x2][y1] - v[x1][y2] + v[x1][y1];
}
int main() {
  scanf("%d%d", &n, &k);
  int a, b;
  char c;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d %c", &a, &b, &c);
    a %= 2 * k, b %= 2 * k;
    if (c == 'W')
      b += k;
    v[a % (2 * k) + 1][b % (2 * k) + 1]++;
  }
  for (int i = 1; i <= 2 * k; i++)
    for (int j = 1; j <= 2 * k; j++)
      v[i][j] = v[i][j] + (v[i - 1][j] + v[i][j - 1] - v[i - 1][j - 1]);
  int ans = 0, sum;
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++) {
      sum = getsize(0, 0, i, j);
      sum += getsize(i, j, k + i, k + j) + getsize(k + i, k + j, 2 * k, 2 * k) +
             getsize(k + i, 0, 2 * k, j) + getsize(0, k + j, i, 2 * k);
      ans = max(ans, max(sum, n - sum));
    }
  printf("%d\n", ans);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>

using namespace std;

int v[2001][2001];
int n, k;
int getsize(int x1, int y1, int x2, int y2) {
  return v[x2][y2] - v[x2][y1] - v[x1][y2] + v[x1][y1];
}
int main() {
  scanf("%d%d", &n, &k);
  int a, b;
  char c;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d %c", &a, &b, &c);
    a %= 2 * k, b %= 2 * k;
    if (c == 'W')
      b += k;
    v[a % (2 * k) + 1][b % (2 * k) + 1]++;
  }
  for (int i = 1; i <= 2 * k; i++)
    for (int j = 1; j <= 2 * k; j++)
      v[i][j] = v[i][j] + (v[i - 1][j] + v[i][j - 1] - v[i - 1][j - 1]);
  int ans = 0, sum;
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= k; j++) {
      sum = getsize(0, 0, i, j);
      sum += getsize(i, j, k + i, k + j) + getsize(k + i, k + j, 2 * k, 2 * k) +
             getsize(k + i, 0, 2 * k, j) + getsize(0, k + j, i, 2 * k);
      ans = max(ans, max(sum, n - sum));
    }
  printf("%d\n", ans);
}
Problem p03458 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
int B[6010][6010], W[6010][6010];
int N, K;
int w;
int ans;
int main() {
  std::cin >> N >> K;
  int D[] = {0, K, 2 * K, K, 0, -K, -2 * K, -K, 0};
  int oD[] = {2 * K, K, 0, -K, -2 * K, -K, 0, K, 0};
  for (int i = 0; i < N; i++) {
    int a, b;
    char c;
    std::cin >> a >> b >> c;
    if (c == 'W') {
      b += K;
    }
    a++;
    b++;
    a %= 2 * K;
    b %= 2 * K;
    a += 2 * K;
    b += 2 * K;
    for (int i = 0; i < 9; i++) {
      int x = a + D[i];
      int y = b + oD[i];
      B[x][y]++;
      B[x][y + K]--;
      B[x + K][y]--;
      B[x + K][y + K]++;
    }
  }
  for (int i = 0; i < K * 4; i++) {
    for (int j = 0; j < K * 4; j++) {
      B[i + 1][j] += B[i][j];
    }
  }

  for (int i = 0; i < K * 4; i++) {
    for (int j = 0; j < K * 4; j++) {
      B[i][j + 1] += B[i][j];
    }
  }
  for (int i = 1; i < K * 4; i++) {
    for (int j = 1; j < K * 4; j++) {
      ans = std::max(ans, B[i][j]);
    }
  }
  std::cout << ans << std::endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
int B[8010][8010], W[8010][8010];
int N, K;
int w;
int ans;
int main() {
  std::cin >> N >> K;
  int D[] = {0, K, 2 * K, K, 0, -K, -2 * K, -K, 0};
  int oD[] = {2 * K, K, 0, -K, -2 * K, -K, 0, K, 0};
  for (int i = 0; i < N; i++) {
    int a, b;
    char c;
    std::cin >> a >> b >> c;
    if (c == 'W') {
      b += K;
    }
    a++;
    b++;
    a %= 2 * K;
    b %= 2 * K;
    a += 2 * K;
    b += 2 * K;
    for (int i = 0; i < 9; i++) {
      int x = a + D[i];
      int y = b + oD[i];
      B[x][y]++;
      B[x][y + K]--;
      B[x + K][y]--;
      B[x + K][y + K]++;
    }
  }
  for (int i = 0; i < K * 4; i++) {
    for (int j = 0; j < K * 4; j++) {
      B[i + 1][j] += B[i][j];
    }
  }

  for (int i = 0; i < K * 4; i++) {
    for (int j = 0; j < K * 4; j++) {
      B[i][j + 1] += B[i][j];
    }
  }
  for (int i = 1; i < K * 4; i++) {
    for (int j = 1; j < K * 4; j++) {
      ans = std::max(ans, B[i][j]);
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p03458 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[1001][1001];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int x, y, b;
    char c;
    cin >> x >> y >> c;
    if (c == 'W')
      y += k;
    b = x / k % 2 == y / k % 2 ? 1 : -1;
    if (b > 0)
      a[0][0]++;
    int l = x % k + 1, r = y % k + 1;
    a[l][0] -= b;
    a[0][r] -= b;
    a[l][r] += b * 2;
  }
  for (int i = 0; i <= k; i++)
    for (int j = 0; j < k; j++)
      a[i][j + 1] += a[i][j];
  for (int i = 0; i <= k; i++)
    for (int j = 0; j < k; j++)
      a[i + 1][j] += a[i][j];
  int s = 0;
  for (int i = 0; i < k; i++)
    for (int j = 0; j < k; j++)
      s = max(max(s, a[i][j]), n - a[i][j]);
  cout << s;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[1005][1005];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int x, y, b;
    char c;
    cin >> x >> y >> c;
    if (c == 'W')
      y += k;
    b = x / k % 2 == y / k % 2 ? 1 : -1;
    if (b > 0)
      a[0][0]++;
    int l = x % k + 1, r = y % k + 1;
    a[l][0] -= b;
    a[0][r] -= b;
    a[l][r] += b * 2;
  }
  for (int i = 0; i <= k; i++)
    for (int j = 0; j < k; j++)
      a[i][j + 1] += a[i][j];
  for (int i = 0; i <= k; i++)
    for (int j = 0; j < k; j++)
      a[i + 1][j] += a[i][j];
  int s = 0;
  for (int i = 0; i < k; i++)
    for (int j = 0; j < k; j++)
      s = max(max(s, a[i][j]), n - a[i][j]);
  cout << s;
}
Problem p03458 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)

// https://ei1333.github.io/algorithm/cumulative-sum2d.html
template <class T> struct CumulativeSum2D {
  T data[1001][1001];
  int w, h;

  CumulativeSum2D(int W, int H) {
    w = W;
    h = H;
  }

  void add(int x, int y, T z) {
    ++x, ++y;
    if (x >= w || y >= h)
      return;
    data[x][y] += z;
  }

  void build() {
    FOR(i, 1, w) FOR(j, 1, h) {
      data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];
    }
  }

  T query(int sx, int sy, int gx, int gy) {
    return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]);
  }

  void print() {
    for (auto &x : data) {
      for (auto &y : x) {
        cout << y << " ";
      }
      cout << endl;
    }
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  char c;
  int n, k, x, y;
  cin >> n >> k;
  CumulativeSum2D<int> s(k * 2, k * 2);
  REP(i, n) {
    cin >> x >> y >> c;
    if (c == 'W') {
      y += k;
    }
    y %= 2 * k;
    if (y >= k) {
      y -= k;
      x += k;
    }
    x %= 2 * k;
    s.add(x, y, 1);
    s.add((x + k) % (2 * k), y + k, 1);
  }
  // 二次元累積和
  s.build();
  // s.print();
  int maxv = 0;
  REP(x, k) REP(y, k) {
    int t = s.query(x, y, x + k, y + k);
    maxv = max({maxv, t, n - t});
  }
  cout << maxv << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)

// https://ei1333.github.io/algorithm/cumulative-sum2d.html
template <class T> struct CumulativeSum2D {
  T data[2001][2001];
  int w, h;

  CumulativeSum2D(int W, int H) {
    w = W;
    h = H;
  }

  void add(int x, int y, T z) {
    ++x, ++y;
    if (x >= w || y >= h)
      return;
    data[x][y] += z;
  }

  void build() {
    FOR(i, 1, w) FOR(j, 1, h) {
      data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];
    }
  }

  T query(int sx, int sy, int gx, int gy) {
    return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]);
  }

  void print() {
    for (auto &x : data) {
      for (auto &y : x) {
        cout << y << " ";
      }
      cout << endl;
    }
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  char c;
  int n, k, x, y;
  cin >> n >> k;
  CumulativeSum2D<int> s(k * 2, k * 2);
  REP(i, n) {
    cin >> x >> y >> c;
    if (c == 'W') {
      y += k;
    }
    y %= 2 * k;
    if (y >= k) {
      y -= k;
      x += k;
    }
    x %= 2 * k;
    s.add(x, y, 1);
    s.add((x + k) % (2 * k), y + k, 1);
  }
  // 二次元累積和
  s.build();
  // s.print();
  int maxv = 0;
  REP(x, k) REP(y, k) {
    int t = s.query(x, y, x + k, y + k);
    maxv = max({maxv, t, n - t});
  }
  cout << maxv << endl;
  return 0;
}
Problem p03458 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define KMAX (1000)
int s[KMAX + 1][2 * KMAX + 1];
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  char c;
  int n, k, x, y;
  cin >> n >> k;
  REP(i, n) {
    cin >> x >> y >> c;
    if (c == 'W') {
      y += k;
    }
    y %= 2 * k;
    x %= 2 * k;
    s[y + 1][x + 1]++;
  }
  // 二次元累積和
  REP(y, k) REP(x, 2 * k) {
    s[y + 1][x + 1] += s[y][x + 1] + s[y + 1][x] - s[y][x];
  }
  int max = 0;
  REP(y, k) {
    int u = s[y][2 * k] - s[y][0] - s[0][2 * k];
    REP(x, k) {
      int t = u - s[0][x] - s[k][x] + s[0][x + k] + s[k][x + k] + 2 * s[y][x] -
              2 * s[y][x + k];
      t = t < n / 2 ? n - t : t;
      max = t > max ? t : max;
    }
  }
  cout << max << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define KMAX (1000)
int s[KMAX + 1][2 * KMAX + 1];
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  char c;
  int n, k, x, y;
  cin >> n >> k;
  REP(i, n) {
    cin >> x >> y >> c;
    if (c == 'W') {
      y += k;
    }
    y %= 2 * k;
    if (y >= k) {
      y -= k;
      x += k;
    }
    x %= 2 * k;
    s[y + 1][x + 1]++;
  }
  // 二次元累積和
  REP(y, k) REP(x, 2 * k) {
    s[y + 1][x + 1] += s[y][x + 1] + s[y + 1][x] - s[y][x];
  }
  int max = 0;
  REP(y, k) {
    int u = s[y][2 * k] - s[y][0] - s[0][2 * k];
    REP(x, k) {
      int t = u - s[0][x] - s[k][x] + s[0][x + k] + s[k][x + k] + 2 * s[y][x] -
              2 * s[y][x + k];
      t = t < n / 2 ? n - t : t;
      max = t > max ? t : max;
    }
  }
  cout << max << endl;
  return 0;
}
Problem p03458 (C++)

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/abc086/tasks/arc089_b
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define REP(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
const int MAX_N = 2001;
int r[MAX_N][MAX_N], s[MAX_N][MAX_N];

int query(int x1, int y1, int x2, int y2) {
  // return sum(x1<=x<=x2,y1<=y<=y2)
  if (x1 > x2 || y1 > y2)
    return 0;
  else
    return s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  REP(i, N) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    if (c == 'B')
      y += K;
    x %= 2 * K;
    y %= 2 * K;
    r[x + 1][y + 1]++;
  }
  // 2次元累積和
  REP(x, 1, 2 * K + 2)
  REP(y, 1, 2 * K + 2)
  s[x][y] = r[x][y] + s[x - 1][y] + s[x][y - 1] - s[x - 1][y - 1];

  int ans = 0;
  REP(x, 1, K + 1) REP(y, 1, K + 1) {
    int temp = query(x, y, x + K - 1, y + K - 1) +
               query(x + K, y + K, 2 * K, 2 * K) + query(1, 1, x - 1, y - 1) +
               query(x + K, 1, 2 * K, y - 1) + query(1, y + K, x - 1, 2 * K);
    ans = max(ans, max(temp, N - temp));
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
// https://atcoder.jp/contests/abc086/tasks/arc089_b
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define REP(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
const int MAX_N = 2003;
int r[MAX_N][MAX_N], s[MAX_N][MAX_N];

int query(int x1, int y1, int x2, int y2) {
  // return sum(x1<=x<=x2,y1<=y<=y2)
  if (x1 > x2 || y1 > y2)
    return 0;
  else
    return s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  REP(i, N) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    if (c == 'B')
      y += K;
    x %= 2 * K;
    y %= 2 * K;
    r[x + 1][y + 1]++;
  }
  // 2次元累積和
  REP(x, 1, 2 * K + 2)
  REP(y, 1, 2 * K + 2)
  s[x][y] = r[x][y] + s[x - 1][y] + s[x][y - 1] - s[x - 1][y - 1];

  int ans = 0;
  REP(x, 1, K + 1) REP(y, 1, K + 1) {
    int temp = query(x, y, x + K - 1, y + K - 1) +
               query(x + K, y + K, 2 * K, 2 * K) + query(1, 1, x - 1, y - 1) +
               query(x + K, 1, 2 * K, y - 1) + query(1, y + K, x - 1, 2 * K);
    ans = max(ans, max(temp, N - temp));
  }
  cout << ans << endl;

  return 0;
}
Problem p03458 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, k;
int imos[1000 * 4][1000 * 4];

int main() {

  // input
  cin >> n >> k;
  while (n--) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    if (c == 'W')
      x += k;
    x %= 2 * k;
    y %= 2 * k;

    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        int xx = x + k * i + 1;
        int yy = y + k * j * 2 + k * (i % 2) + 1;
        imos[xx][yy]++;
        if (xx + k < 4 * k) {
          imos[xx + k][yy]--;
        }
        if (yy + k < 4 * k) {
          imos[xx][yy + k]--;
        }
        if (xx + k < 4 * k && yy + k < 4 * k) {
          imos[xx + k][yy + k]++;
        }
      }
    }
    // for(int i=0;i<4*k;i++){
    // for(int j=0;j<4*k;j++){
    // printf("%2d ", imos[i][j]);
    //}
    // printf("\n");
    //}
    // printf("\n");
  }

  // debug
  // for(int i=0;i<4*k;i++){
  // for(int j=0;j<4*k;j++){
  // printf("%2d ", imos[i][j]);
  //}
  // printf("\n");
  //}

  // imos
  for (int i = 0; i < 4 * k; i++) {
    for (int j = 1; j < 4 * k; j++) {
      imos[i][j] += imos[i][j - 1];
    }
  }
  for (int i = 1; i < 4 * k; i++) {
    for (int j = 0; j < 4 * k; j++) {
      imos[i][j] += imos[i - 1][j];
    }
  }

  // debug
  // printf("\n");
  // for(int i=0;i<4*k;i++){
  // for(int j=0;j<4*k;j++){
  // printf("%2d ", imos[i][j]);
  //}
  // printf("\n");
  //}

  // output
  int ans = 0;
  for (int i = 0; i < 4 * k; i++) {
    for (int j = 0; j < 4 * k; j++) {
      ans = max(ans, imos[i][j]);
    }
  }
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int n, k;
int imos[1000 * 4][1000 * 4];

int main() {

  // input
  cin >> n >> k;
  while (n--) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    if (c == 'W')
      x += k;
    x %= 2 * k;
    y %= 2 * k;

    for (int i = x % k + 1; i < 4 * k; i += k) {
      for (int j = y % k + 1; j < 4 * k; j += k) {
        if ((i + j) % (2 * k) != (x + 1 + y + 1) % (2 * k))
          continue;
        if (i + k < 4 * k && j + k < 4 * k) {
          imos[i][j]++;
          imos[i + k][j]--;
          imos[i][j + k]--;
          imos[i + k][j + k]++;
        }
      }
    }
    // for(int i=0;i<4*k;i++){
    // for(int j=0;j<4*k;j++){
    // printf("%2d ", imos[i][j]);
    //}
    // printf("\n");
    //}
    // printf("\n");
  }

  // debug
  // for(int i=0;i<4*k;i++){
  // for(int j=0;j<4*k;j++){
  // printf("%2d ", imos[i][j]);
  //}
  // printf("\n");
  //}

  // imos
  for (int i = 0; i < 4 * k; i++) {
    for (int j = 1; j < 4 * k; j++) {
      imos[i][j] += imos[i][j - 1];
    }
  }
  for (int i = 1; i < 4 * k; i++) {
    for (int j = 0; j < 4 * k; j++) {
      imos[i][j] += imos[i - 1][j];
    }
  }

  // debug
  // printf("\n");
  // for(int i=0;i<4*k;i++){
  // for(int j=0;j<4*k;j++){
  // printf("%2d ", imos[i][j]);
  //}
  // printf("\n");
  //}

  // output
  int ans = 0;
  for (int i = 0; i < 4 * k; i++) {
    for (int j = 0; j < 4 * k; j++) {
      ans = max(ans, imos[i][j]);
    }
  }
  printf("%d\n", ans);

  return 0;
}
Problem p03458 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main(void) {
  long n, k;
  cin >> n >> k;
  vector<vector<long>> b(4 * k, vector<long>(4 * k));
  vector<vector<long>> w(4 * k, vector<long>(4 * k));

  for (long i = 0; i < n; i++) {
    long x, y;
    string c;
    cin >> x >> y >> c;
    x %= 2 * k, y %= 2 * k;
    x++, y++;
    (c[0] == 'B' ? b : w)[y][x]++;
    (c[0] == 'B' ? b : w)[y][x + 2 * k]++;
    (c[0] == 'B' ? b : w)[y + 2 * k][x]++;
    (c[0] == 'B' ? b : w)[y + 2 * k][x + 2 * k]++;
  }

  for (long i = 1; i < 4 * k; i++)
    for (long j = 1; j < 4 * k; j++)
      b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];

  for (long i = 1; i < 4 * k; i++)
    for (long j = 1; j < 4 * k; j++)
      w[i][j] += w[i - 1][j] + w[i][j - 1] - w[i - 1][j - 1];

  long r = 0;
  for (long i = 0; i < 2 * k; i++)
    for (long j = 0; j < 2 * k; j++) {
      long t = 0;
      t += b[i + k][j + k] - b[i + k][j] - b[i][j + k] + b[i][j];
      i += k, j += k;
      t += b[i + k][j + k] - b[i + k][j] - b[i][j + k] + b[i][j];

      i -= k;
      t += w[i + k][j + k] - w[i + k][j] - w[i][j + k] + w[i][j];
      i += k, j -= k;
      t += w[i + k][j + k] - w[i + k][j] - w[i][j + k] + w[i][j];
      i -= k;

      r = max(r, t);
    }
  cout << r << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main(void) {
  long n, k;
  cin >> n >> k;
  vector<vector<long>> b(4 * k + 1, vector<long>(4 * k + 1));
  vector<vector<long>> w(4 * k + 1, vector<long>(4 * k + 1));

  for (long i = 0; i < n; i++) {
    long x, y;
    string c;
    cin >> x >> y >> c;
    x %= 2 * k, y %= 2 * k;
    x++, y++;
    (c[0] == 'B' ? b : w)[y][x]++;
    (c[0] == 'B' ? b : w)[y][x + 2 * k]++;
    (c[0] == 'B' ? b : w)[y + 2 * k][x]++;
    (c[0] == 'B' ? b : w)[y + 2 * k][x + 2 * k]++;
  }

  for (long i = 1; i < 4 * k; i++)
    for (long j = 1; j < 4 * k; j++)
      b[i][j] += b[i - 1][j] + b[i][j - 1] - b[i - 1][j - 1];

  for (long i = 1; i < 4 * k; i++)
    for (long j = 1; j < 4 * k; j++)
      w[i][j] += w[i - 1][j] + w[i][j - 1] - w[i - 1][j - 1];

  long r = 0;
  for (long i = 0; i < 2 * k; i++)
    for (long j = 0; j < 2 * k; j++) {
      long t = 0;
      t += b[i + k][j + k] - b[i + k][j] - b[i][j + k] + b[i][j];
      i += k, j += k;
      t += b[i + k][j + k] - b[i + k][j] - b[i][j + k] + b[i][j];

      i -= k;
      t += w[i + k][j + k] - w[i + k][j] - w[i][j + k] + w[i][j];
      i += k, j -= k;
      t += w[i + k][j + k] - w[i + k][j] - w[i][j + k] + w[i][j];
      i -= k;

      r = max(r, t);
    }
  cout << r << endl;
}
Problem p03458 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>

using namespace std;

#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> w(k, vector<int>(k, 0)), b(k, vector<int>(k, 0));
  rip(i, n, 0) {
    int x, y;
    char c;
    scanf("%d%d%c", x, y, c);
    if (bool((x / k % 2) ^ (y / k % 2)) ^ (c == 'W')) {
      w[x % k][y % k]++;
    } else {
      b[x % k][y % k]++;
    }
  }
  int ans = 0, al = 0;
  vector<int> X(k, 0), sX(k, 0);
  rip(i, k, 0) {
    rip(j, k, 0) {
      sX[i] += w[i][j] + b[i][j];
      X[i] += w[i][j];
    }
  }
  rip(i, k, 0) {
    int now = 0;
    rip(j, k, 0) { now += X[j]; }
    rip(j, k, 0) {
      ans = max(ans, max(now, n - now));
      now += sX[j] - X[j] * 2;
    }
    rip(j, k, 0) { X[j] += b[j][i] - w[j][i]; }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>

using namespace std;

#define rip(i, n, s) for (int i = (s); i < (int)(n); i++)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> w(k, vector<int>(k, 0)), b(k, vector<int>(k, 0));
  rip(i, n, 0) {
    int x, y;
    char c;
    scanf("%d %d %c", &x, &y, &c);
    if (bool((x / k % 2) ^ (y / k % 2)) ^ (c == 'W')) {
      w[x % k][y % k]++;
    } else {
      b[x % k][y % k]++;
    }
  }
  int ans = 0, al = 0;
  vector<int> X(k, 0), sX(k, 0);
  rip(i, k, 0) {
    rip(j, k, 0) {
      sX[i] += w[i][j] + b[i][j];
      X[i] += w[i][j];
    }
  }
  rip(i, k, 0) {
    int now = 0;
    rip(j, k, 0) { now += X[j]; }
    rip(j, k, 0) {
      ans = max(ans, max(now, n - now));
      now += sX[j] - X[j] * 2;
    }
    rip(j, k, 0) { X[j] += b[j][i] - w[j][i]; }
  }
  cout << ans << endl;
}
Problem p03458 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>

#define FOR(i, to) for (int i = 0; i < (to); i++)
#define FORR(i, from) for (int i = from; i > -1; i--)

using namespace std;

int N, K;
int s[4002][4002];
int w[4002][4002];

int calc(int x, int y) {
  return w[x][y] - w[x - K][y] - w[x][y + K] + w[x - K][y + K];
}

int main() {
  cin >> N >> K;

  FOR(i, N) {
    int x, y;
    char c;
    cin >> x >> y >> c;

    if (c == 'W')
      s[(x + K) % (2 * K) + K][y % (2 * K) + K] += 1;
    else
      s[x % (2 * K) + K][y % (2 * K) + K] += 1;
  }

  FOR(y, 4 * K) FOR(x, 4 * K) {
    if (x == -K)
      w[x][y] = s[x][y];
    else
      w[x][y] = w[x - 1][y] + s[x][y];
  }

  FOR(x, 4 * K) FORR(y, 4 * K - 1) {
    if (y == 4 * K - 1)
      w[x][y] = w[x][y];
    else
      w[x][y] = w[x][y + 1] + w[x][y];
  }

  int ans = 0;
  int tmp = 0;

  for (int y = K; y < 2 * K; y++) {
    for (int x = 2 * K; x < 3 * K; x++) {
      tmp = calc(x, y) + calc(x - K, y - K) + calc(x + K, y - K) +
            calc(x + K, y + K) + calc(x - K, y + K);
      ans = max(ans, max(N - tmp, tmp));
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>

#define FOR(i, to) for (int i = 0; i < (to); i++)
#define FORR(i, from) for (int i = from; i > -1; i--)

using namespace std;

int N, K;
int s[4002][4002];
int w[4002][4002];

int calc(int x, int y) {
  return w[x][y] - w[x - K][y] - w[x][y + K] + w[x - K][y + K];
}

int main() {
  cin >> N >> K;

  FOR(i, N) {
    int x, y;
    char c;
    cin >> x >> y >> c;

    if (c == 'W')
      s[(x + K) % (2 * K) + K][y % (2 * K) + K] += 1;
    else
      s[x % (2 * K) + K][y % (2 * K) + K] += 1;
  }

  FOR(y, 4 * K) FOR(x, 4 * K) {
    if (x == 0)
      w[x][y] = s[x][y];
    else
      w[x][y] = w[x - 1][y] + s[x][y];
  }

  FOR(x, 4 * K) FORR(y, 4 * K - 1) {
    if (y == 4 * K - 1)
      w[x][y] = w[x][y];
    else
      w[x][y] = w[x][y + 1] + w[x][y];
  }

  int ans = 0;
  int tmp = 0;

  for (int y = K; y < 2 * K; y++) {
    for (int x = 2 * K; x < 3 * K; x++) {
      tmp = calc(x, y) + calc(x - K, y - K) + calc(x + K, y - K) +
            calc(x + K, y + K) + calc(x - K, y + K);
      ans = max(ans, max(N - tmp, tmp));
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p03458 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define int long long
using namespace std;

typedef pair<int, int> Pii;

template <typename T> void readvec(vector<T> &a);
void readindex(vector<int> &a);

void add_b(vector<vector<int>> &b, int x0, int y0, int K) {
  b[x0][y0]++;
  if (x0 < 3 * K)
    b[x0 + K][y0]--;
  if (y0 < 3 * K)
    b[x0][y0 + K]--;
  if (x0 < 3 * K && y0 < 3 * K)
    b[x0 + K][y0 + K]++;
}

signed main() {

  int N, K;
  cin >> N >> K;

  vector<vector<int>> b(4 * K, vector<int>(4 * K, 0)),
      s(3 * K, vector<int>(4 * K, 0));

  int x, y, x0, y0;
  char c;
  REP(i, N) {
    cin >> x >> y >> c;
    if (c == 'W')
      x += K;

    x0 = x % (2 * K);
    y0 = y % (2 * K);
    REP(p, 4) {
      REP(q, 4) {
        if ((p + q) % 2 == 0)
          add_b(b, (x0 + p * K) % (4 * K), (y0 + q * K) % (4 * K), K);
      }
    }
  }

  s[0][0] = b[0][0];
  int ans = s[0][0];

  FOR(j, 1, 4 * K) {
    s[0][j] = s[0][j - 1] + b[0][j];
    ans = max(ans, s[0][j]);
  }
  FOR(i, 1, 4 * K) {
    int tmp = 0;
    FOR(j, 0, 4 * K) {
      tmp += b[i][j];
      s[i][j] = s[i - 1][j] + tmp;
      ans = max(ans, s[i][j]);
    }
  }

  cout << ans;

  return 0;
}

template <typename T> void readvec(vector<T> &a) {
  REP(i, a.size()) { cin >> a[i]; }
}
void readindex(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define int long long
using namespace std;

typedef pair<int, int> Pii;

template <typename T> void readvec(vector<T> &a);
void readindex(vector<int> &a);

void add_b(vector<vector<int>> &b, int x0, int y0, int K) {
  b[x0][y0]++;
  if (x0 < 3 * K)
    b[x0 + K][y0]--;
  if (y0 < 3 * K)
    b[x0][y0 + K]--;
  if (x0 < 3 * K && y0 < 3 * K)
    b[x0 + K][y0 + K]++;
}

signed main() {

  int N, K;
  cin >> N >> K;

  vector<vector<int>> b(4 * K, vector<int>(4 * K, 0)),
      s(4 * K, vector<int>(4 * K, 0));

  int x, y, x0, y0;
  char c;
  REP(i, N) {
    cin >> x >> y >> c;
    if (c == 'W')
      x += K;

    x0 = x % (2 * K);
    y0 = y % (2 * K);
    REP(p, 4) {
      REP(q, 4) {
        if ((p + q) % 2 == 0)
          add_b(b, (x0 + p * K) % (4 * K), (y0 + q * K) % (4 * K), K);
      }
    }
  }

  s[0][0] = b[0][0];
  int ans = s[0][0];

  FOR(j, 1, 4 * K) {
    s[0][j] = s[0][j - 1] + b[0][j];
    ans = max(ans, s[0][j]);
  }
  FOR(i, 1, 4 * K) {
    int tmp = 0;
    FOR(j, 0, 4 * K) {
      tmp += b[i][j];
      s[i][j] = s[i - 1][j] + tmp;
      ans = max(ans, s[i][j]);
    }
  }

  cout << ans;

  return 0;
}

template <typename T> void readvec(vector<T> &a) {
  REP(i, a.size()) { cin >> a[i]; }
}
void readindex(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}
Problem p03459 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <string>
#define ll long long
#define INF 0x3f3f3f3f
#define N 1005
using namespace std;

struct node {
  int t, x, y;
  bool operator<(node a) { return t < a.t; }
} a[N];

int main() {
  ll n, t, x, y;
  bool flag = true;
  cin >> n;
  a[0].t = a[0].x = a[0].y = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].t >> a[i].x >> a[i].y;
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    int lenx = a[i].x - a[i - 1].x;
    if (lenx < 0)
      lenx = -lenx;
    int leny = a[i].y - a[i - 1].y;
    if (leny < 0)
      leny = -leny;
    int len = lenx + leny;
    int dt = a[i].t - a[i - 1].t;
    if (dt < len) {
      flag = false;
      break;
    }
    if (len & 1) {
      if (dt % 2 == 0) {
        flag = false;
        break;
      }
    } else {
      if (dt & 1) {
        flag = false;
        break;
      }
    }
  }
  if (flag)
    puts("Yes");
  else
    puts("No");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <string>
#define ll long long
#define INF 0x3f3f3f3f
#define N 100005
using namespace std;

struct node {
  int t, x, y;
  bool operator<(node a) { return t < a.t; }
} a[N];

int main() {
  ll n, t, x, y;
  bool flag = true;
  cin >> n;
  a[0].t = a[0].x = a[0].y = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].t >> a[i].x >> a[i].y;
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) {
    int lenx = a[i].x - a[i - 1].x;
    if (lenx < 0)
      lenx = -lenx;
    int leny = a[i].y - a[i - 1].y;
    if (leny < 0)
      leny = -leny;
    int len = lenx + leny;
    int dt = a[i].t - a[i - 1].t;
    if (dt < len) {
      flag = false;
      break;
    }
    if (len & 1) {
      if (dt % 2 == 0) {
        flag = false;
        break;
      }
    } else {
      if (dt & 1) {
        flag = false;
        break;
      }
    }
  }
  if (flag)
    puts("Yes");
  else
    puts("No");
  return 0;
}
Problem p03459 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;

int N;
int t[10101], x[10101], y[10101];
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> t[i] >> x[i] >> y[i];
  }
  int t1 = 0, x1 = 0, y1 = 0;
  for (int i = 0; i < N; i++) {
    int t2, x2, y2;
    t2 = t[i];
    x2 = x[i];
    y2 = y[i];
    long long int d = abs(x2 - x1) + abs(y2 - y1);
    if (d <= (t2 - t1) && ((t2 - t1) % 2 == d % 2)) {
      t1 = t2;
      x1 = x2;
      y1 = y2;
    } else {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  return 0;
}


Fixed submission:
#include <cstdlib>
#include <iostream>
#include <stdio.h>
using namespace std;

int N;
int t[101010], x[101010], y[101010];
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> t[i] >> x[i] >> y[i];
  }
  int t1 = 0, x1 = 0, y1 = 0;
  for (int i = 0; i < N; i++) {
    int t2, x2, y2;
    t2 = t[i];
    x2 = x[i];
    y2 = y[i];
    long long int d = abs(x2 - x1) + abs(y2 - y1);
    if (d <= (t2 - t1) && ((t2 - t1) % 2 == d % 2)) {
      t1 = t2;
      x1 = x2;
      y1 = y2;
    } else {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  return 0;
}
Problem p03459 (C++)

Buggy submission (status: Runtime Error):
#include "math.h"
#include <algorithm>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define VI vector<int>
#define debug(x) cout << #x << ": " << x << endl
#define DEBUG(x) cout << #x << ": " << x << endl
int N;
vector<int> t, x, y;

int main() {

  cin >> N;

  t.resize(N);
  x.resize(N);
  y.resize(N);

  FOR(i, 1, N + 1) {
    cin >> t[i];
    cin >> x[i];
    cin >> y[i];
  }

  t[0] = 0;
  x[0] = 0;
  y[0] = 0;

  FOR(i, 1, N + 1) {
    int l = abs(x[i] - x[i - 1]) + abs(y[i] - y[i - 1]);
    if (t[i] - t[i - 1] < l) {
      cout << "No" << endl;
      return 0;
    } else {
      if ((t[i] - t[i - 1] - l) % 2 == 1) {
        cout << "No" << endl;
        return 0;
      }
    }
  }

  cout << "Yes" << endl;
}


Fixed submission:
#include "math.h"
#include <algorithm>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define VI vector<int>
#define debug(x) cout << #x << ": " << x << endl
#define DEBUG(x) cout << #x << ": " << x << endl
int N;
vector<int> t, x, y;

int main() {

  cin >> N;

  t.resize(N + 1);
  x.resize(N + 1);
  y.resize(N + 1);

  FOR(i, 1, N + 1) {
    cin >> t[i];
    cin >> x[i];
    cin >> y[i];
  }

  t[0] = 0;
  x[0] = 0;
  y[0] = 0;

  FOR(i, 1, N + 1) {
    int l = abs(x[i] - x[i - 1]) + abs(y[i] - y[i - 1]);
    if (t[i] - t[i - 1] < l) {
      cout << "No" << endl;
      return 0;
    } else {
      if ((t[i] - t[i - 1] - l) % 2 == 1) {
        cout << "No" << endl;
        return 0;
      }
    }
  }

  cout << "Yes" << endl;
}
Problem p03459 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 1e9
#define MAX_N 10002
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int main() {
  int N;
  int t[MAX_N], x[MAX_N], y[MAX_N];
  int dt[MAX_N], dx[MAX_N], dy[MAX_N];
  t[0] = x[0] = y[0] = 0;
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> t[i] >> x[i] >> y[i];
    dt[i] = t[i] - t[i - 1];
    dx[i] = abs(x[i] - x[i - 1]);
    dy[i] = abs(y[i] - y[i - 1]);
  }

  bool flag = true;
  for (int i = 1; i <= N; ++i) {
    int k = dt[i] - (dx[i] + dy[i]);
    if (k < 0 || k % 2 == 1) {
      flag = false;
      break;
    }
  }

  if (flag) {
    cout << "Yes\n";
  } else {
    cout << "No\n";
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 1e9
#define MAX_N 100002
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int main() {
  int N;
  int t[MAX_N], x[MAX_N], y[MAX_N];
  int dt[MAX_N], dx[MAX_N], dy[MAX_N];
  t[0] = x[0] = y[0] = 0;
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> t[i] >> x[i] >> y[i];
    dt[i] = t[i] - t[i - 1];
    dx[i] = abs(x[i] - x[i - 1]);
    dy[i] = abs(y[i] - y[i - 1]);
  }

  bool flag = true;
  for (int i = 1; i <= N; ++i) {
    int k = dt[i] - (dx[i] + dy[i]);
    if (k < 0 || k % 2 == 1) {
      flag = false;
      break;
    }
  }

  if (flag) {
    cout << "Yes\n";
  } else {
    cout << "No\n";
  }

  return 0;
}
Problem p03459 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(c) begin(c), end(c)
#define dump(x) cerr << __LINE__ << ":\t" #x " = " << (x) << endl

class bipartite_matching {
public:
  int n;
  std::vector<std::vector<int>> g;
  std::vector<int> match;

  bipartite_matching(int n_) : n(n_), g(n_), match(n_), used(n_) {}

  void add_edge(int u, int v) {
    g[u].push_back(v);
    g[v].push_back(u);
  }

  int maximum_matching(void) {
    int res = 0;
    fill(begin(match), end(match), -1);
    for (int v = 0; v < n; ++v) {
      if (match[v] == -1) {
        fill(begin(used), end(used), false);
        if (dfs(v))
          res++;
      }
    }
    return res;
  }

private:
  std::vector<int> used;
  bool dfs(int v) {
    used[v] = true;
    for (int u : g[v]) {
      int w = match[u];
      if (w == -1 || (!used[w] && dfs(w))) {
        match[v] = u;
        match[u] = v;
        return true;
      }
    }
    return false;
  }
};

int main() {
  int n;
  while (cin >> n) {
    vector<int> t(n), x(n), y(n);
    rep(i, n) cin >> t[i + 1] >> x[i + 1] >> y[i + 1];
    bool ok = true;
    rep(i, n) {
      int dx = abs(x[i] - x[i + 1]);
      int dy = abs(y[i] - y[i + 1]);
      int d = dx + dy;
      int dt = t[i + 1] - t[i];
      if (d % 2 == dt % 2 && d <= dt)
        ;
      else
        ok = false;
    }
    cout << (ok ? "Yes" : "No") << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(c) begin(c), end(c)
#define dump(x) cerr << __LINE__ << ":\t" #x " = " << (x) << endl

class bipartite_matching {
public:
  int n;
  std::vector<std::vector<int>> g;
  std::vector<int> match;

  bipartite_matching(int n_) : n(n_), g(n_), match(n_), used(n_) {}

  void add_edge(int u, int v) {
    g[u].push_back(v);
    g[v].push_back(u);
  }

  int maximum_matching(void) {
    int res = 0;
    fill(begin(match), end(match), -1);
    for (int v = 0; v < n; ++v) {
      if (match[v] == -1) {
        fill(begin(used), end(used), false);
        if (dfs(v))
          res++;
      }
    }
    return res;
  }

private:
  std::vector<int> used;
  bool dfs(int v) {
    used[v] = true;
    for (int u : g[v]) {
      int w = match[u];
      if (w == -1 || (!used[w] && dfs(w))) {
        match[v] = u;
        match[u] = v;
        return true;
      }
    }
    return false;
  }
};

int main() {
  int n;
  while (cin >> n) {
    vector<int> t(n + 1), x(n + 1), y(n + 1);
    rep(i, n) cin >> t[i + 1] >> x[i + 1] >> y[i + 1];
    bool ok = true;
    rep(i, n) {
      int dx = abs(x[i] - x[i + 1]);
      int dy = abs(y[i] - y[i + 1]);
      int d = dx + dy;
      int dt = t[i + 1] - t[i];
      if (d % 2 == dt % 2 && d <= dt)
        ;
      else
        ok = false;
    }
    cout << (ok ? "Yes" : "No") << endl;
  }
}