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

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

int sum[5000][5000];
int X[5000], Y[5000];
int X1[500000], X2[500000], Y1[500000], Y2[500000];
int n, m;

int compress(int *x, int *X1, int *X2) {
  vector<int> v;
  rep(i, n) v.push_back(x[i]);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  rep(i, m) {
    X1[i] = lower_bound(v.begin(), v.end(), X1[i]) - v.begin();
    X2[i] = upper_bound(v.begin(), v.end(), X2[i]) - v.begin() - 1;
  }
  rep(i, n) { x[i] = lower_bound(v.begin(), v.end(), x[i]) - v.begin(); }
  return (int)v.size();
}
signed main() {
  scanf("%d%d", &n, &m);
  rep(i, n) scanf("%d%d", &X[i], &Y[i]);
  rep(i, m) scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
  int h = compress(X, X1, X2), w = compress(Y, Y1, Y2);
  rep(i, n) sum[X[i]][Y[i]]++;
  rep(i, h + 1) for (int j = 1; j <= w; j++) { sum[i][j] += sum[i][j - 1]; }
  for (int i = 1; i <= h; i++)
    rep(j, w + 1) { sum[i][j] += sum[i - 1][j]; }
  rep(i, m) {
    if (X2[i] < 0 || Y2[i] < 0) {
      puts("0");
      continue;
    }
    int ans = sum[X2[i]][Y2[i]];
    if (X1[i] > 0)
      ans -= sum[X1[i] - 1][Y2[i]];
    if (Y1[i] > 0)
      ans -= sum[X2[i]][Y1[i] - 1];
    if (X1[i] > 0 && Y1[i] > 0)
      ans += sum[X1[i] - 1][Y1[i] - 1];
    printf("%d\n", ans);
  }
}

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

int sum[6000][6000];
int X[6000], Y[6000];
int X1[600000], X2[600000], Y1[600000], Y2[600000];
int n, m;

int compress(int *x, int *X1, int *X2) {
  vector<int> v;
  rep(i, n) v.push_back(x[i]);
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  rep(i, m) {
    X1[i] = lower_bound(v.begin(), v.end(), X1[i]) - v.begin();
    X2[i] = upper_bound(v.begin(), v.end(), X2[i]) - v.begin() - 1;
  }
  rep(i, n) { x[i] = lower_bound(v.begin(), v.end(), x[i]) - v.begin(); }
  return (int)v.size();
}
signed main() {
  scanf("%d%d", &n, &m);
  rep(i, n) scanf("%d%d", &X[i], &Y[i]);
  rep(i, m) scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
  int h = compress(X, X1, X2), w = compress(Y, Y1, Y2);
  rep(i, n) sum[X[i]][Y[i]]++;
  rep(i, h + 1) for (int j = 1; j <= w; j++) { sum[i][j] += sum[i][j - 1]; }
  for (int i = 1; i <= h; i++)
    rep(j, w + 1) { sum[i][j] += sum[i - 1][j]; }
  rep(i, m) {
    if (X2[i] < 0 || Y2[i] < 0) {
      puts("0");
      continue;
    }
    int ans = sum[X2[i]][Y2[i]];
    if (X1[i] > 0)
      ans -= sum[X1[i] - 1][Y2[i]];
    if (Y1[i] > 0)
      ans -= sum[X2[i]][Y1[i] - 1];
    if (X1[i] > 0 && Y1[i] > 0)
      ans += sum[X1[i] - 1][Y1[i] - 1];
    printf("%d\n", ans);
  }
}
Problem p01540 (C++)

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

P p[5000];
vector<int> dat[10000];
void init(int k, int l, int r) {
  if (r - l == 1) {
    dat[k].push_back(p[l].second);
    return;
  }
  int lb = k * 2 + 1, rb = k * 2 + 2;
  init(lb, l, (l + r) / 2);
  init(rb, (l + r) / 2, r);
  dat[k].resize(r - l);
  merge(dat[lb].begin(), dat[lb].end(), dat[rb].begin(), dat[rb].end(),
        dat[k].begin());
}
int query(int a, int b, int c, int d, int k, int l, int r) {
  if (b <= l || r <= a)
    return 0;
  if (a <= l && r <= b) {
    int u = upper_bound(dat[k].begin(), dat[k].end(), d) - dat[k].begin();
    int v = upper_bound(dat[k].begin(), dat[k].end(), c - 1) - dat[k].begin();
    return u - v;
  }
  int lb = query(a, b, c, d, k * 2 + 1, l, (l + r) / 2);
  int rb = query(a, b, c, d, k * 2 + 2, (l + r) / 2, r);
  return lb + rb;
}
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  rep(i, n) scanf("%d%d", &p[i].first, &p[i].second);
  sort(p, p + n);
  init(0, 0, n);
  rep(i, q) {
    int sx, tx, sy, ty;
    scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
    int a = lower_bound(p, p + n, P(sx, INT_MIN)) - p;
    int b = lower_bound(p, p + n, P(tx, INT_MAX)) - p;
    printf("%d\n", query(a, b, sy, ty, 0, 0, n));
  }
}

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

P p[5000];
vector<int> dat[50000];
void init(int k, int l, int r) {
  if (r - l == 1) {
    dat[k].push_back(p[l].second);
    return;
  }
  int lb = k * 2 + 1, rb = k * 2 + 2;
  init(lb, l, (l + r) / 2);
  init(rb, (l + r) / 2, r);
  dat[k].resize(r - l);
  merge(dat[lb].begin(), dat[lb].end(), dat[rb].begin(), dat[rb].end(),
        dat[k].begin());
}
int query(int a, int b, int c, int d, int k, int l, int r) {
  if (b <= l || r <= a)
    return 0;
  if (a <= l && r <= b) {
    int u = upper_bound(dat[k].begin(), dat[k].end(), d) - dat[k].begin();
    int v = upper_bound(dat[k].begin(), dat[k].end(), c - 1) - dat[k].begin();
    return u - v;
  }
  int lb = query(a, b, c, d, k * 2 + 1, l, (l + r) / 2);
  int rb = query(a, b, c, d, k * 2 + 2, (l + r) / 2, r);
  return lb + rb;
}
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  rep(i, n) scanf("%d%d", &p[i].first, &p[i].second);
  sort(p, p + n);
  init(0, 0, n);
  rep(i, q) {
    int sx, tx, sy, ty;
    scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
    int a = lower_bound(p, p + n, P(sx, INT_MIN)) - p;
    int b = lower_bound(p, p + n, P(tx, INT_MAX)) - p;
    printf("%d\n", query(a, b, sy, ty, 0, 0, n));
  }
}
Problem p01540 (C++)

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

