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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const string com[4] = {"insert", "delete", "deleteFirst", "deleteLast"};

int main() {
  ios_base::sync_with_stdio(0);
  deque<int> deq;

  int n;
  cin >> n;
  rep(i, n) {
    string op = "";
    cin >> op;
    rep(j, 4) {
      if (op == com[j]) {
        if (j == 0 || j == 1) {
          int x;
          cin >> x;
          if (j == 0)
            deq.push_front(x);
          else {
            deque<int>::iterator it = deq.begin();
            for (; it != deq.end(); it++) {
              int curr = (*it);
              if (curr == x) {
                deq.erase(it);
                break;
              } // end if
            }   // end for
          }     // end if
        } else if (j == 2) {
          deq.pop_front();
        } else { // if (j == 3 )
          deq.pop_back();
        } // end if
      }   // end if
    }     // end rep
  }       // end rep

  rep(i, deq.size()) {
    cout << deq[i] << (i != deq.size() - 1 ? ' ' : '\\n');
  } // end rep

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const string com[4] = {"insert", "delete", "deleteFirst", "deleteLast"};

int main() {
  deque<int> deq;

  int n;
  cin >> n;
  rep(i, n) {
    string op = "";
    cin >> op;
    rep(j, 4) {
      if (op == com[j]) {
        if (j == 0 || j == 1) {
          int x;
          cin >> x;
          if (j == 0)
            deq.push_front(x);
          else {
            deque<int>::iterator it = deq.begin();
            for (; it != deq.end(); it++) {
              int curr = (*it);
              if (curr == x) {
                deq.erase(it);
                break;
              } // end if
            }   // end for
          }     // end if
        } else if (j == 2) {
          deq.pop_front();
        } else { // if (j == 3 )
          deq.pop_back();
        } // end if
      }   // end if
    }     // end rep
  }       // end rep

  rep(i, deq.size()) {
    cout << deq[i] << (i != deq.size() - 1 ? ' ' : '\\n');
  } // end rep

  return 0;
}