prithivMLmods/Coder-Stat huggingface dataset:
Problem p02756 (Python)

Buggy submission (status: Time Limit Exceeded):
s = input()
q = int(input())

counter = 0
for i in range(q):
    x = input().split()

    if str(x[0]) == "1":
        counter += 1

    else:
        if counter % 2 != 0:
            s = s[::-1]
            counter = 0
        if str(x[1]) == "1":
            s = str(x[2]) + s

        else:
            s = s + str(x[2])

if counter % 2 != 0:
    s = s[::-1]
print(s)


Fixed submission:
s = input()
q = int(input())
qs = [input().split() for i in range(q)]
n = False
left = right = ""
for operation in qs:
    if operation[0] == "1":
        n = not n
    if operation[0] == "2":
        t, f, c = operation
        f = int(f)
        if n:
            f = 1 if f != 1 else 2
        if f == 1:
            left = c + left
        elif f == 2:
            right += c
s = left + s + right
print(s[::-1] if n else s)
Problem p02756 (Python)

Buggy submission (status: Runtime Error):
from collections import deque

S = deque(list(input()))
q = int(input())
r = False
for _ in range(q):
    t = input().split()
    if len(t) == 1:
        r = not r
    else:
        t[1] = int(t[1]) - 1
        if (not t[1] and not r) or (t[1] and r):
            S.appendleft(t[2])
        else:
            S.append(t[2])
if r:
    print("".join(S[::-1]))
else:
    print("".join(S))


Fixed submission:
from collections import deque

S = deque(list(input()))
q = int(input())
r = False
for _ in range(q):
    t = input().split()
    if len(t) == 1:
        r = not r
    else:
        t[1] = int(t[1]) - 1
        if (not t[1] and not r) or (t[1] and r):
            S.appendleft(t[2])
        else:
            S.append(t[2])
if r:
    S = list(S)
    print("".join(S[::-1]))
else:
    print("".join(S))
Problem p02756 (Python)

Buggy submission (status: Runtime Error):
import sys
from collections import deque

input = sys.stdin.readline

S = deque(input()[:-1])
reverse_flg = 0
Q = int(input())
qs = [input().split() for _ in range(Q)]
for q in qs:
    if q[0] == "1":
        reverse_flg = reverse_flg ^ 1
    else:
        flg = (int(q[1]) + reverse_flg) % 2
        if flg == 1:
            S.appendleft(q[2])
        else:
            S.append(q[2])

if reverse_flg:
    S = S[::-1]
print("".join(S))


Fixed submission:
import sys
from collections import deque

input = sys.stdin.readline

S = deque(input()[:-1])
reverse_flg = 0
Q = int(input())
qs = [input().split() for _ in range(Q)]
for q in qs:
    if q[0] == "1":
        reverse_flg = reverse_flg ^ 1
    else:
        flg = (int(q[1]) + reverse_flg) % 2
        if flg == 1:
            S.appendleft(q[2])
        else:
            S.append(q[2])

if reverse_flg:
    S.reverse()
print("".join(S))
Problem p02756 (C++)

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

int main() {
  string s;
  cin >> s;
  int q;
  cin >> q;
  int size = s.size();
  vector<char> data(50 * q * size);
  int left = 25 * q * size;
  int right = left + size - 1;
  rep(i, size) { data[left + i] = s[i]; }
  rep(i, q) {
    int a;
    cin >> a;
    if (a == 1) {
      int n, m;
      n = right;
      m = left;
      right = m;
      left = n;
    } else {
      size++;
      int b;
      char c;
      cin >> b >> c;
      if (b == 1) {
        if (left <= right) {
          data[left - 1] = c;
          left--;
        } else {
          data[left + 1] = c;
          left++;
        }
      } else {
        if (left <= right) {
          data[right + 1] = c;
          right++;
        } else {
          data[right - 1] = c;
          right--;
        }
      }
    }
  }
  if (left <= right) {
    rep(i, size) { cout << data[left + i]; }
  } else {
    rep(i, size) { cout << data[left - i]; }
  }
  cout << endl;
}


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

int main() {
  string s;
  cin >> s;
  int q;
  cin >> q;
  int size = s.size();
  vector<char> data(50 * q + 50 * size);
  int left = 25 * q + 25 * size;
  int right = left + size - 1;
  rep(i, size) { data[left + i] = s[i]; }
  rep(i, q) {
    int a;
    cin >> a;
    if (a == 1) {
      int n, m;
      n = right;
      m = left;
      right = m;
      left = n;
    } else {
      size++;
      int b;
      char c;
      cin >> b >> c;
      if (b == 1) {
        if (left <= right) {
          data[left - 1] = c;
          left--;
        } else {
          data[left + 1] = c;
          left++;
        }
      } else {
        if (left <= right) {
          data[right + 1] = c;
          right++;
        } else {
          data[right - 1] = c;
          right--;
        }
      }
    }
  }
  if (left <= right) {
    rep(i, size) { cout << data[left + i]; }
  } else {
    rep(i, size) { cout << data[left - i]; }
  }
  cout << endl;
}
Problem p02756 (C++)

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

int main() {
  deque<char> str;
  int Q, T, F, a, b;
  string S;
  char C;
  cin >> S >> Q;
  a = S.size();
  rep(i, a) { str.push_back(S.at(i)); }
  bool rev = false;
  rep(i, Q) {
    cin >> T;
    if (T == 1)
      rev = !rev;
    else {
      cin >> F >> C;
      if (rev) {
        reverse(str.begin(), str.end());
        rev = false;
      }
      if (F == 1)
        str.push_front(C);
      else
        str.push_back(C);
    }
  }
  if (rev) {
    reverse(str.begin(), str.end());
  }
  a = str.size();
  rep(i, a) { cout << str.at(i); }
  cout << endl;
}

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

