Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
// #include<chrono>
#include <array>
#include <assert.h>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iterator>
#include <list>
#include <map>
#include <random>
#include <stack>
#include <unordered_set>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*
cout<<setprecision(20)
cin.tie(0);
ios::sync_with_stdio(false);
*/
const llint mod = 1e9 + 7;
const llint big = 2.19e15 + 1;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-15;
template <class T, class U> void mineq(T &a, U b) {
  if (a > b) {
    a = b;
  }
}
template <class T, class U> void maxeq(T &a, U b) {
  if (a < b) {
    a = b;
  }
}
llint gcd(llint a, llint b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
// llint lcm(llint a,llint b){return a/gcd(a,b)*b;}
template <class T> void SO(T &ve) { sort(ve.begin(), ve.end()); }
template <class T> void REV(T &ve) { reverse(ve.begin(), ve.end()); }
template <class T> llint LBI(vector<T> &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> llint UBI(vector<T> &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  deque<int> que;
  while (n--) {
    string cm;
    int x;
    cin >> cm;
    if (cm == "insert") {
      cin >> x;
      que.puf(x);
    }
    if (cm == "delete") {
      cin >> x;
      auto itr = find(que.begin(), que.end(), x);
      if (itr != que.end()) {
        que.erase(itr);
      }
    }
    if (cm == "deleteFirst") {
      que.pof();
    }
    if (cm == "deleteLast") {
      que.pob();
    }
  }
  n = que.size();
  for (int i = 0; i < n; i++) {
    cout << que[i] << (i + 1 < n ? " " : "\\n");
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
/*
cout<<setprecision(20)
cin.tie(0);
ios::sync_with_stdio(false);
*/
const llint mod = 1e9 + 7;
const llint big = 2.19e15 + 1;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-15;
template <class T, class U> void mineq(T &a, U b) {
  if (a > b) {
    a = b;
  }
}
template <class T, class U> void maxeq(T &a, U b) {
  if (a < b) {
    a = b;
  }
}
llint gcd(llint a, llint b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
// llint lcm(llint a,llint b){return a/gcd(a,b)*b;}
template <class T> void SO(T &ve) { sort(ve.begin(), ve.end()); }
template <class T> void REV(T &ve) { reverse(ve.begin(), ve.end()); }
template <class T> llint LBI(vector<T> &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> llint UBI(vector<T> &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}
int main(void) {
  int i, q;
  cin >> q;
  // これリストいらない....
  vector<int> insa(q);
  map<int, deque<int>> kai; // 何番目の要素があるか
  set<int> aru;
  while (q--) {
    string ope;
    int num;
    cin >> ope;
    if (ope == "insert") {
      cin >> num;
      insa[q] = num;
      kai[num].puf(q);
      aru.ins(q);
    }
    if (ope == "delete") {
      cin >> num;
      if (kai.count(num) == 0) {
        continue;
      }
      if (kai[num].size() == 0) {
        continue;
      }
      aru.era(*kai[num].begin());
      kai[num].pof();
    }
    if (ope == "deleteFirst") {
      int num = insa[*aru.begin()];
      if (kai.count(num) == 0) {
        continue;
      }
      if (kai[num].size() == 0) {
        continue;
      }
      aru.era(*kai[num].begin());
      kai[num].pof();
    }
    if (ope == "deleteLast") {
      int num = insa[*prev(aru.end())];
      if (kai.count(num) == 0) {
        continue;
      }
      if (kai[num].size() == 0) {
        continue;
      }
      aru.era(*prev(kai[num].end()));
      kai[num].pob();
    }
  }
  for (auto it : aru) {
    cout << insa[it];
    if (it != *prev(aru.end())) {
      cout << " ";
    }
  }
  cout << endl;
}