int sum[5100][5100];
signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(n), y(n);
  vector<pii> p(n);
  REP(i, n) {
    cin >> p[i].first >> p[i].second;
    x[i] = p[i].first;
    y[i] = p[i].second;
  }
  sort(ALL(x));
  x.erase(unique(ALL(x)), end(x));
  sort(ALL(y));
  y.erase(unique(ALL(y)), end(y));

  auto get_x = [&](int i) {
    return distance(begin(x), lower_bound(ALL(x), i));
  };
  auto get_y = [&](int i) {
    return distance(begin(y), lower_bound(ALL(y), i));
  };

  REP(i, n) {
    int xx = get_x(p[i].first);
    int yy = get_y(p[i].second);
    sum[xx][yy]++;
  }
  REP(i, 5100) REPF(j, 1, 5100) sum[i][j] += sum[i][j - 1];
  REP(j, 5100) REPF(i, 1, 5100) sum[i][j] += sum[i - 1][j];

  REP(i, m) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int xx1 = get_x(x1);
    int yy1 = get_y(y1);
    int xx2 = get_x(x2);
    int yy2 = get_y(y2);
    if (find(ALL(x), x2) == end(x))
      xx2--;
    if (find(ALL(y), y2) == end(y))
      yy2--;

    int ans = sum[xx2][yy2];
    if (xx1 > 0) {
      ans -= sum[xx1 - 1][yy2];
    }
    if (yy1 > 0) {
      ans -= sum[xx2][yy1 - 1];
    }
    if (xx1 > 0 && yy1 > 0) {
      ans += sum[xx1 - 1][yy1 - 1];
    }
    cout << ans << endl;
  }
  return 0;
}

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

int sum[5100][5100];
signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(n), y(n);
  vector<pii> p(n);
  REP(i, n) {
    cin >> p[i].first >> p[i].second;
    x[i] = p[i].first;
    y[i] = p[i].second;
  }
  sort(ALL(x));
  x.erase(unique(ALL(x)), end(x));
  sort(ALL(y));
  y.erase(unique(ALL(y)), end(y));

  auto get_x = [&](int i) {
    return distance(begin(x), lower_bound(ALL(x), i));
  };
  auto get_y = [&](int i) {
    return distance(begin(y), lower_bound(ALL(y), i));
  };

  REP(i, n) {
    int xx = get_x(p[i].first);
    int yy = get_y(p[i].second);
    sum[xx][yy]++;
  }
  REP(i, 5100) REPF(j, 1, 5100) sum[i][j] += sum[i][j - 1];
  REP(j, 5100) REPF(i, 1, 5100) sum[i][j] += sum[i - 1][j];

  REP(i, m) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int xx1 = get_x(x1);
    int yy1 = get_y(y1);
    int xx2 = get_x(x2);
    int yy2 = get_y(y2);
    if (find(ALL(x), x2) == end(x))
      xx2--;
    if (find(ALL(y), y2) == end(y))
      yy2--;

    if (xx2 < xx1 || yy2 < yy1) {
      cout << 0 << endl;
      continue;
    }

    int ans = sum[xx2][yy2];
    if (xx1 > 0) {
      ans -= sum[xx1 - 1][yy2];
    }
    if (yy1 > 0) {
      ans -= sum[xx2][yy1 - 1];
    }
    if (xx1 > 0 && yy1 > 0) {
      ans += sum[xx1 - 1][yy1 - 1];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01544 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
using namespace std;
int n, c, e, ans[4009], pre[4009], dp[4009], ps[4009][4009], l[8000009],
    r[8000009], p[8000009];
long long s[4009];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &s[i]), s[i] += s[i - 1];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      l[c] = i, r[c] = j, p[c] = c, c++;
    }
  }
  sort(p, p + c, [](int i, int j) {
    return s[r[i]] - s[l[i]] != s[r[j]] - s[l[j]]
               ? s[r[i]] - s[l[i]] < s[r[j]] - s[l[j]]
               : i > j;
  });
  fill(dp + 1, dp + n + 1, -(1 << 30));
  for (int i = 0; i < c; i++) {
    if (dp[r[p[i]]] < dp[l[p[i]]] + 1) {
      dp[r[p[i]]] = dp[l[p[i]]] + 1;
      ps[r[p[i]]][l[p[i]]] = pre[l[p[i]]];
      pre[r[p[i]]] = l[p[i]];
    }
  }
  printf("%d\n", dp[n]);
  int p1 = n, p2 = pre[n];
  while (p2 != 0) {
    ans[e++] = p2;
    swap(p1, p2);
    p2 = ps[p2][p1];
  }
  for (int i = e - 1; i >= 0; i--)
    printf(i == 0 ? "%d\n" : "%d ", ans[i]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
int n, c, e, ans[4009], pre[4009], dp[4009], ps[4009][4009], l[8002009],
    r[8002009], p[8002009];
long long s[4009];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &s[i]), s[i] += s[i - 1];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      l[c] = i, r[c] = j, p[c] = c, c++;
    }
  }
  sort(p, p + c, [](int i, int j) {
    return s[r[i]] - s[l[i]] != s[r[j]] - s[l[j]]
               ? s[r[i]] - s[l[i]] < s[r[j]] - s[l[j]]
               : i > j;
  });
  fill(dp + 1, dp + n + 1, -(1 << 30));
  for (int i = 0; i < c; i++) {
    if (dp[r[p[i]]] < dp[l[p[i]]] + 1) {
      dp[r[p[i]]] = dp[l[p[i]]] + 1;
      ps[r[p[i]]][l[p[i]]] = pre[l[p[i]]];
      pre[r[p[i]]] = l[p[i]];
    }
  }
  printf("%d\n", dp[n]);
  int p1 = n, p2 = pre[n];
  while (p2 != 0) {
    ans[e++] = p2;
    swap(p1, p2);
    p2 = ps[p2][p1];
  }
  for (int i = e - 1; i >= 0; i--)
    printf(i == 0 ? "%d\n" : "%d ", ans[i]);
  return 0;
}
Problem p01545 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define PB push_back
#define FI first
#define SE second
using namespace std;
static const int MAX_N = 1 << 17;
static const int inf = 1ll << 6;
typedef pair<int, int> PII;