int main() {
  deque<char> str;
  int Q, T, F, a, b;
  string S;
  char C;
  cin >> S >> Q;
  a = S.size();
  rep(i, a) { str.push_back(S.at(i)); }
  bool rev = false;
  rep(i, Q) {
    cin >> T;
    if (T == 1)
      rev = !rev;
    else {
      cin >> F >> C;
      if (rev) {
        F = 3 - F;
      }
      if (F == 1)
        str.push_front(C);
      else
        str.push_back(C);
    }
  }
  if (rev) {
    reverse(str.begin(), str.end());
  }
  a = str.size();
  rep(i, a) { cout << str.at(i); }
  cout << endl;
}
Problem p02756 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define TV(K, L, N, M, R, S)                                                   \
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define mod 1000000007
#define MAX 510000
#define ALL(a) a.begin(), a.end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
  lli A, B, C, D, E, F, N, M, K, L, X, Y, Z, H, W, sum = 0, num = 0, flag = 0;
  string S, T;
  cin >> S >> N;
  rep(i, 0, N) {
    cin >> A;
    if (A == 1)
      sum++;
    else {
      cin >> B >> T;
      if (B == 1) {
        if (sum % 2 == 0)
          S = T + S;
        else
          S += T;
      } else {
        if (sum % 2 == 0)
          S += T;
        else
          S = T + S;
      }
    }
  }
  if (sum % 2 == 1)
    Rev(S);
  Out(S);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define TV(K, L, N, M, R, S)                                                   \
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define mod 1000000007
#define MAX 510000
#define ALL(a) a.begin(), a.end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
  lli A, B, C, D, E, F, N, M, K, L, X, Y, Z, H, W, sum = 0, num = 0, flag = 0;
  string S, T;
  cin >> S >> N;
  rep(i, 0, N) {
    cin >> A;
    if (A == 1)
      sum++;
    else {
      cin >> B >> T;
      if ((sum + B) % 2 == 1)
        S.insert(0, T);
      else
        S += T;
    }
  }
  if (sum % 2 == 1)
    Rev(S);
  Out(S);
}
Problem p02756 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  string S;
  int Q;
  cin >> S;
  cin >> Q;

  deque<char> S_deque;
  for (int i = 0; i < S.size(); i++) {
    S_deque.push_back(S[i]);
  }
  int t, f, t_sum;
  char c;
  t_sum = 0;
  for (int i = 0; i < Q; i++) {
    cin >> t;
    if (t == 1) {
      t_sum++;
      t_sum %= 2;
    } else {
      cin >> f >> c;
      if ((f + t_sum) % 2 == 1) {
        S_deque.push_front(c);
      } else {
        S_deque.push_back(c);
      }
    }
  }

  if (t_sum == 1) {
    for (int i = 0; i < S_deque.size(); i++) {
      cout << S_deque[S.size() - i + 1];
    }
  } else {
    for (int i = 0; i < S_deque.size(); i++) {
      cout << S_deque[i];
    }
  }

  cout << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  string S;
  int Q;
  cin >> S;
  cin >> Q;

  deque<char> S_deque;
  for (int i = 0; i < S.size(); i++) {
    S_deque.push_back(S[i]);
  }
  int t, f, t_sum;
  char c;
  t_sum = 0;
  for (int i = 0; i < Q; i++) {
    cin >> t;
    if (t == 1) {
      t_sum++;
      t_sum %= 2;
    } else {
      cin >> f >> c;
      if ((f + t_sum) % 2 == 1) {
        S_deque.push_front(c);
      } else {
        S_deque.push_back(c);
      }
    }
  }

  if (t_sum == 1) {
    for (int i = 0; i < S_deque.size(); i++) {
      cout << S_deque[S_deque.size() - i - 1];
    }
  } else {
    for (int i = 0; i < S_deque.size(); i++) {
      cout << S_deque[i];
    }
  }

  cout << endl;
}
Problem p02756 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
int main() {
  char str[50001];
  bool hanten = false;
  vector<char> ft, bk;
  int Q, T, F;
  char c[2];
  scanf("%s", str);
  scanf("%d", &Q);
  rep(i, 0, Q) {
    scanf("%d", &T);
    if (T == 1)
      hanten = !hanten;
    else {
      scanf("%d", &F);
      scanf("%s", c);
      getchar();
      (((F - 1) ^ hanten) ? bk : ft).push_back(c[0]);
    }
  }
  if (hanten) {
    int len = strlen(str);
    vector<char> str2(len + 1);
    str2[len] = 0;
    rep(i, 0, len) { str2[i] = str[len - i - 1]; }
    reverse(bk.begin(), bk.end());
    bk.push_back(0);
    ft.push_back(0);
    printf("%s%s%s", bk.data(), str2.data(), ft.data());
  } else {
    reverse(ft.begin(), ft.end());
    ft.push_back(0);
    bk.push_back(0);
    printf("%s%s%s", ft.data(), str, bk.data());
  }
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <functional>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
int main() {
  char str[100001];
  bool hanten = false;
  vector<char> ft, bk;
  int Q, T, F;
  char c[2];
  scanf("%s", str);
  scanf("%d", &Q);
  rep(i, 0, Q) {
    scanf("%d", &T);
    if (T == 1)
      hanten = !hanten;
    else {
      scanf("%d", &F);
      scanf("%s", c);
      getchar();
      (((F - 1) ^ hanten) ? bk : ft).push_back(c[0]);
    }
  }
  if (hanten) {
    int len = strlen(str);
    vector<char> str2(len + 1);
    str2[len] = 0;
    rep(i, 0, len) { str2[i] = str[len - i - 1]; }
    reverse(bk.begin(), bk.end());
    bk.push_back(0);
    ft.push_back(0);
    printf("%s%s%s", bk.data(), str2.data(), ft.data());
  } else {
    reverse(ft.begin(), ft.end());
    ft.push_back(0);
    bk.push_back(0);
    printf("%s%s%s", ft.data(), str, bk.data());
  }
  return 0;
}
Problem p02756 (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() {
  string S;
  int Q;
  cin >> S >> Q;
  bool sei = true;

  vector<char> start(2 * 10 * 5);
  vector<char> end(2 * 10 * 5);

  int sIndex = 0;
  int eIndex = 0;

  rep(i, Q) {
    int r, f;
    char c;
    cin >> r;
    if (r == 2) {
      cin >> f >> c;
    }

    if (r == 1) {
      sei = !sei;
    } else {
      if (f == 1) {
        if (sei) {
          start[sIndex] = c;
          sIndex++;
        } else {
          end[eIndex] = c;
          eIndex++;
        }
      } else {
        if (sei) {
          end[eIndex] = c;
          eIndex++;
        } else {
          start[sIndex] = c;
          sIndex++;
        }
      }
    }
  }
  if (sei) {
    rep(i, start.size()) {
      if (!start[start.size() - 1 - i])
        continue;
      cout << start[start.size() - 1 - i];
    }
    cout << S;
    rep(i, end.size()) {
      if (!end[i])
        break;
      cout << end[i];
    }
  } else {
    rep(i, end.size()) {
      if (!end[end.size() - 1 - i])
        continue;
      cout << end[end.size() - 1 - i];
    }
    reverse(S.begin(), S.end());
    cout << S;
    rep(i, start.size()) {
      if (!start[i])
        break;
      cout << start[i];
    }
  }
  cout << endl;

  return 0;
}


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() {
  string S;
  int Q;
  cin >> S >> Q;
  bool sei = true;

  vector<char> start(1000000);
  vector<char> end(1000000);

  int sIndex = 0;
  int eIndex = 0;

  rep(i, Q) {
    int r, f;
    char c;
    cin >> r;
    if (r == 2) {
      cin >> f >> c;
    }

    if (r == 1) {
      sei = !sei;
    } else {
      if (f == 1) {
        if (sei) {
          start[sIndex] = c;
          sIndex++;
        } else {
          end[eIndex] = c;
          eIndex++;
        }
      } else {
        if (sei) {
          end[eIndex] = c;
          eIndex++;
        } else {
          start[sIndex] = c;
          sIndex++;
        }
      }
    }
  }
  if (sei) {
    rep(i, start.size()) {
      if (!start[start.size() - 1 - i])
        continue;
      cout << start[start.size() - 1 - i];
    }
    cout << S;
    rep(i, end.size()) {
      if (!end[i])
        break;
      cout << end[i];
    }
  } else {
    rep(i, end.size()) {
      if (!end[end.size() - 1 - i])
        continue;
      cout << end[end.size() - 1 - i];
    }
    reverse(S.begin(), S.end());
    cout << S;
    rep(i, start.size()) {
      if (!start[i])
        break;
      cout << start[i];
    }
  }
  cout << endl;

  return 0;
}
Problem p02756 (C++)

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

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

const int N = 100005;

void solve() {
  string s;
  cin >> s;
  vector<char> v(N * 2);
  rep(i, s.size()) v[N + i] = s[i];
  int q;
  cin >> q;
  int l = N;
  int r = N + s.size() - 1;
  // for (int i = l; i <= r; i++)
  //   cout << v[i] << endl;
  int mode = 1;
  rep(i, q) {
    int t;
    cin >> t;
    if (t == 1) {
      mode = mode == 1 ? 2 : 1;
    } else {
      int f;
      char c;
      cin >> f >> c;
      if (f != mode) {
        v[++r] = c;
      } else {
        v[--l] = c;
      }
    }
    // dump(l, r, mode);
    // for (int i = l; i <= r; i++)
    //   cout << v[i] << endl;
  }
  string ans;
  if (mode == 1) {
    for (int i = l; i <= r; i++)
      ans += v[i];
  } else {
    for (int i = r; l <= i; i--)
      ans += v[i];
  }
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}

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

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#define dumpv(...)
#endif

#define rep(i, n) for (int i = 0; i < (n); i++)
#define mins(x, y) (x = min(x, y))
#define maxs(x, y) (x = max(x, y))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using P = pair<int, int>;
const int MOD = 1e9 + 7;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

const int N = 200005;

void solve() {
  string s;
  cin >> s;
  vector<char> v(N * 2);
  rep(i, s.size()) v[N + i] = s[i];
  int q;
  cin >> q;
  int l = N;
  int r = N + s.size() - 1;
  // for (int i = l; i <= r; i++)
  //   cout << v[i] << endl;
  int mode = 1;
  rep(i, q) {
    int t;
    cin >> t;
    if (t == 1) {
      mode = mode == 1 ? 2 : 1;
    } else {
      int f;
      char c;
      cin >> f >> c;
      if (f != mode) {
        v[++r] = c;
      } else {
        v[--l] = c;
      }
    }
    // dump(l, r, mode);
    // for (int i = l; i <= r; i++)
    //   cout << v[i] << endl;
  }
  string ans;
  if (mode == 1) {
    for (int i = l; i <= r; i++)
      ans += v[i];
  } else {
    for (int i = r; l <= i; i--)
      ans += v[i];
  }
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  // freopen("temp.1", "r", stdin);
  solve();
  return 0;
}
Problem p02756 (C++)

Buggy submission (status: Time Limit Exceeded):

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

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  string s;
  cin >> s;
  // since repeated reversing the string is time consuming, instead we save the
  // state of the string 1 or 2
  short state = 1;
  int q = 0;
  cin >> q;
  while (q--) {
    int t, f;
    char c;
    cin >> t;
    if (t == 1) {
      state = (state == 1) ? 0 : 1;
    } else if (t == 2) {
      cin >> f >> c;
      if (state == 1)
        f == 1 ? s = c + s : s = s + c;
      else
        f == 1 ? s = s + c : s = c + s;
    }
  }
  if (state == 0)
    reverse(s.begin(), s.end());
  cout << s;
  return 0;
}

Fixed submission:

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

typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  string s;
  cin >> s;
  // since repeated reversing the string is time consuming, instead we save the
  // state of the string 1 or 2
  short state = 1;
  int q = 0;
  cin >> q;
  while (q--) {
    int t, f;
    char c;
    cin >> t;
    if (t == 1) {
      state = (state == 1) ? 0 : 1;
    } else if (t == 2) {
      cin >> f >> c;
      if (state == 1) {
        if (f == 1)
          s.insert(s.begin(), c);
        else
          s.insert(s.end(), c);
      } else {
        if (f == 1)
          s.insert(s.end(), c);
        else
          s.insert(s.begin(), c);
      }
    }
  }
  if (state == 0)
    reverse(s.begin(), s.end());
  cout << s;
  return 0;
}
Problem p02756 (C++)

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

