Problem p02756 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdarg>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>

using namespace std;
#define INF 2e9 + 9
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
#define Int int64_t
#define pll pair<Int, Int>
template <class T> using pql = priority_queue<T, vector<T>>;
template <class T> using pq = priority_queue<T, vector<T>, greater<T>>;
#define ll(n)                                                                  \
  Int n;                                                                       \
  cin >> n;
#define LL(...)                                                                \
  Int __VA_ARGS___;                                                            \
  cin >> __VA_ARGS___;
#define OUT(...) cout << __VA_ARGS___;
#define RP(n, i, s) for (Int i = s; i < n; i++)
#define REP(n, i) for (Int i = 0; i < n; i++)
#define REPe(n, i) for (Int i = 1; i <= n; i++)
#define RPe(n, i, s) for (Int i = s; i <= n; i++)
#define upd_max(maxer, updater)                                                \
  if (maxer < updater)                                                         \
    maxer = updater;

void ansYes(bool flag) {
  if (flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

void ansYES(bool flag) {
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

struct node {
  Int to;
  Int length;
};

vector<Int> divisor(Int n) {
  vector<Int> ret;
  for (Int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  return (ret);
}

/*
vector<vector<int>> to;
vector<int> flag;
vector<int> c;


Int dfs(int from, int mae) {
    flag[from] = 1;
    Int mini = 0;
    for (auto next : to[from]) {
        if (next == mae) continue;
        if (flag[next] == 1) {
            return -1;
        } else {
            mini = min(mini, dfs(next, from));
        }
    }
    return mini;
}*/

int dx8[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

struct datas {
  Int max;
  Int size;
};

int main(void) {
  string s;
  int q;
  cin >> s >> q;
  int t, f;
  char c;
  bool mae = true;
  REP(q, i) {
    cin >> t;
    if (t == 1)
      mae = !mae;
    else {
      cin >> f >> c;
      if (mae) {
        if (f == 1) {
          s = c + s;
        } else {
          s.push_back(c);
        }
      } else {
        if (f == 1) {
          s.push_back(c);
        } else {
          s = c + s;
        }
      }
    }
  }
  if (!mae)
    reverse(s.begin(), s.end());
  cout << s << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdarg>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>

using namespace std;
#define INF 2e9 + 9
#define MOD 1000000007
#define ALL(v) v.begin(), v.end()
#define Int int64_t
#define pll pair<Int, Int>
template <class T> using pql = priority_queue<T, vector<T>>;
template <class T> using pq = priority_queue<T, vector<T>, greater<T>>;
#define ll(n)                                                                  \
  Int n;                                                                       \
  cin >> n;
#define LL(...)                                                                \
  Int __VA_ARGS___;                                                            \
  cin >> __VA_ARGS___;
#define OUT(...) cout << __VA_ARGS___;
#define RP(n, i, s) for (Int i = s; i < n; i++)
#define REP(n, i) for (Int i = 0; i < n; i++)
#define REPe(n, i) for (Int i = 1; i <= n; i++)
#define RPe(n, i, s) for (Int i = s; i <= n; i++)
#define upd_max(maxer, updater)                                                \
  if (maxer < updater)                                                         \
    maxer = updater;

void ansYes(bool flag) {
  if (flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

void ansYES(bool flag) {
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

struct node {
  Int to;
  Int length;
};

vector<Int> divisor(Int n) {
  vector<Int> ret;
  for (Int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  return (ret);
}

/*
vector<vector<int>> to;
vector<int> flag;
vector<int> c;


Int dfs(int from, int mae) {
    flag[from] = 1;
    Int mini = 0;
    for (auto next : to[from]) {
        if (next == mae) continue;
        if (flag[next] == 1) {
            return -1;
        } else {
            mini = min(mini, dfs(next, from));
        }
    }
    return mini;
}*/

int dx8[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

struct datas {
  Int max;
  Int size;
};

int main(void) {
  string s;
  int q;
  cin >> s >> q;
  int t, f;
  char c;
  bool mae = true;
  REP(q, i) {
    cin >> t;
    if (t == 1)
      mae = !mae;
    else {
      cin >> f >> c;
      if (mae) {
        if (f == 1) {
          s.insert(s.begin(), c);
        } else {
          s.push_back(c);
        }
      } else {
        if (f == 1) {
          s.push_back(c);
        } else {
          s = c + s;
        }
      }
    }
  }
  if (!mae)
    reverse(s.begin(), s.end());
  cout << s << endl;
}