int N;
vector<PII> V;

class Segment {
public:
  int dp[MAX_N];
  int n;

  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; ++i)
      dp[i] = -inf;
  }

  void update(int k, int a) {
    k += n - 1;
    dp[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dp[k] = max(dp[2 * k + 1], dp[2 * k + 2]);
    }
  }

  int query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return -inf;
    if (a <= l && r <= b)
      return dp[k];
    else {
      int vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
      int vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
      return max(vl, vr);
    }
  }
};

int res;

signed main() {
  cin >> N;
  Segment seg;
  seg.init(N);
  int MAX = N * (N + 1) / 2;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    V.PB(PII(a, -i));
  }
  sort(V.begin(), V.end());
  for (int i = 0; i < N; ++i) {
    int j = -V[i].SE;
    seg.update(j, V[i].FI);
    seg.update(
        j, max(seg.dp[j + seg.n - 1], seg.query(0, j, 0, 0, seg.n) + V[i].FI));
  }
  cout << MAX - seg.dp[0] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define PB push_back
#define FI first
#define SE second
using namespace std;
static const int MAX_N = 1 << 17;
static const int inf = 1ll << 6;
typedef pair<int, int> PII;

int N;
vector<PII> V;

class Segment {
public:
  int dp[2 * MAX_N];
  int n;

  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    for (int i = 0; i < 2 * n - 1; ++i)
      dp[i] = -inf;
  }

  void update(int k, int a) {
    k += n - 1;
    dp[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dp[k] = max(dp[2 * k + 1], dp[2 * k + 2]);
    }
  }

  int query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return -inf;
    if (a <= l && r <= b)
      return dp[k];
    else {
      int vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
      int vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
      return max(vl, vr);
    }
  }
};

int res;

signed main() {
  cin >> N;
  Segment seg;
  seg.init(N);
  int MAX = N * (N + 1) / 2;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    V.PB(PII(a, -i));
  }
  sort(V.begin(), V.end());
  for (int i = 0; i < N; ++i) {
    int j = -V[i].SE;
    seg.update(j, V[i].FI);
    seg.update(
        j, max(seg.dp[j + seg.n - 1], seg.query(0, j, 0, 0, seg.n) + V[i].FI));
  }
  cout << MAX - seg.dp[0] << endl;
}
Problem p01545 (C++)

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

const int MAX_N = 100000;

typedef long long ll;

using namespace std;

ll n, dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
}

void update(int k, ll a) {
  k += n - 1;
  dat[k] = a;

  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = max(dat[2 * k + 1], dat[2 * k + 2]);
  }
}

ll query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  else {
    ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
}

int main(void) {

  ll in, m, ans = 0;

  cin >> m;

  init(m);

  for (int i = 0; i < m; i++) {
    cin >> in;
    update(in, in + query(1, in + 1, 0, 0, n));
    // ans=max(ans,dat[in+n-1]);
  }
  cout << m * (m + 1) / 2 - query(1, m + 1, 0, 0, n) << endl;

  return 0;
}

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

const int MAX_N = 1000000;

typedef long long ll;

using namespace std;

ll n, dat[2 * MAX_N - 1];

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
}

void update(int k, ll a) {
  k += n - 1;
  dat[k] = a;

  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = max(dat[2 * k + 1], dat[2 * k + 2]);
  }
}

ll query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  else {
    ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
}