int main() {
  string S, D;
  int Q, i;
  bool b = true;
  cin >> S >> Q;
  for (i = 0; i < Q; ++i) {
    int que = 0;
    cin >> que;
    if (que == 1)
      if (b)
        b = false;
      else
        b = true;
    else {
      int F = 0;
      char C;
      cin >> F >> C;
      if (F == 1) {
        if (b) {
          D.push_back(C);
        } else {
          S.push_back(C);
        }
      } else {
        if (b)
          S.push_back(C);
        else {
          D.push_back(C);
        }
      }
    }
  }
  if (!b) {
    reverse(S.begin(), S.end());
    cout << S << D;
  } else {
    reverse(D.begin(), S.end());
    cout << D << S;
  }
}


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

int main() {
  string S, D;
  int Q, i;
  bool b = true;
  cin >> S >> Q;
  for (i = 0; i < Q; ++i) {
    int que = 0;
    cin >> que;
    if (que == 1)
      if (b)
        b = false;
      else
        b = true;
    else {
      int F = 0;
      char C;
      cin >> F >> C;
      if (F == 1) {
        if (b) {
          D.push_back(C);
        } else {
          S.push_back(C);
        }
      } else {
        if (b)
          S.push_back(C);
        else {
          D.push_back(C);
        }
      }
    }
  }
  if (!b) {
    reverse(S.begin(), S.end());
    cout << S << D;
  } else {
    reverse(D.begin(), D.end());
    cout << D << S;
  }
}
Problem p02756 (Python)

