Problem p02756 (C++)

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define pii pair<int, int>
#define ld long double

ostream &operator<<(ostream &a, const vector<int> &b) {
  for (auto k : b)
    cout << k << " ";
  return a;
}

#ifdef LOCAL
#define dbg(x) cout << #x << " : " << (x) << "\n";
const int INF = 1e18;
//    const int mod = 2600000069;
//    const int p = 10;
//    const ld PI = 3.1415926535;
#else
#define dbg(x)
const int INF = 1e18;
//    const int mod = 2600000069;
//    const int p = 179;
//    const ld PI = 3.1415926535;
#endif

// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,sse3,sse4")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("fast-math")
// #pragma GCC target("avx2")
// #pragma GCC optimize("section-anchors")
// #pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
// #pragma GCC optimize("vpt")
// #pragma GCC optimize("rename-registers")
// #pragma GCC optimize("move-loop-invariants")
// #pragma GCC optimize("unswitch-loops")
// #pragma GCC optimize("function-sections")
// #pragma GCC optimize("data-sections")

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
#else
  //        freopen("capitals.in", "r", stdin);
  //        freopen("capitals.out", "w", stdout);
#endif
  string s;
  cin >> s;
  int n = s.size();
  int q;
  cin >> q;
  int t, f;
  char c;
  bool rev = 0;
  vector<pair<bool, char>> a;
  for (int i = 0; i < q; i++) {
    cin >> t;
    if (t == 1)
      rev ^= 1;
    else {
      cin >> f >> c;
      if (f == 1) {
        if (rev)
          a.pb({1, c});
        else
          a.pb({0, c});
      } else {
        if (rev)
          a.pb({0, c});
        else
          a.pb({1, c});
      }
    }
  }
  if (rev) {
    reverse(all(s));
    for (int i = 0; i < q; i++)
      a[i].first ^= 1;
  }
  for (int i = q - 1; i >= 0; i--)
    if (!a[i].first)
      cout << a[i].second;
  //    cout << "\n";
  cout << s;
  //    cout << "\n";
  for (int i = 0; i < q; i++)
    if (a[i].first)
      cout << a[i].second;
}
/*

*/


Fixed submission:
#include <chrono>

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define pii pair<int, int>
#define ld long double

ostream &operator<<(ostream &a, const vector<int> &b) {
  for (auto k : b)
    cout << k << " ";
  return a;
}

#ifdef LOCAL
#define dbg(x) cout << #x << " : " << (x) << "\n";
const int INF = 1e18;
//    const int mod = 2600000069;
//    const int p = 10;
//    const ld PI = 3.1415926535;
#else
#define dbg(x)
const int INF = 1e18;
//    const int mod = 2600000069;
//    const int p = 179;
//    const ld PI = 3.1415926535;
#endif

// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,sse3,sse4")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("fast-math")
// #pragma GCC target("avx2")
// #pragma GCC optimize("section-anchors")
// #pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
// #pragma GCC optimize("vpt")
// #pragma GCC optimize("rename-registers")
// #pragma GCC optimize("move-loop-invariants")
// #pragma GCC optimize("unswitch-loops")
// #pragma GCC optimize("function-sections")
// #pragma GCC optimize("data-sections")

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifdef LOCAL
#else
  //        freopen("capitals.in", "r", stdin);
  //        freopen("capitals.out", "w", stdout);
#endif
  string s;
  cin >> s;
  int n = s.size();
  int q;
  cin >> q;
  int t, f;
  char c;
  bool rev = 0;
  vector<pair<bool, char>> a;
  for (int i = 0; i < q; i++) {
    cin >> t;
    if (t == 1)
      rev ^= 1;
    else {
      cin >> f >> c;
      if (f == 1) {
        if (rev)
          a.pb({1, c});
        else
          a.pb({0, c});
      } else {
        if (rev)
          a.pb({0, c});
        else
          a.pb({1, c});
      }
    }
  }
  q = a.size();
  if (rev) {
    reverse(all(s));
    for (int i = 0; i < q; i++)
      a[i].first ^= 1;
  }
  for (int i = q - 1; i >= 0; i--)
    if (!a[i].first)
      cout << a[i].second;
  //    cout << "\n";
  cout << s;
  //    cout << "\n";
  for (int i = 0; i < q; i++)
    if (a[i].first)
      cout << a[i].second;
}
/*

*/