Problem p02756 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
using Complex = complex<double>;
using ll = long long;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll inf = 1145141919810364364LL;
double PI = acos(-1.0);
#define all(x) (x).begin(), (x).end()
#define puts(x) cout << x << endl
#define rep(i, m, n) for (ll i = m; i < n; ++i)
#define pb push_back
#define fore(i, a) for (auto &i : a)
#define rrep(i, m, n) for (ll i = m; i >= n; --i)

map<int, char> mp;
int nxt[101010];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string S;
  cin >> S;
  int Q;
  cin >> Q;

  int tp = -1;
  int bk = -2;
  int cnt = 0;
  for (int i = 0; i < Q; i++) {
    int q;
    cin >> q;
    if (q == 1) {
      swap(tp, bk);
    } else {
      int f;
      char c;
      cin >> f >> c;
      mp[cnt] = c;
      if (f == 1) {
        nxt[cnt] = tp;
        tp = cnt;
      } else {
        nxt[cnt] = bk;
        bk = cnt;
      }
      cnt++;
    }
  }

  int fl = 0;
  string s1, s2;
  while (1) {
    if (tp < 0) {
      if (tp == -2)
        fl = 1;
      break;
    }
    s1.pb(mp[tp]);
    tp = nxt[tp];
  }
  while (1) {
    if (bk < 0)
      break;
    s2.pb(mp[bk]);
    bk = nxt[bk];
  }
  string ans;
  ans += s1;
  if (fl == 1)
    reverse(all(S));
  ans += S;
  reverse(all(s2));
  ans += s2;
  puts(ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
using namespace std;
using Complex = complex<double>;
using ll = long long;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const ll inf = 1145141919810364364LL;
double PI = acos(-1.0);
#define all(x) (x).begin(), (x).end()
#define puts(x) cout << x << endl
#define rep(i, m, n) for (ll i = m; i < n; ++i)
#define pb push_back
#define fore(i, a) for (auto &i : a)
#define rrep(i, m, n) for (ll i = m; i >= n; --i)

map<int, char> mp;
int nxt[202020];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  string S;
  cin >> S;
  int Q;
  cin >> Q;

  int tp = -1;
  int bk = -2;
  int cnt = 0;
  for (int i = 0; i < Q; i++) {
    int q;
    cin >> q;
    if (q == 1) {
      swap(tp, bk);
    } else {
      int f;
      char c;
      cin >> f >> c;
      mp[cnt] = c;
      if (f == 1) {
        nxt[cnt] = tp;
        tp = cnt;
      } else {
        nxt[cnt] = bk;
        bk = cnt;
      }
      cnt++;
    }
  }

  int fl = 0;
  string s1, s2;
  while (1) {
    if (tp < 0) {
      if (tp == -2)
        fl = 1;
      break;
    }
    s1.pb(mp[tp]);
    tp = nxt[tp];
  }
  while (1) {
    if (bk < 0)
      break;
    s2.pb(mp[bk]);
    bk = nxt[bk];
  }
  string ans;
  ans += s1;
  if (fl == 1)
    reverse(all(S));
  ans += S;
  reverse(all(s2));
  ans += s2;
  puts(ans);

  return 0;
}