Buggy submission (status: Runtime Error):
from collections import deque

S = deque(input())
Q = int(input())
Queries = [input().split() for _ in range(Q)]

need_reverse = False

for q in Queries:
    if q[0] == "1":
        need_reverse = not need_reverse
    else:
        if q[1] == "1":
            if not need_reverse:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if not need_reverse:
                S.append(q[2])
            else:
                S.appendleft(q[2])

print(S if not need_reverse else S[::-1])


Fixed submission:
from collections import deque

S = deque(input())
Q = int(input())
Queries = [input().split() for _ in range(Q)]

need_reverse = False

for q in Queries:
    if q[0] == "1":
        need_reverse = not need_reverse
    else:
        if q[1] == "1":
            if not need_reverse:
                S.appendleft(q[2])
            else:
                S.append(q[2])
        else:
            if not need_reverse:
                S.append(q[2])
            else:
                S.appendleft(q[2])

S = "".join(S)
print(S if not need_reverse else S[::-1])
Problem p02756 (C++)

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

void textIO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

int main() {
  textIO();
  fastIO();

  string s;
  long long q, t, f;
  char c;
  cin >> s;
  cin >> q;
  bool rev = false;
  while (q--) {
    cin >> t;
    if (t == 1) {
      rev = !rev;
    } else {
      cin >> f >> c;
      if (rev) {
        if (f == 1) {
          s.append(1, c);
        } else {
          s.insert(s.begin(), c);
        }
      } else {
        if (f == 1) {
          s.insert(s.begin(), c);
        } else {
          s.append(1, c);
        }
      }
    }
  }
  if (rev) {
    reverse(s.begin(), s.end());
    cout << s << '\n';
  } else {
    cout << s << '\n';
  }

  return 0;
}


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

void textIO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

int main() {
  // textIO();
  fastIO();

  string s;
  long long q, t, f;
  char c;
  cin >> s;
  cin >> q;
  bool rev = false;
  while (q--) {
    cin >> t;
    if (t == 1) {
      rev = !rev;
    } else {
      cin >> f >> c;
      if (rev) {
        if (f == 1) {
          s.append(1, c);
        } else {
          s.insert(s.begin(), c);
        }
      } else {
        if (f == 1) {
          s.insert(s.begin(), c);
        } else {
          s.append(1, c);
        }
      }
    }
  }
  if (rev) {
    reverse(s.begin(), s.end());
    cout << s << '\n';
  } else {
    cout << s << '\n';
  }

  return 0;
}
Problem p02756 (C++)

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

void textIO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

int main() {
  textIO();
  fastIO();

  string s, prefix, suffix;
  long long q, t, f;
  char c;
  cin >> s;
  cin >> q;
  bool rev = false;
  while (q--) {
    cin >> t;
    if (t == 1) {
      rev = !rev;
    } else {
      cin >> f >> c;
      if (rev) {
        if (f == 1) {
          suffix += c;
        } else {
          prefix += c;
        }
      } else {
        if (f == 1) {
          prefix += c;
        } else {
          suffix += c;
        }
      }
    }
  }
  if (rev) {
    reverse(suffix.begin(), suffix.end());
    reverse(s.begin(), s.end());
    cout << suffix << s << prefix << '\n';
  } else {
    reverse(prefix.begin(), prefix.end());
    cout << prefix << s << suffix << '\n';
  }

  return 0;
}


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

