Problem p02756 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cstdint>
#include <vector>
#define FOR(i, l, r) for (int i = (l); i < (r); ++i)
#define RFOR(i, l, r) for (int i = (l); i >= (int)(r); i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n - 1, 0)

#define int long long
using namespace std;
const int MX = 1e6;
const int inf = 1e9;

vector<int> ra(MX, 0);
vector<int> par(MX, 0);
using P = pair<int, int>;
using ll = long long;
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (ra[x] < ra[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (ra[x] == ra[y]) {
      ra[x]++;
    }
  }
}

bool same(int x, int y) { return find(x) == find(y); }

int a[MX];
int c[10][1000];
signed main() {
  string s;
  cin >> s;
  int q;
  cin >> q;
  bool isrev = false;
  deque<char> queue;
  rep(i, s.length()) queue.push_back(s[i]);
  rep(i, q) {
    int com;
    cin >> com;
    if (com == 1)
      isrev = !isrev;
    else {
      int pos;
      cin >> pos;
      char c;
      cin >> c;
      if (isrev)
        pos = (pos) % 2 + 1;
      if (pos == 1)
        queue.push_front(c);
      else
        queue.push_back(c);
    }
  }
  if (isrev) {
    rrep(i, s.length() + q) cout << queue[i] << flush;
  } else {
    rep(i, s.length() + q) cout << queue[i] << flush;
  }
  cout << "" << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <cstdint>
#include <vector>
#define FOR(i, l, r) for (int i = (l); i < (r); ++i)
#define RFOR(i, l, r) for (int i = (l); i >= (int)(r); i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n - 1, 0)

#define int long long
using namespace std;
const int MX = 1e6;
const int inf = 1e9;

vector<int> ra(MX, 0);
vector<int> par(MX, 0);
using P = pair<int, int>;
using ll = long long;
const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }

int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (ra[x] < ra[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (ra[x] == ra[y]) {
      ra[x]++;
    }
  }
}

bool same(int x, int y) { return find(x) == find(y); }

int a[MX];
int c[10][1000];
signed main() {
  string s;
  cin >> s;
  int q;
  cin >> q;
  bool isrev = false;
  deque<char> queue;
  rep(i, s.length()) queue.push_back(s[i]);
  rep(i, q) {
    int com;
    cin >> com;
    if (com == 1)
      isrev = !isrev;
    else {
      int pos;
      cin >> pos;
      char c;
      cin >> c;
      if (isrev)
        pos = (pos) % 2 + 1;
      if (pos == 1)
        queue.push_front(c);
      else
        queue.push_back(c);
    }
  }
  if (isrev)
    reverse(queue.begin(), queue.end());
  rep(i, queue.size()) { cout << queue[i] << flush; }
  cout << "" << endl;

  return 0;
}