int main(void) {

  ll in, m, ans = 0;

  cin >> m;

  init(m);

  for (int i = 0; i < m; i++) {
    cin >> in;
    update(in, in + query(1, in + 1, 0, 0, n));
    // ans=max(ans,dat[in+n-1]);
  }
  cout << m * (m + 1) / 2 - query(1, m + 1, 0, 0, n) << endl;

  return 0;
}
Problem p01549 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned char uc;
int main() {
  int m;
  cin >> m;
  int l[m], r[m];
  map<string, int> ms;
  for (int i = 0; i < m; i++) {
    string s;
    cin >> s >> l[i] >> r[i];
    ms[s] = i;
  }
  int n;
  cin >> n;
  string ss[n];
  for (int i = 0; i < n; i++)
    cin >> ss[i];
  bool f = 0;
  time_t start = clock();
  srand((unsigned)time(NULL));
  while ((double)(clock() - start) / CLOCKS_PER_SEC < 1.7 && !f) {
    stack<uc> sc;
    for (int i = 0; !f && i < n; i++) {
      // cout<<i<<":"<<ss[i]<<endl;
      if (ms.find(ss[i]) != ms.end()) {
        int j = ms[ss[i]], k = (l[j] + (rand() % (r[j] - l[j])));
        // cout<<ss[i]<<":"<<k<<endl;
        sc.push(k);
      } else if (isdigit(ss[i][0])) {
        uc a = 0;
        for (int j = 0; j < (int)ss[i].size(); j++)
          a = (uc)(a * 10 + ss[i][j] - '0');
        sc.push(a);
      } else {
        uc a, b;
        b = sc.top();
        sc.pop();
        a = sc.top();
        sc.pop();
        if (ss[i] == "+")
          sc.push(a + b);
        if (ss[i] == "-")
          sc.push(a - b);
        if (ss[i] == "*")
          sc.push(a * b);
        if (ss[i] == "/") {
          if (b)
            sc.push(a / b);
          else {
            f = 1;
            break;
          }
        }
      }
    }
  }
  cout << (!f ? "correct" : "error") << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned char uc;
int main() {
  int m;
  cin >> m;
  int l[m], r[m];
  map<string, int> ms;
  for (int i = 0; i < m; i++) {
    string s;
    cin >> s >> l[i] >> r[i];
    ms[s] = i;
  }
  int n;
  cin >> n;
  string ss[n];
  for (int i = 0; i < n; i++)
    cin >> ss[i];
  bool f = 0;
  time_t start = clock();
  srand((unsigned)time(NULL));
  while ((double)(clock() - start) / CLOCKS_PER_SEC < 1.7 && !f) {
    stack<uc> sc;
    for (int i = 0; !f && i < n; i++) {
      // cout<<i<<":"<<ss[i]<<endl;
      if (ms.find(ss[i]) != ms.end()) {
        int j = ms[ss[i]], k = l[j];
        if (r[j] != l[j])
          k += (rand() % (r[j] - l[j]));
        // cout<<ss[i]<<":"<<k<<endl;
        sc.push(k);
      } else if (isdigit(ss[i][0])) {
        uc a = 0;
        for (int j = 0; j < (int)ss[i].size(); j++)
          a = (uc)(a * 10 + ss[i][j] - '0');
        sc.push(a);
      } else {
        uc a, b;
        b = sc.top();
        sc.pop();
        a = sc.top();
        sc.pop();
        if (ss[i] == "+")
          sc.push(a + b);
        if (ss[i] == "-")
          sc.push(a - b);
        if (ss[i] == "*")
          sc.push(a * b);
        if (ss[i] == "/") {
          if (b)
            sc.push(a / b);
          else {
            f = 1;
            break;
          }
        }
      }
    }
  }
  cout << (!f ? "correct" : "error") << endl;
  return 0;
}
Problem p01553 (C++)

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

using namespace std;

#define MOD 1000000007

typedef long long ll;

ll dp[201][201][201] = {0}; // dp[n][rest U][rest empty box] = ??´????????°

int main() {
  int N;
  cin >> N;
  vector<int> v;
  for (int i = 0; i < N; ++i) {
    char c;
    cin >> c;
    if (c != '-') {
      v.push_back(c);
    }
  }
  N = v.size();

  if (v[0] == 'D') {
    cout << 0 << endl;
    return 0;
  }

  dp[0][1][1] = 1; // v[0] = 'U'
  for (int i = 0; i < N - 1; ++i) {
    for (int j = 0; j < N; ++j) {
      for (int k = 0; k < N; ++k) {
        if (v[i + 1] == 'U') {
          dp[i + 1][j + 1][k + 1] =
              (dp[i + 1][j + 1][k + 1] + dp[i][j][k]) % MOD;
          if (j >= 1) {
            dp[i + 1][j][k] = (dp[i + 1][j][k] + j * dp[i][j][k]) % MOD;
          }
        } else { // D
          if (k >= 1) {
            //						if (dp[i][j][k]) cout << i << " " << j << " " << k <<
            //" : " << dp[i][j][k] << endl;
            dp[i + 1][j][k] = (dp[i + 1][j][k] + k * dp[i][j][k]) % MOD;
            if (j >= 1) {
              dp[i + 1][j - 1][k - 1] =
                  (dp[i + 1][j - 1][k - 1] + j * k * dp[i][j][k]) % MOD;
            }
          }
        }
      }
    }
  }

  cout << dp[N - 1][0][0] << endl;
}

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

using namespace std;

#define MOD 1000000007

typedef long long ll;

ll dp[201][201][201] = {0}; // dp[n][rest U][rest empty box] = ??´????????°

int main() {
  int N;
  cin >> N;
  vector<int> v;
  for (int i = 0; i < N; ++i) {
    char c;
    cin >> c;
    if (c != '-') {
      v.push_back(c);
    }
  }
  N = v.size();

  if (N == 0) {
    cout << 1 << endl;
    return 0;
  }
  if (v[0] == 'D') {
    cout << 0 << endl;
    return 0;
  }

  dp[0][1][1] = 1; // v[0] = 'U'
  for (int i = 0; i < N - 1; ++i) {
    for (int j = 0; j < N; ++j) {
      for (int k = 0; k < N; ++k) {
        if (v[i + 1] == 'U') {
          dp[i + 1][j + 1][k + 1] =
              (dp[i + 1][j + 1][k + 1] + dp[i][j][k]) % MOD;
          if (j >= 1) {
            dp[i + 1][j][k] = (dp[i + 1][j][k] + j * dp[i][j][k]) % MOD;
          }
        } else { // D
          if (k >= 1) {
            //						if (dp[i][j][k]) cout << i << " " << j << " " << k <<
            //" : " << dp[i][j][k] << endl;
            dp[i + 1][j][k] = (dp[i + 1][j][k] + k * dp[i][j][k]) % MOD;
            if (j >= 1) {
              dp[i + 1][j - 1][k - 1] =
                  (dp[i + 1][j - 1][k - 1] + j * k * dp[i][j][k]) % MOD;
            }
          }
        }
      }
    }
  }

  cout << dp[N - 1][0][0] << endl;
}
Problem p01553 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
ll dp[128][128][128];

int main() {
  int n;
  int const mod = 1000000007;
  while (cin >> n && n) {
    memset(dp, 0, sizeof(dp));
    dp[0][0][0] = 1;
    for (int k = 0; k < n; k++) {
      char c;
      cin >> c;
      if (c == '-') {
        for (int x = 0; x <= k; x++) {
          for (int y = 0; y <= k; y++) {
            dp[k + 1][x][y] += dp[k][x][y];
          }
        }
      }

      if (c == 'D') {
        for (int x = 1; x <= k; x++) {
          for (int y = 1; y <= k; y++) {
            dp[k + 1][x - 1][y - 1] += dp[k][x][y] * x * y;
            dp[k + 1][x - 1][y - 1] %= mod;
          }
        }
        for (int x = 0; x <= k; x++) {
          for (int y = 0; y <= k; y++) {
            dp[k + 1][x][y] += dp[k][x][y] * y;
            dp[k + 1][x][y] %= mod;
          }
        }
      }

      if (c == 'U') {
        for (int x = 0; x <= k; x++) {
          for (int y = 0; y <= k; y++) {
            dp[k + 1][x][y] += dp[k][x][y] * x;
            dp[k + 1][x][y] %= mod;
          }
        }
        for (int x = 0; x <= k; x++) {
          for (int y = 0; y <= k; y++) {
            dp[k + 1][x + 1][y + 1] += dp[k][x][y];
            dp[k + 1][x + 1][y + 1] %= mod;
          }
        }
      }
    }

    // for(int i=0;i<=n;i++){
    //     for(int j=0;j<=n;j++){
    //         cout<<dp[n][i][j]<<" ";
    //         if(j==n)cout<<endl;
    //     }
    // }
    cout << dp[n][0][0] << endl;
  }
}

Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
ll dp[256][256][256];

int main() {
  int n;
  int const mod = 1000000007;
  while (cin >> n && n) {
    memset(dp, 0, sizeof(dp));
    dp[0][0][0] = 1;
    for (int k = 0; k < n; k++) {
      char c;
      cin >> c;
      if (c == '-') {
        for (int x = 0; x <= k; x++) {
          for (int y = 0; y <= k; y++) {
            dp[k + 1][x][y] += dp[k][x][y];
          }
        }
      }

      if (c == 'D') {
        for (int x = 1; x <= k; x++) {
          for (int y = 1; y <= k; y++) {
            dp[k + 1][x - 1][y - 1] += dp[k][x][y] * x * y;
            dp[k + 1][x - 1][y - 1] %= mod;
          }
        }
        for (int x = 0; x <= k; x++) {
          for (int y = 0; y <= k; y++) {
            dp[k + 1][x][y] += dp[k][x][y] * y;
            dp[k + 1][x][y] %= mod;
          }
        }
      }

      if (c == 'U') {
        for (int x = 0; x <= k; x++) {
          for (int y = 0; y <= k; y++) {
            dp[k + 1][x][y] += dp[k][x][y] * x;
            dp[k + 1][x][y] %= mod;
          }
        }
        for (int x = 0; x <= k; x++) {
          for (int y = 0; y <= k; y++) {
            dp[k + 1][x + 1][y + 1] += dp[k][x][y];
            dp[k + 1][x + 1][y + 1] %= mod;
          }
        }
      }
    }

    // for(int i=0;i<=n;i++){
    //     for(int j=0;j<=n;j++){
    //         cout<<dp[n][i][j]<<" ";
    //         if(j==n)cout<<endl;
    //     }
    // }
    cout << dp[n][0][0] << endl;
  }
}
Problem p01553 (C++)

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

using namespace std;
typedef long long ll;

int main() {
  ll dp[420] = {}, *dp0 = dp + 1, *dp1 = dp + 221;
  dp1[0] = 1;
  int n;
  cin >> n;
  while (n--) {
    char c;
    cin >> c;
    if (c == '-')
      continue;
    for (int j = 0; j < 210; j++) {
      dp0[j] = (j * dp1[j] +
                ((c == 'U') ? dp1[j - 1] : (j + 1) * (j + 1) * dp1[j + 1])) %
               (ll)(1e9 + 7);
    }
    swap(dp0, dp1);
  }
  cout << dp1[0] << endl;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;
typedef long long ll;

int main() {
  ll dp[700] = {}, *dp0 = dp + 100, *dp1 = dp + 400;
  dp1[0] = 1;
  int n;
  cin >> n;
  while (n--) {
    char c;
    cin >> c;
    if (c == '-')
      continue;
    for (int j = 0; j < 210; j++) {
      dp0[j] = (j * dp1[j] +
                ((c == 'U') ? dp1[j - 1] : (j + 1) * (j + 1) * dp1[j + 1])) %
               (ll)(1e9 + 7);
    }
    swap(dp0, dp1);
  }
  cout << dp1[0] << endl;
  return 0;
}
Problem p01554 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {

  int n, m;
  bool open = true;

  cin >> n;
  string tou[200];
  REP(i, n) { cin >> tou[i]; }
  cin >> m;
  string kita;
  REP(i, m) {
    cin >> kita;
    REP(j, n) {
      if (kita == tou[j]) {
        if (open) {
          cout << "Opened by " << kita << endl;
          open = (not open);
          break;
        } else {
          cout << "Closed by " << kita << endl;
          open = (not open);
          break;
        }
      }
      if (j == n - 1)
        cout << "Unknown " << kita << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {

  int n, m;
  bool open = true;

  cin >> n;
  string tou[500];
  REP(i, n) { cin >> tou[i]; }
  cin >> m;
  string kita;
  REP(i, m) {
    cin >> kita;
    REP(j, n) {
      if (kita == tou[j]) {
        if (open) {
          cout << "Opened by " << kita << endl;
          open = (not open);
          break;
        } else {
          cout << "Closed by " << kita << endl;
          open = (not open);
          break;
        }
      }
      if (j == n - 1)
        cout << "Unknown " << kita << endl;
    }
  }
}
Problem p01554 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int main() {
  int n;
  cin >> n;
  string str[100];
  for (int i = 0; i < n; i++) {
    cin >> str[i];
  }
  int m;
  cin >> m;
  int cnt = 0;
  for (int j = 0; j < m; j++) {
    string str2;
    cin >> str2;
    for (int k = 0; k < n; k++) {
      if (str[k] == str2) {
        cnt++;
        if (cnt % 2 == 1)
          cout << "Opened by " << str2 << endl;
        else
          cout << "Closed by " << str2 << endl;
        break;
      }
      if (k == n - 1) {
        cout << "Unknown " << str2 << endl;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int main() {
  int n;
  cin >> n;
  string str[256];
  for (int i = 0; i < n; i++) {
    cin >> str[i];
  }
  int m;
  cin >> m;
  int cnt = 0;
  for (int j = 0; j < m; j++) {
    string str2;
    cin >> str2;
    for (int k = 0; k < n; k++) {
      if (str[k] == str2) {
        cnt++;
        if (cnt % 2 == 1)
          cout << "Opened by " << str2 << endl;
        else
          cout << "Closed by " << str2 << endl;
        break;
      }
      if (k == n - 1) {
        cout << "Unknown " << str2 << endl;
      }
    }
  }
  return 0;
}
Problem p01554 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;
int main() {
  int N, M;
  int frag = 0;
  string U[255];
  string T;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> U[i];
  }
  cin >> M;
  for (int j = 0; j < M; j++) {
    cin >> T;
    for (int i = 0; i < N; i++) {
      if (T == U[i]) {
        frag += 1;

        if (frag % 2 == 0) {
          cout << "Closed by " << T << endl;
        }

        else {
          cout << "Opened by " << T << endl;
        }

        break;
      }
      if (i == N - 1) {
        cout << "Unknown " << T << endl;
      }
    }
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int main() {
  int N, M;
  int frag = 0;
  string U[256];
  string T;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> U[i];
  }
  cin >> M;
  for (int j = 0; j < M; j++) {
    cin >> T;
    for (int i = 0; i < N; i++) {
      if (T == U[i]) {
        frag += 1;

        if (frag % 2 == 0) {
          cout << "Closed by " << T << endl;
        }

        else {
          cout << "Opened by " << T << endl;
        }

        break;
      }
      if (i == N - 1) {
        cout << "Unknown " << T << endl;
      }
    }
  }

  return 0;
}
Problem p01554 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  string id[11];
  for (int i = 0; i < n; i++) {
    cin >> id[i];
  }
  int lock = 0;
  int m;
  cin >> m;
  string x;
  for (int i = 0; i < m; i++) {
    int cnt = 0;
    cin >> x;
    for (int j = 0; j < n; j++) {
      if (x == id[j]) {
        if (lock == 0) {
          lock = 1;
          cout << "Opened by " << x << endl;
        } else {
          lock = 0;
          cout << "Closed by " << x << endl;
        }
        continue;
      } else
        cnt++;
    }
    if (cnt == n) {
      cout << "Unknown " << x << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  string id[257];
  for (int i = 0; i < n; i++) {
    cin >> id[i];
  }
  int lock = 0;
  int m;
  cin >> m;
  string x;
  for (int i = 0; i < m; i++) {
    int cnt = 0;
    cin >> x;
    for (int j = 0; j < n; j++) {
      if (x == id[j]) {
        if (lock == 0) {
          lock = 1;
          cout << "Opened by " << x << endl;
        } else {
          lock = 0;
          cout << "Closed by " << x << endl;
        }
        continue;
      } else
        cnt++;
    }
    if (cnt == n) {
      cout << "Unknown " << x << endl;
    }
  }

  return 0;
}
Problem p01555 (C++)

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

ll length(ll n) {
  if (n == 0) {
    return 0;
  }
  ll k = 1, d = 1;
  while (10 * k <= n) {
    k *= 10;
    d++;
  }
  k--;
  ll n3 = n / 3 - k / 3;
  ll n5 = n / 5 - k / 5;
  ll n15 = n / 15 - k / 15;
  return length(k) + 4 * (n3 + n5) + d * (n - k - n3 - n5 + n15);
}

int main() {
  ll s;
  cin >> s;

  ll ok = 1, ng = 1e17, m;
  while (ng - ok > 1) {
    m = (ok + ng) / 2;
    ll l = length(m - 1);
    if (l <= s - 1) {
      ok = m;
    } else {
      ng = m;
    }
  }

  string r;
  for (ll i = ok; r.size() < 100; i++) {
    if (i % 15 == 0) {
      r += "FizzBuzz";
    } else if (i % 3 == 0) {
      r += "Fizz";
    } else if (i % 5 == 0) {
      r += "Buzz";
    } else {
      r += to_string(i);
    }
  }
  cerr << r << endl;
  cout << r.substr(s - 1 - length(ok - 1), 20) << endl;
}


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

ll length(ll n) {
  if (n == 0) {
    return 0;
  }
  ll k = 1, d = 1;
  while (10 * k <= n) {
    k *= 10;
    d++;
  }
  k--;
  ll n3 = n / 3 - k / 3;
  ll n5 = n / 5 - k / 5;
  ll n15 = n / 15 - k / 15;
  return length(k) + 4 * (n3 + n5) + d * (n - k - n3 - n5 + n15);
}

int main() {
  ll s;
  cin >> s;

  ll ok = 1, ng = 1e17, m;
  while (ng - ok > 1) {
    m = (ok + ng) / 2;
    ll l = length(m - 1);
    if (l <= s - 1) {
      ok = m;
    } else {
      ng = m;
    }
  }

  string r;
  for (ll i = ok; r.size() < 100; i++) {
    if (i % 15 == 0) {
      r += "FizzBuzz";
    } else if (i % 3 == 0) {
      r += "Fizz";
    } else if (i % 5 == 0) {
      r += "Buzz";
    } else {
      r += to_string(i);
    }
  }
  cout << r.substr(s - 1 - length(ok - 1), 20) << endl;
}
Problem p01555 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

long long getLen(long long n) {
  long long ret = 0;

  long long a = 1;
  long long b = 9;
  long long len = 1;
  while (a <= n) {
    b = min(b, n);
    long long fizzbuzz = b / 15 - (a - 1) / 15;
    long long fizz = b / 3 - (a - 1) / 3 - fizzbuzz;
    long long buzz = b / 5 - (a - 1) / 5 - fizzbuzz;

    ret += fizzbuzz * 8 + fizz * 4 + buzz * 4;
    ret += (b - a + 1 - fizzbuzz - fizz - buzz) * len;

    a *= 10;
    b = (b * 10) + 9;
    ++len;
  }

  return ret;
}

int main() {
  long long n;
  cin >> n;

  long long a = 0;
  long long b = 10000000000000000LL;
  while (a < b) {
    long long c = (a + b + 1) / 2;
    if (getLen(c) < n)
      a = c;
    else
      b = c - 1;
  }

  ostringstream oss;
  while (oss.str().size() < 100) {
    ++a;
    if (a % 3 == 0 || a % 5 == 0) {
      if (a % 3 == 0)
        oss << "Fizz";
      if (a % 5 == 0)
        oss << "Buzz";
    } else {
      oss << a;
    }
  }

  string s = oss.str();
  cout << s.substr(n - getLen(b) - 1, 20) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

long long getLen(long long n) {
  long long ret = 0;

  long long a = 1;
  long long b = 9;
  long long len = 1;
  while (a <= n) {
    b = min(b, n);
    long long fizzbuzz = b / 15 - (a - 1) / 15;
    long long fizz = b / 3 - (a - 1) / 3 - fizzbuzz;
    long long buzz = b / 5 - (a - 1) / 5 - fizzbuzz;

    ret += fizzbuzz * 8 + fizz * 4 + buzz * 4;
    ret += (b - a + 1 - fizzbuzz - fizz - buzz) * len;

    a *= 10;
    b = (b * 10) + 9;
    ++len;
  }

  return ret;
}

int main() {
  long long n;
  cin >> n;

  long long a = 0;
  long long b = 100000000000000000LL;
  while (a < b) {
    long long c = (a + b + 1) / 2;
    if (getLen(c) < n)
      a = c;
    else
      b = c - 1;
  }

  ostringstream oss;
  while (oss.str().size() < 100) {
    ++a;
    if (a % 3 == 0 || a % 5 == 0) {
      if (a % 3 == 0)
        oss << "Fizz";
      if (a % 5 == 0)
        oss << "Buzz";
    } else {
      oss << a;
    }
  }

  string s = oss.str();
  cout << s.substr(n - getLen(b) - 1, 20) << endl;

  return 0;
}
Problem p01555 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;

LL fz_add(LL x) { return (x / 3LL) + (x / 5LL); }

LL fz_cnt(LL x) { return (x / 3LL) + (x / 5LL) - (x / 15LL); }

LL count_char(LL x) {
  LL count = 0;

  for (LL z = 1, inc = 1; z <= x; ++inc) {
    z *= 10;
    if (z <= x) {
      count += inc * (z - (z / 10)) + 4 * (fz_add(z - 1) - fz_add(z / 10 - 1)) -
               inc * (fz_cnt(z - 1) - fz_cnt(z / 10 - 1));
    } else {
      count += inc * (x - (z / 10 - 1)) + 4 * (fz_add(x) - fz_add(z / 10 - 1)) -
               inc * (fz_cnt(x) - fz_cnt(z / 10 - 1));
    }
  }

  return count;
}

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

  LL l, r, count;
  l = 0LL;
  r = 1000000000000000001LL;
  while (l + 1 < r) {
    LL m = (l + r) / 2;

    LL count = count_char(m);

    if (count < N) {
      l = m;
    } else {
      r = m;
    }
  }

  count = count_char(l);

  stringstream ss;
  for (int x = l + 1; x <= l + 20; ++x) {
    if (x % 15 == 0) {
      ss << "FizzBuzz";
    } else if (x % 3 == 0) {
      ss << "Fizz";
    } else if (x % 5 == 0) {
      ss << "Buzz";
    } else {
      ss << x;
    }
  }

  string ret;
  ss >> ret;
  cout << ret.substr(N - count - 1, 20) << endl;

  return 0;
}

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

LL fz_add(LL x) { return (x / 3LL) + (x / 5LL); }

LL fz_cnt(LL x) { return (x / 3LL) + (x / 5LL) - (x / 15LL); }

LL count_char(LL x) {
  LL count = 0;

  for (LL z = 1, inc = 1; z <= x; ++inc) {
    z *= 10;
    if (z <= x) {
      count += inc * (z - (z / 10)) + 4 * (fz_add(z - 1) - fz_add(z / 10 - 1)) -
               inc * (fz_cnt(z - 1) - fz_cnt(z / 10 - 1));
    } else {
      count += inc * (x - (z / 10 - 1)) + 4 * (fz_add(x) - fz_add(z / 10 - 1)) -
               inc * (fz_cnt(x) - fz_cnt(z / 10 - 1));
    }
  }

  return count;
}

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

  LL l, r, count;
  l = 0LL;
  r = 1000000000000000001LL;
  while (l + 1 < r) {
    LL m = (l + r) / 2;

    LL count = count_char(m);

    if (count < N) {
      l = m;
    } else {
      r = m;
    }
  }

  count = count_char(l);

  stringstream ss;
  for (LL x = l + 1; x <= l + 20; ++x) {
    if (x % 15 == 0) {
      ss << "FizzBuzz";
    } else if (x % 3 == 0) {
      ss << "Fizz";
    } else if (x % 5 == 0) {
      ss << "Buzz";
    } else {
      ss << x;
    }
  }

  string ret;
  ss >> ret;
  cout << ret.substr(N - count - 1, 20) << endl;

  return 0;
}
Problem p01557 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<short> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int n;

map<vi, short> bfs(vi v) {
  map<vi, short> mp;
  queue<vi> que;
  que.push(v);
  mp[v] = 0;
  while (que.size()) {
    vi tmp = que.front();
    que.pop();
    int t = mp[tmp];
    if (t >= n / 2)
      continue;
    REP(i, n) REP(j, i) {
      vi nx = tmp;
      reverse(nx.begin() + j, nx.begin() + i + 1);
      if (mp.find(nx) != mp.end())
        continue;
      else {
        mp[nx] = t + 1;
        que.push(nx);
      }
    }
  }
  return mp;
}

signed main() {
  cin >> n;
  vi a(n), b;
  REP(i, n) cin >> a[i];
  b = a;
  sort(ALL(b));
  auto mp1 = bfs(a);
  auto mp2 = bfs(b);
  int ans = n - 1;
  for (auto it = mp1.begin(); it != mp1.end(); it++) {
    if (mp2.find(it->first) != mp2.end())
      chmin(ans, it->second + mp2[it->first]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<short> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int n;

map<vi, short> bfs(vi v) {
  map<vi, short> mp;
  queue<vi> que;
  que.push(v);
  mp[v] = 0;
  while (que.size()) {
    vi tmp = que.front();
    que.pop();
    int t = mp[tmp];
    if (t >= (n - 1) / 2)
      continue;
    REP(i, n) REP(j, i) {
      vi nx = tmp;
      reverse(nx.begin() + j, nx.begin() + i + 1);
      if (mp.find(nx) != mp.end())
        continue;
      else {
        mp[nx] = t + 1;
        que.push(nx);
      }
    }
  }
  return mp;
}

signed main() {
  cin >> n;
  vi a(n), b;
  REP(i, n) cin >> a[i];
  b = a;
  sort(ALL(b));
  auto mp1 = bfs(a);
  auto mp2 = bfs(b);
  int ans = n - 1;
  for (auto it = mp1.begin(); it != mp1.end(); it++) {
    if (mp2.find(it->first) != mp2.end())
      chmin(ans, it->second + mp2[it->first]);
  }
  cout << ans << endl;
  return 0;
}
Problem p01558 (C++)

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

typedef unsigned long long ulli;

const ulli B = 1000000007ULL;
const int MAXN = 100005;
int n, m;
string s;
ulli Bn[MAXN];
ulli sum_h[MAXN];

int main() {
  for (int i = 0; i < MAXN; ++i) {
    if (i == 0)
      Bn[i] = 1;
    else
      Bn[i] = Bn[i - 1] * B;
  }
  while (cin >> n >> m) {
    cin >> s;
    sum_h[0] = 0;
    for (int i = 0; i < n; ++i) {
      sum_h[i + 1] = sum_h[i] + s[i] * Bn[i];
    }
    set<ulli> t;
    int l = 0, r = 0;
    while (m--) {
      string q;
      cin >> q;
      if (0) {
      } else if (q == "L++") {
        l++;
      } else if (q == "L--") {
        l--;
      } else if (q == "R++") {
        r++;
      } else if (q == "R--") {
        r--;
      }
      ulli hash = (sum_h[r + 1] - sum_h[l]) * Bn[n - r];
      t.insert(hash);
    }
    cout << t.size() << endl;
  }
  return 0;
}

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

typedef unsigned long long ulli;

const ulli B = 1000000007ULL;
const int MAXN = 300005;
int n, m;
string s;
ulli Bn[MAXN];
ulli sum_h[MAXN];

int main() {
  for (int i = 0; i < MAXN; ++i) {
    if (i == 0)
      Bn[i] = 1;
    else
      Bn[i] = Bn[i - 1] * B;
  }
  while (cin >> n >> m) {
    cin >> s;
    sum_h[0] = 0;
    for (int i = 0; i < n; ++i) {
      sum_h[i + 1] = sum_h[i] + s[i] * Bn[i];
    }
    set<ulli> t;
    int l = 0, r = 0;
    while (m--) {
      string q;
      cin >> q;
      if (0) {
      } else if (q == "L++") {
        l++;
      } else if (q == "L--") {
        l--;
      } else if (q == "R++") {
        r++;
      } else if (q == "R--") {
        r--;
      }
      ulli hash = (sum_h[r + 1] - sum_h[l]) * Bn[n - r];
      t.insert(hash);
    }
    cout << t.size() << endl;
  }
  return 0;
}
Problem p01558 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
struct RollingHash {
  vector<unsigned> hashed, power;
  RollingHash(const string &s, unsigned _base = 1000000007) {
    _base = _base;
    int sz = s.size();
    hashed.assign(sz + 1, 0);
    power.assign(sz + 1, 0);

    power[0] = 1;
    for (int i = 0; i < sz; i++) {
      power[i + 1] = power[i] * _base;
    }
    for (int i = 0; i < sz; i++) {
      hashed[i + 1] = (hashed[i] + s[i]) * _base;
    }
  }
  unsigned get(int l, int r) // [l, r)
  {
    return ((hashed[r] - hashed[l] * power[r - l]));
  }
};

int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  char S[100001];
  scanf(" %s", S);
  RollingHash hash1(S), hash2(S, 1e9 + 9);
  vector<tuple<unsigned, unsigned>> poyo;
  int l = 0, r = 1;
  while (M--) {
    scanf("%s", S);
    if (S[0] == 'R') {
      if (S[1] == '+')
        ++r;
      else
        --r;
    } else {
      if (S[1] == '+')
        ++l;
      else
        --l;
    }
    poyo.emplace_back(hash1.get(l, r), hash2.get(l, r));
  }
  sort(poyo.begin(), poyo.end());
  poyo.erase(unique(poyo.begin(), poyo.end()), poyo.end());
  printf("%d\n", poyo.size());
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct RollingHash {
  vector<unsigned> hashed, power;
  RollingHash(const string &s, unsigned _base = 1000000007) {
    _base = _base;
    int sz = s.size();
    hashed.assign(sz + 1, 0);
    power.assign(sz + 1, 0);

    power[0] = 1;
    for (int i = 0; i < sz; i++) {
      power[i + 1] = power[i] * _base;
    }
    for (int i = 0; i < sz; i++) {
      hashed[i + 1] = (hashed[i] + s[i]) * _base;
    }
  }
  unsigned get(int l, int r) // [l, r)
  {
    return ((hashed[r] - hashed[l] * power[r - l]));
  }
};

int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  char S[300001];
  scanf(" %s", S);
  RollingHash hash1(S), hash2(S, 1e9 + 9);
  vector<tuple<unsigned, unsigned>> poyo;
  int l = 0, r = 1;
  while (M--) {
    scanf("%s", S);
    if (S[0] == 'R') {
      if (S[1] == '+')
        ++r;
      else
        --r;
    } else {
      if (S[1] == '+')
        ++l;
      else
        --l;
    }
    poyo.emplace_back(hash1.get(l, r), hash2.get(l, r));
  }
  sort(poyo.begin(), poyo.end());
  poyo.erase(unique(poyo.begin(), poyo.end()), poyo.end());
  printf("%d\n", poyo.size());
}
Problem p01558 (C++)

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

using namespace std;

typedef long long ull;

const ull P1 = 1000532779;
const ull P2 = 1777777777;
const ull B1 = 1000000009;
const ull B2 = 1000000007;
int main() {
  char s[100001];
  static pair<ull, ull> hs[100001], pow[100001];
  int n, m;

  scanf("%d %d", &n, &m);
  scanf("%s", s);

  hs[0] = make_pair(0, 0);
  pow[0] = make_pair(1, 1);

  for (int i = 0; s[i]; i++) {
    hs[i + 1] = make_pair((hs[i].first * B1 + s[i]) % P1,
                          (hs[i].second * B2 + s[i]) % P2);
    pow[i + 1] = make_pair(pow[i].first * B1 % P1, pow[i].second * B2 % P2);
  }

  set<pair<ull, ull>> se;

  int left = 0, right = 0;
  for (int i = 0; i < m; i++) {
    char t[4];
    scanf("%s", t);

    if (t[0] == 'L' && t[1] == '+')
      left++;
    if (t[0] == 'R' && t[1] == '+')
      right++;
    if (t[0] == 'L' && t[1] == '-')
      left--;
    if (t[0] == 'R' && t[1] == '-')
      right--;

    pair<ull, ull> L = hs[left], R = hs[right + 1];

    se.insert(make_pair(
        (R.first - (L.first * pow[right - left + 1].first) % P1 + P1) % P1,
        (R.second - (L.second * pow[right - left + 1].second) % P2 + P2) % P2));
  }

  printf("%d\n", se.size());

  return (0);
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ull;

const ull P1 = 1000532779;
const ull P2 = 1777777777;
const ull B1 = 1000000009;
const ull B2 = 1000000007;
int main() {
  char s[300001];
  static pair<ull, ull> hs[300001], pow[300001];
  int n, m;

  scanf("%d %d", &n, &m);
  scanf("%s", s);

  hs[0] = make_pair(0, 0);
  pow[0] = make_pair(1, 1);

  for (int i = 0; s[i]; i++) {
    hs[i + 1] = make_pair((hs[i].first * B1 + s[i]) % P1,
                          (hs[i].second * B2 + s[i]) % P2);
    pow[i + 1] = make_pair(pow[i].first * B1 % P1, pow[i].second * B2 % P2);
  }

  set<pair<ull, ull>> se;

  int left = 0, right = 0;
  for (int i = 0; i < m; i++) {
    char t[4];
    scanf("%s", t);

    if (t[0] == 'L' && t[1] == '+')
      left++;
    if (t[0] == 'R' && t[1] == '+')
      right++;
    if (t[0] == 'L' && t[1] == '-')
      left--;
    if (t[0] == 'R' && t[1] == '-')
      right--;

    pair<ull, ull> L = hs[left], R = hs[right + 1];

    se.insert(make_pair(
        (R.first - (L.first * pow[right - left + 1].first) % P1 + P1) % P1,
        (R.second - (L.second * pow[right - left + 1].second) % P2 + P2) % P2));
  }

  printf("%d\n", se.size());

  return (0);
}