void textIO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

int main() {
  // textIO();
  fastIO();

  string s, prefix, suffix;
  long long q, t, f;
  char c;
  cin >> s;
  cin >> q;
  bool rev = false;
  while (q--) {
    cin >> t;
    if (t == 1) {
      rev = !rev;
    } else {
      cin >> f >> c;
      if (rev) {
        if (f == 1) {
          suffix += c;
        } else {
          prefix += c;
        }
      } else {
        if (f == 1) {
          prefix += c;
        } else {
          suffix += c;
        }
      }
    }
  }
  if (rev) {
    reverse(suffix.begin(), suffix.end());
    reverse(s.begin(), s.end());
    cout << suffix << s << prefix << '\n';
  } else {
    reverse(prefix.begin(), prefix.end());
    cout << prefix << s << suffix << '\n';
  }

  return 0;
}
Problem p02756 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
unordered_map<int, pair<int, char>> mp;
int a[N], c[N];
char b[3 * N];
int main() {
  string s;
  string x;
  int t, k, f, q, sum = 0;
  cin >> s >> q;
  for (int i = 1; i <= q; i++) {
    scanf("%d", &t);
    if (t == 1)
      a[i] = 1, sum++;
    else {
      cin >> f >> x;
      mp[i] = {f, x[0]};
    }
  }
  c[0] = sum % 2;
  for (int i = 1; i <= q; i++)
    c[i] = c[i - 1] + a[i];
  int l = 1e5, r = l - 1;
  if (sum % 2)
    for (int i = s.size() - 1; i >= 0; i--)
      b[++r] = s[i];
  else
    for (int i = 0; i < s.size(); i++)
      b[++r] = s[i];
  //  for(int i = 1; i <= q ; i ++) cout << c[i] << " " ;
  //  puts("") ;
  for (int i = 1; i <= q; i++) {
    if (a[i])
      continue;
    int f = mp[i].first;
    char h = mp[i].second;
    if (c[i] % 2 == f % 2)
      b[++r] = h;
    else
      b[--l] = h;
  }
  for (int i = l; i <= r; i++)
    cout << b[i];
  puts("");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
unordered_map<int, pair<int, char>> mp;
int a[N], c[N];
char b[3 * N];
int main() {
  string s;
  string x;
  int t, k, f, q, sum = 0;
  cin >> s >> q;
  for (int i = 1; i <= q; i++) {
    scanf("%d", &t);
    if (t == 1)
      a[i] = 1, sum++;
    else {
      cin >> f >> x;
      mp[i] = {f, x[0]};
    }
  }
  c[0] = sum % 2;
  for (int i = 1; i <= q; i++)
    c[i] = c[i - 1] + a[i];
  int l = 3e5, r = l - 1;
  if (sum % 2)
    for (int i = s.size() - 1; i >= 0; i--)
      b[++r] = s[i];
  else
    for (int i = 0; i < s.size(); i++)
      b[++r] = s[i];
  //  for(int i = 1; i <= q ; i ++) cout << c[i] << " " ;
  //  puts("") ;
  for (int i = 1; i <= q; i++) {
    if (a[i])
      continue;
    int f = mp[i].first;
    char h = mp[i].second;
    if (c[i] % 2 == f % 2)
      b[++r] = h;
    else
      b[--l] = h;
  }
  for (int i = l; i <= r; i++)
    cout << b[i];
  puts("");
  return 0;
}
Problem p02756 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < n; i++)
#define Rep(i, n) for (long long i = 1; i < n; i++)
#define ll long long
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
#define PI acos(-1)
using P = pair<int, int>;

