Problem p02756 (C++)

Buggy submission (status: Time Limit Exceeded):
// https://atcoder.jp/contests/abc158/tasks/abc158_d
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  string S;
  cin >> S;

  deque<char> lst;
  for (auto &c : S) {
    lst.push_back(c);
  }
  dump(lst);

  ll Q;
  cin >> Q;
  REP(i, Q) {
    ll t;
    cin >> t;
    if (t == 1) {
      // swap(lst.back(),*lst.begin());
      std::reverse(lst.begin(), lst.end());
    } else if (t == 2) {
      ll t2;
      cin >> t2;
      char c;
      cin >> c;
      if (t2 == 1) {
        lst.push_front(c);
      } else {
        lst.push_back(c);
      }
    }
  }

  for (auto &c : lst) {
    cout << c;
  }
  cout << endl;

  return 0;
}

Fixed submission:
// https://atcoder.jp/contests/abc158/tasks/abc158_d
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
#if LOCAL & 01
  std::ifstream in("./test/sample-1.in");
  // std::ifstream in("./input.txt");
  std::cin.rdbuf(in.rdbuf());
#else
  cin.tie(0);
  ios::sync_with_stdio(false);
#endif
  string S;
  cin >> S;
  ll Q;
  cin >> Q;
  struct info {
    ll type = 0;
    ll fb = 0;
    char c = 0;
  };
  vector<info> ql(Q);
  for (auto &&q : ql) {
    cin >> q.type;
    if (q.type == 2) {
      cin >> q.fb >> q.c;
    }
  }

  bool sw_flag = false;
  for (ll i = Q - 1; i >= 0; --i) {
    auto &&q = ql.at(i);
    if (q.type == 1) {
      sw_flag = !sw_flag; // 反転
    } else {
      if (sw_flag) {
        if (q.fb == 1) {
          q.fb = 2;
        } else {
          q.fb = 1;
        }
      }
    }
  }

  if (sw_flag)
    reverse(S.begin(), S.end());
  deque<char> lst;
  for (auto &c : S) {
    lst.push_back(c);
  }
  dump(lst);

  REP(i, Q) {
    auto &q = ql.at(i);
    if (q.type == 1) {
    } else {
      ll t2 = q.fb;
      char c = q.c;
      if (t2 == 1) {
        lst.push_front(c);
      } else {
        lst.push_back(c);
      }
    }
  }

  for (auto &c : lst) {
    cout << c;
  }
  cout << endl;
  return 0;
}

int main_() {
  string S;
  cin >> S;

  deque<char> lst;
  for (auto &c : S) {
    lst.push_back(c);
  }
  dump(lst);

  ll Q;
  cin >> Q;
  REP(i, Q) {
    ll t;
    cin >> t;
    if (t == 1) {
      // swap(lst.back(),*lst.begin());
      std::reverse(lst.begin(), lst.end());
    } else if (t == 2) {
      ll t2;
      cin >> t2;
      char c;
      cin >> c;
      if (t2 == 1) {
        lst.push_front(c);
      } else {
        lst.push_back(c);
      }
    }
  }

  for (auto &c : lst) {
    cout << c;
  }
  cout << endl;

  return 0;
}