ll ketasu(ll a) {
  ll num = 1;
  while (a / 10) {
    num++;
    a /= 10;
  }
  return num;
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll kosuu(ll a) {
  ll sum = 0;
  for (ll i = 1; i * i <= a; i++) {
    if (a % i == 0) {
      if (a != 1 && i * i != a) {
        sum += 2;
      } else {
        sum++;
      }
    }
  }
  return sum;
}
ll ans = -1000000000;

int main() {
  string s;
  cin >> s;
  ll n;
  cin >> n;
  ll cnt = 0;
  rep(i, n) {
    ll a;
    cin >> a;
    if (a == 1)
      cnt++;
    if (a == 2) {
      ll b;
      string c;
      cin >> b >> c;
      if (cnt % 2 == 1) {
        if (b == 1)
          b = 2;
        else
          b = 1;
      }
      if (b == 1)
        s = c + s;
      if (b == 2)
        s = s + c;
    }
  }
  if (cnt % 2 == 1)
    reverse(s.begin(), s.end());
  cout << s << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < n; i++)
#define Rep(i, n) for (long long i = 1; i < n; i++)
#define ll long long
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;
#define PI acos(-1)
using P = pair<int, int>;

ll ketasu(ll a) {
  ll num = 1;
  while (a / 10) {
    num++;
    a /= 10;
  }
  return num;
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

ll kosuu(ll a) {
  ll sum = 0;
  for (ll i = 1; i * i <= a; i++) {
    if (a % i == 0) {
      if (a != 1 && i * i != a) {
        sum += 2;
      } else {
        sum++;
      }
    }
  }
  return sum;
}
ll ans = -1000000000;

int main() {
  string s;
  cin >> s;
  ll n;
  cin >> n;
  ll cnt = 0;
  rep(i, n) {
    ll a;
    cin >> a;
    if (a == 1)
      cnt++;
    if (a == 2) {
      ll b;
      string c;
      cin >> b >> c;
      if (cnt % 2 == 1) {
        if (b == 1)
          b = 2;
        else
          b = 1;
      }
      if (b == 1)
        s = c + s;
      if (b == 2)
        s += c;
    }
  }
  if (cnt % 2 == 1)
    reverse(s.begin(), s.end());
  cout << s << endl;
}
Problem p02756 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define fastinput                                                              \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

int main() {
  fastinput;
  LL n, i, j, k, t, temp;
  LL truth = 0;
  string s = "";
  char str[300003];
  LL now = 0;
  LL queries;
  cin >> s;
  LL len = s.length();
  LL start, end;
  start = now - 1;

  for (i = 0; i < len; i++) {
    str[i + now] = s[i];
  }
  end = now + len;
  LL f, c;
  cin >> queries;
  while (queries--) {
    cin >> t;
    if (t == 1) {
      truth ^= 1;
    } else {
      char ch;
      cin >> f >> ch;
      if ((f == 1 && truth == 0) || (f == 2 && truth == 1)) {
        str[start--] = ch;
      } else {
        str[end++] = ch;
      }
    }
  }
  if (truth == 1) {
    for (i = end - 1; i > start; i--) {
      cout << str[i];
    }
    cout << endl;
  } else {
    for (i = start + 1; i < end; i++) {
      cout << str[i];
    }
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define fastinput                                                              \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

int main() {
  fastinput;
  LL n, i, j, k, t, temp;
  LL truth = 0;
  string s = "";
  char str[500003];
  LL now = 200003;
  LL queries;
  cin >> s;
  LL len = s.length();
  LL start, end;
  start = now - 1;

  for (i = 0; i < len; i++) {
    str[i + now] = s[i];
  }
  end = now + len;
  LL f, c;
  cin >> queries;
  while (queries--) {
    cin >> t;
    if (t == 1) {
      truth ^= 1;
    } else {
      char ch;
      cin >> f >> ch;
      if ((f == 1 && truth == 0) || (f == 2 && truth == 1)) {
        str[start--] = ch;
      } else {
        str[end++] = ch;
      }
    }
  }
  if (truth == 1) {
    for (i = end - 1; i > start; i--) {
      cout << str[i];
    }
    cout << endl;
  } else {
    for (i = start + 1; i < end; i++) {
      cout << str[i];
    }
    cout << endl;
  }
}
Problem p02756 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007

int main() {
  string s;
  cin >> s;
  int q;
  scanf("%d", &q);
  bool rev = false;

  for (int i = 0; i < q; i++) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      rev = !rev;
    } else {
      int f;
      string c;
      scanf("%d", &f);
      cin >> c;
      if (rev) {
        if (f == 1)
          f = 2;
        else
          f = 1;
      }

      if (f == 1) {
        s = c + s;
        // s.insert(0,c, 1);
        // cout << s << endl;
      } else {
        // s = s+c;
        s.push_back(c[0]);
      }
    }
  }
  int len = s.size();
  if (rev) {
    for (int i = len - 1; i >= 0; i--)
      cout << s[i];
    cout << endl;
  } else
    cout << s << endl;
  return 0;
}

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

int main() {
  string s;
  cin >> s;
  int q;
  scanf("%d", &q);
  bool rev = false;

  for (int i = 0; i < q; i++) {
    int t;
    scanf("%d", &t);
    if (t == 1) {
      rev = !rev;
    } else {
      int f;
      string c;
      scanf("%d", &f);
      cin >> c;
      if (rev) {
        if (f == 1)
          f = 2;
        else
          f = 1;
      }

      if (f == 1) {
        // s = c+s;
        s.insert(0, c);
      } else {
        // s = s+c;
        s.push_back(c[0]);
      }
    }
  }
  int len = s.size();
  if (rev) {
    for (int i = len - 1; i >= 0; i--)
      cout << s[i];
    cout << endl;
  } else
    cout << s << endl;
  return 0;
}
Problem p02756 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn = 4e5 + 10;
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
char S[maxn], Ci;
int Q, Fi;

int main() {
#ifdef __LOCAL__
  freopen("1.in", "r", stdin);
  // freopen("1.out","w",stdout);
#endif

  string a;
  int pos = 10000, left = 10000, right = pos - 1;
  bool flag = true;
  cin >> a >> Q;
  for (unsigned int i = 0; i < a.size(); i++)
    S[++right] = a[i];

  while (Q--) {
    // for(int i=left;i<=right;i++)
    //     cout<<S[i];
    // cout<<endl;
    // cout<<flag<<endl;
    int type;
    cin >> type;
    if (type == 1) {
      flag = !flag;
    } else if (type == 2) {
      cin >> Fi >> Ci;
      if (Fi == 1) {
        if (flag)
          S[--left] = Ci;
        else
          S[++right] = Ci;
      } else if (Fi == 2) {
        if (!flag)
          S[--left] = Ci;
        else
          S[++right] = Ci;
      }
    }
  }
  if (flag) {
    for (int i = left; i <= right; i++)
      cout << S[i];
    cout << endl;
  } else {
    for (int i = right; i >= left; i--)
      cout << S[i];
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn = 4e5 + 10;
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
char S[maxn], Ci;
int Q, Fi;

int main() {
#ifdef __LOCAL__
  freopen("1.in", "r", stdin);
  // freopen("1.out","w",stdout);
#endif

  string a;
  int pos = 2e5, left = pos, right = pos - 1;
  bool flag = true;
  cin >> a >> Q;
  for (unsigned int i = 0; i < a.size(); i++)
    S[++right] = a[i];

  while (Q--) {
    // for(int i=left;i<=right;i++)
    //     cout<<S[i];
    // cout<<endl;
    // cout<<flag<<endl;
    int type;
    cin >> type;
    if (type == 1) {
      flag = !flag;
    } else if (type == 2) {
      cin >> Fi >> Ci;
      if (Fi == 1) {
        if (flag)
          S[--left] = Ci;
        else
          S[++right] = Ci;
      } else if (Fi == 2) {
        if (!flag)
          S[--left] = Ci;
        else
          S[++right] = Ci;
      }
    }
  }
  if (flag) {
    for (int i = left; i <= right; i++)
      cout << S[i];
    cout << endl;
  } else {
    for (int i = right; i >= left; i--)
      cout << S[i];
    cout << endl;
  }
  return 0;
}
Problem p02756 (C++)

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

int main() {
  string s;
  int q;
  cin >> s >> q;
  vector<int> t(q), f(q, 0);
  vector<string> c(q, "");
  for (int i = 0; i < q; i++) {
    cin >> t.at(i);
    if (t.at(i) == 2) {
      cin >> f.at(i) >> c.at(i);
    }
  }

  bool normal = true;
  for (int i = 0; i < q; i++) {
    if (t.at(i) == 1) {
      reverse(s.begin(), s.end());
    } else if (t.at(i) == 2) {
      if (!normal) {
        if (f.at(i) == 1)
          f.at(i) = 2;
        else if (f.at(i) == 2)
          f.at(i) = 1;
      }
      if (f.at(i) == 1) {
        s.insert(0, c.at(i));
      } else if (f.at(i) == 2) {
        s.append(c.at(i));
      }
    }
    // cout << s << " process " << i << endl;
  }
  if (!normal)
    reverse(s.begin(), s.end());
  cout << s << endl;
}


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

int main() {
  string s;
  int q;
  cin >> s >> q;
  vector<int> t(q), f(q, 0);
  vector<string> c(q, "");
  for (int i = 0; i < q; i++) {
    cin >> t.at(i);
    if (t.at(i) == 2) {
      cin >> f.at(i) >> c.at(i);
    }
  }

  bool normal = true;
  for (int i = 0; i < q; i++) {
    if (t.at(i) == 1) {
      normal = !normal;
    } else if (t.at(i) == 2) {
      if (!normal) {
        if (f.at(i) == 1)
          f.at(i) = 2;
        else if (f.at(i) == 2)
          f.at(i) = 1;
      }
      if (f.at(i) == 1) {
        s.insert(0, c.at(i));
      } else if (f.at(i) == 2) {
        s.append(c.at(i));
      }
    }
    // cout << s << " process " << i << endl;
  }
  if (!normal)
    reverse(s.begin(), s.end());
  cout << s << endl;
}
Problem p02756 (C++)

Buggy submission (status: Time Limit Exceeded):
#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() {
  string s;
  cin >> s;
  ll q;
  cin >> q;
  int count = 0;
  for (ll i = 0; i < q; i++) {
    int t;
    cin >> t;
    if (t == 1)
      count++;
    if (t == 2) {
      int f;
      cin >> f;
      char c;
      cin >> c;
      if (f == 1 && count % 2 == 0 || f == 2 && count % 2 == 1)
        s = c + s;
      if (f == 2 && count % 2 == 0 || f == 1 && count % 2 == 1)
        s = s + c;
    }
  }
  if (count % 2 == 0)
    cout << s << endl;
  else {
    reverse(s.begin(), s.end());
    cout << s << endl;
  }
  return 0;
}

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() {
  string s;
  cin >> s;
  ll q;
  cin >> q;
  int count = 0;
  for (ll i = 0; i < q; i++) {
    int t;
    cin >> t;
    if (t == 1)
      count++;
    if (t == 2) {
      int f;
      cin >> f;
      char c;
      cin >> c;
      if (((f == 1) && (count % 2 == 0)) || ((f == 2) && (count % 2 == 1))) {
        string u;
        u.push_back(c);
        s.insert(0, u);
      }
      if (((f == 2) && (count % 2 == 0)) || ((f == 1) && (count % 2 == 1)))
        s.push_back(c);
    }
  }
  if (count % 2 == 0)
    cout << s << endl;
  else {
    reverse(s.begin(), s.end());
    cout << s << endl;
  }
  return 0;
}
Problem p02756 (C++)

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

typedef long long ll;
const int maxn = 1e5 + 5;
vector<char> fr, ba;
int main() {
  string buff;
  cin >> buff;
  char tem[2];
  int Q;
  cin >> Q;
  bool rev = 0;
  while (Q--) {
    int t, f, c;
    cin >> t;
    if (t == 1) {
      rev ^= 1;
      continue;
    } else {
      cin >> f >> tem;
      f--;
      f ^= rev;
      if (f == 1)
        ba.push_back(*tem);
      else
        fr.push_back(*tem);
    }
  }
  if (rev) {
    swap(fr, ba);
    reverse(buff.begin(), buff.end());
  }
  for (auto x = fr.end() - 1;; x--) {
    cout << *x;
    if (x == fr.begin())
      break;
  }
  cout << buff;
  for (auto x : ba)
    cout << x;
  return 0;
}

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

typedef long long ll;
const int maxn = 1e5 + 5;
vector<char> fr, ba;
int main() {
  string buff;
  cin >> buff;
  char tem[2];
  int Q;
  cin >> Q;
  bool rev = 0;
  while (Q--) {
    int t, f, c;
    cin >> t;
    if (t == 1) {
      rev ^= 1;
      continue;
    } else {
      cin >> f >> tem;
      f--;
      f ^= rev;
      if (f == 1)
        ba.push_back(*tem);
      else
        fr.push_back(*tem);
    }
  }
  if (rev) {
    swap(fr, ba);
    reverse(buff.begin(), buff.end());
  }
  if (fr.size())
    for (auto x = fr.end() - 1;; x--) {
      cout << *x;
      if (x == fr.begin())
        break;
    }
  cout << buff;
  for (auto x : ba)
    cout << x;
  return 0;
}
Problem p02756 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#include <iostream>
#include <math.h>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
using ll = long long;

using Graph = vector<vector<int>>;

int main() {
  string s;
  cin >> s;
  int n;
  cin >> n;

  bool rev = false;
  int t;
  int f;
  char c;

  // vector<int>q;
  rep(i, n) {
    cin >> t;
    // q.push_back(t);
    if (t == 1) {
      rev = !rev;
      continue;
    }

    if (rev) {
      reverse(s.begin(), s.end());
      rev = false;
    }
    cin >> f >> c;
    if (f == 1) {
      s.insert(s.begin(), c);
    } else { // f==2
      s.push_back(c);
    }
  }

  if (rev)
    reverse(s.begin(), s.end());

  cout << s << endl;

  return 0;
}


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

using Graph = vector<vector<int>>;

int main() {
  string s;
  cin >> s;
  int n;
  cin >> n;

  bool rev = false;
  int t;
  int f;
  char c;

  // vector<int>q;
  rep(i, n) {
    cin >> t;
    // q.push_back(t);
    if (t == 1) {
      rev = !rev;
    } else {
      cin >> f >> c;
      if (f == 1) {
        if (rev)
          s.push_back(c);
        else
          s.insert(s.begin(), c);
      } else { // f==2
        if (rev)
          s.insert(s.begin(), c);
        else
          s.push_back(c);
      }
    }
  }

  if (rev)
    reverse(s.begin(), s.end());

  cout << s << endl;

  return 0;
}
Problem p02756 (C++)

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

int main() {
  string S;
  int Q;
  cin >> S >> Q;

  int T, F;
  string C;
  bool rev = false;

  rep(i, Q) {
    cin >> T;
    if (T == 1) {
      rev = !rev;
    } else {
      cin >> F >> C;
      if ((F == 1 && !rev) || (F == 2 && rev))
        S.insert(0, C);
      else
        S = S + C;
    }
  }

  if (rev)
    reverse(S.begin(), S.end());

  cout << S << endl;
}


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

int main() {
  string S;
  int Q;
  cin >> S >> Q;

  int T, F;
  string C;
  bool rev = false;

  rep(i, Q) {
    cin >> T;
    if (T == 1) {
      rev = !rev;
    } else {
      cin >> F >> C;
      if ((F == 1 && !rev) || (F == 2 && rev))
        S.insert(0, C);
      else {
        char D = C.at(0);
        S.push_back(D);
      }
    }
  }

  if (rev)
    reverse(S.begin(), S.end());

  cout << S << endl;
}
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pi 3.14159265359
#define int long long
#define pii pair<int, int>
const int mod = 1e9 + 7;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif
  int n, p;
  string s;
  cin >> n >> p >> s;
  int ans = 0;
  if (p == 2 || p == 5) {
    for (int i = 0; i < n; ++i)
      if ((s[i] - '0') % p == 0)
        ans += i + 1;
    cout << ans << "\n";
    return 0;
  }

  map<int, int> mp;
  mp[0]++;
  int t = 0, p2 = 1;
  for (int i = n - 1; i >= 0; --i) {
    t = (t + (s[i] - '0') * p2) % p;
    ans += mp[t];
    mp[t]++;
    p2 = p2 * 10 % p;
  }

  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pi 3.14159265359
#define int long long
#define pii pair<int, int>
const int mod = 1e9 + 7;

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, p;
  string s;
  cin >> n >> p >> s;
  int ans = 0;
  if (p == 2 || p == 5) {
    for (int i = 0; i < n; ++i)
      if ((s[i] - '0') % p == 0)
        ans += i + 1;
    cout << ans << "\n";
    return 0;
  }

  map<int, int> mp;
  mp[0]++;
  int t = 0, p2 = 1;
  for (int i = n - 1; i >= 0; --i) {
    t = (t + (s[i] - '0') * p2) % p;
    ans += mp[t];
    mp[t]++;
    p2 = p2 * 10 % p;
  }

  cout << ans;
}
Problem p02757 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <string>
using namespace std;
long long a[100010], ten[100010], sum[100010];
map<long long, int> mp;
int main() {
  long long i, n, p, ans = 0;
  string s;
  cin >> n >> p >> s;
  if (p == 2) {
    for (i = 0; i < n; i++) {
      if ((s[i] - '0') % 2 == 0) {
        ans += (i + 1);
      }
    }
  } else if (p == 5) {
    for (i = 0; i < n; i++) {
      if ((s[i] - '0') % 5 == 0) {
        ans += (i + 1);
      }
    }
  } else {
    ten[0] = 1;
    for (i = 1; i <= n; i++) {
      ten[i] = (ten[i - 1] * 10) % p;
    }
    for (i = 0; i < n; i++) {
      long long x = s[i] - '0';
      a[i] = (ten[n - 1 - i] * x) % p;
    }
    sum[0] = 0;
    mp[0]++;
    for (i = 0; i < n; i++) {
      sum[i + 1] = (sum[i] + a[i]) % p;
      ans += mp[sum[i + 1]];
      mp[sum[i + 1]]++;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
#include <map>
#include <string>
using namespace std;
long long a[200010], ten[200010], sum[200010];
map<long long, int> mp;
int main() {
  long long i, n, p, ans = 0;
  string s;
  cin >> n >> p >> s;
  if (p == 2) {
    for (i = 0; i < n; i++) {
      if ((s[i] - '0') % 2 == 0) {
        ans += (i + 1);
      }
    }
  } else if (p == 5) {
    for (i = 0; i < n; i++) {
      if ((s[i] - '0') % 5 == 0) {
        ans += (i + 1);
      }
    }
  } else {
    ten[0] = 1;
    for (i = 1; i <= n; i++) {
      ten[i] = (ten[i - 1] * 10) % p;
    }
    for (i = 0; i < n; i++) {
      long long x = s[i] - '0';
      a[i] = (ten[n - 1 - i] * x) % p;
    }
    sum[0] = 0;
    mp[0]++;
    for (i = 0; i < n; i++) {
      sum[i + 1] = (sum[i] + a[i]) % p;
      ans += mp[sum[i + 1]];
      mp[sum[i + 1]]++;
    }
  }
  cout << ans << endl;
}