alexjercan/bugnet huggingface dataset:
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template <typename T = lint> inline T in() {
  T x;
  cin >> x;
  return x;
}
#define INF 1e16
#define REP(i, n) for (lint i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (lint i = 1, i##_len = (n); i <= i##_len; ++i)
#define REPR(i, n) for (lint i = n - 1; i >= 0; --i)
#define REPR1(i, n) for (lint i = n; i > 0; --i)
#define REPI(i, ini, n) for (lint i = (ini), i##_len = (n); i < i##_len; ++i)
#define REPC(i, obj) for (auto i : obj)
#define R_UP(a, b) (((a) + (b)-1) / (b))
#define ALL(obj) (obj).begin(), (obj).end()
signed main() {
  int n;
  scanf("%d", &n);
  list<int> l;
  REP(i, n) {
    char cmd[12];
    scanf("%s", cmd);
    if (!strcmp(cmd, "insert")) {
      int num;
      scanf("%d", &num);
      l.push_front(num);
    } else if (!strcmp(cmd, "delete")) {
      int num;
      scanf("%d", &num);
      auto itr = find_if(l.begin(), l.end(), [num](int n) { return n == num; });
      if (itr != l.end())
        l.erase(itr);
    } else if (!strcmp(cmd, "deleteFirst"))
      l.pop_front();
    else
      l.pop_back();
  }
  REP(i, l.size()) {
    static auto itr = l.begin();
    if (i != 0)
      printf(" ");
    printf("%d", *itr++);
  }
  printf("\\n");
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template <typename T = lint> inline T in() {
  T x;
  cin >> x;
  return x;
}
#define INF 1e16
#define REP(i, n) for (lint i = 0, i##_len = (n); i < i##_len; ++i)
#define REP1(i, n) for (lint i = 1, i##_len = (n); i <= i##_len; ++i)
#define REPR(i, n) for (lint i = n - 1; i >= 0; --i)
#define REPR1(i, n) for (lint i = n; i > 0; --i)
#define REPI(i, ini, n) for (lint i = (ini), i##_len = (n); i < i##_len; ++i)
#define REPC(i, obj) for (auto i : obj)
#define R_UP(a, b) (((a) + (b)-1) / (b))
#define ALL(obj) (obj).begin(), (obj).end()
signed main() {
  int n = in();
  list<int> l;
  REP(i, n) {
    string cmd = in<string>();
    if (cmd == "insert") {
      int num = in();
      l.push_front(num);
    } else if (cmd == "delete") {
      int num = in();
      auto itr = find_if(l.begin(), l.end(), [num](int n) { return n == num; });
      if (itr != l.end())
        l.erase(itr);
    } else if (cmd == "deleteFirst")
      l.pop_front();
    else
      l.pop_back();
  }
  REP(i, l.size()) {
    static auto itr = l.begin();
    if (i != 0)
      cout << ' ';
    cout << *itr++;
  }
  cout << endl;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <string>
#include <unordered_map>

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::list<unsigned> l;

  unsigned i;
  std::unordered_map<std::string, std::function<void(void)>> funcs{
      {"insert",
       [&l, &i]() {
         std::cin >> i;
         l.push_front(i);
       }},
      {"delete",
       [&l, &i]() {
         std::cin >> i;
         auto it = std::find(l.begin(), l.end(), i);
         if (it != l.end())
           l.erase(it);
       }},
      {"deleteFirst", [&l]() { l.erase(l.begin()); }},
      {"deleteLast", [&l]() { l.erase(--(l.end())); }}};

  unsigned n;
  std::string buf;
  std::cin >> n;
  for (; n; n--) {
    std::cin >> buf;
    funcs[buf]();
  }

  std::cout << *(l.begin());
  for (auto it = ++l.begin(); it != l.end(); ++it) {
    std::cout << ' ' << *it;
  }
  std::cout << '\\n';
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <string>
#include <unordered_map>

int main() {
  std::list<unsigned> l;

  unsigned i;
  std::unordered_map<std::string, std::function<void(void)>> funcs{
      {"insert",
       [&l, &i]() {
         std::cin >> i;
         l.push_front(i);
       }},
      {"delete",
       [&l, &i]() {
         std::cin >> i;
         auto it = std::find(l.begin(), l.end(), i);
         if (it != l.end())
           l.erase(it);
       }},
      {"deleteFirst", [&l]() { l.erase(l.begin()); }},
      {"deleteLast", [&l]() { l.erase(--(l.end())); }}};

  unsigned n;
  std::string buf;
  std::cin >> n;
  for (; n; n--) {
    std::cin >> buf;
    funcs[buf]();
  }

  std::cout << *(l.begin());
  for (auto it = ++l.begin(); it != l.end(); ++it) {
    std::cout << ' ' << *it;
  }
  std::cout << '\\n';
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <string>
#include <unordered_map>

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::list<unsigned> l;

  std::unordered_map<std::string, std::function<void(void)>> funcs{
      {"insert",
       [&l]() {
         unsigned i;
         std::cin >> i;
         l.push_front(i);
       }},
      {"delete",
       [&l]() {
         unsigned i;
         std::cin >> i;
         auto it = std::find(l.begin(), l.end(), i);
         if (it != l.end())
           l.erase(it);
       }},
      {"deleteFirst", [&l]() { l.erase(l.begin()); }},
      {"deleteLast", [&l]() { l.erase(--(l.end())); }}};

  unsigned n;
  std::string buf;
  std::cin >> n;
  for (; n; n--) {
    std::cin >> buf;
    funcs[buf]();
  }

  std::cout << *(l.begin());
  for (auto it = ++l.begin(); it != l.end(); ++it) {
    std::cout << ' ' << *it;
  }
  std::cout << '\\n';
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <string>
#include <unordered_map>

int main() {
  std::list<unsigned> l;

  std::unordered_map<std::string, std::function<void(void)>> funcs{
      {"insert",
       [&l]() {
         unsigned i;
         std::cin >> i;
         l.push_front(i);
       }},
      {"delete",
       [&l]() {
         unsigned i;
         std::cin >> i;
         auto it = std::find(l.begin(), l.end(), i);
         if (it != l.end())
           l.erase(it);
       }},
      {"deleteFirst", [&l]() { l.erase(l.begin()); }},
      {"deleteLast", [&l]() { l.erase(--(l.end())); }}};

  unsigned n;
  std::string buf;
  std::cin >> n;
  for (; n; n--) {
    std::cin >> buf;
    funcs[buf]();
  }

  std::cout << *(l.begin());
  for (auto it = ++l.begin(); it != l.end(); ++it) {
    std::cout << ' ' << *it;
  }
  std::cout << '\\n';
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  list<ll> lst;
  map<ll, int> num;
  REP(i, n) {
    string s;
    cin >> s;
    if (s == "insert") {
      ll x;
      cin >> x;
      lst.push_front(x);
      num[x]++;
    }
    if (s == "delete") {
      ll x;
      cin >> x;
      if (num[x]) {
        list<ll>::iterator itr;
        for (itr = lst.begin(); itr != lst.end(); itr++) {
          if (*itr == x) {
            lst.erase(itr);
            num[x]--;
            break;
          }
        }
      }
    }
    if (s == "deleteFirst") {
      num[*(lst.begin())]--;
      lst.pop_front();
    }
    if (s == "deleteLast") {
      num[*(--lst.end())]--;
      lst.pop_back();
    }
  }
  list<ll>::iterator itr;
  for (itr = lst.begin(); itr != lst.end(); itr++) {
    if (itr != --lst.end())
      cout << *itr << " ";
    else
      cout << *itr << "\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  string hoge;
  getline(cin, hoge);
  vector<string> com(n);
  REP(i, n) { getline(cin, com[i]); }
  vector<ll> key;
  map<ll, int> num;
  REP(i, n) {
    if (com[i] == "deleteFirst") {
      num[key[0]]--;
      key.erase(key.begin());
    }
    if (com[i] == "deleteLast") {
      num[key.size() - 1]--;
      key.pop_back();
    }
    if (com[i][0] == 'i') {
      num[stoi(com[i].substr(7))]++;
      key.insert(key.begin(), stoi(com[i].substr(7)));
    }
    if (com[i][0] == 'd' && com[i][6] == ' ' && num[stoi(com[i].substr(7))]) {
      num[stoi(com[i].substr(7))]--;
      REP(j, (int)key.size()) {
        if (key[j] == stoi(com[i].substr(7))) {
          key.erase(key.begin() + j);
          break;
        }
      }
    }
  }
  int N = key.size();
  REP(i, N) {
    if (i < N - 1)
      cout << key[i] << " ";
    else
      cout << key[i] << "\\n";
  }

  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Node {
  int key;
  Node *prev, *next;
};

Node *NIL;

void init() {
  NIL = new Node();
  NIL->prev = NIL;
  NIL->next = NIL;
}

void insert(int key) {
  Node *x = new Node();
  x->key = key;
  x->next = NIL->next;
  NIL->next->prev = x;
  NIL->next = x;
  x->prev = NIL;
}

Node *listSearch(int key) {
  Node *cur = NIL->next;
  while (cur->key != key && cur != NIL) {
    cur = cur->next;
  }
  return cur;
}

void deleteNode(Node *tmp) {
  if (tmp == NIL)
    return;
  tmp->next->prev = tmp->prev;
  tmp->prev->next = tmp->next;
  delete tmp;
}

void deleteFirst() { deleteNode(NIL->next); }

void deleteLast() { deleteNode(NIL->prev); }

void deleteKey(int key) { deleteNode(listSearch(key)); }

void print() {
  Node *cur = NIL->next;
  while (cur != NIL) {
    if (cur != NIL->next)
      printf(" ");
    printf("%d", cur->key);
    cur = cur->next;
  }
  printf("\\n");
}

int main() {
  int n;
  char S[20];
  int key;

  init();
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s %d", S, &key);
    if (S[0] == 'i')
      insert(key);
    if (S[0] == 'd') {
      if (S[6] == 'F')
        deleteFirst();
      else if (S[6] == 'L')
        deleteLast();
      else
        deleteKey(key);
    }
  }

  print();
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Node {
  int key;
  Node *prev, *next;
};

Node *NIL;

void init() {
  NIL = new Node();
  NIL->prev = NIL;
  NIL->next = NIL;
}

void insert(int key) {
  Node *x = new Node();
  x->key = key;
  x->next = NIL->next;
  NIL->next->prev = x;
  NIL->next = x;
  x->prev = NIL;
}

Node *listSearch(int key) {
  Node *cur = NIL->next;
  while (cur->key != key && cur != NIL) {
    cur = cur->next;
  }
  return cur;
}

void deleteNode(Node *tmp) {
  tmp->next->prev = tmp->prev;
  tmp->prev->next = tmp->next;
  delete tmp;
}

void deleteFirst() { deleteNode(NIL->next); }

void deleteLast() { deleteNode(NIL->prev); }

void deleteKey(int key) { deleteNode(listSearch(key)); }

void print() {
  Node *cur = NIL->next;
  while (cur != NIL) {
    if (cur != NIL->next)
      printf(" ");
    printf("%d", cur->key);
    cur = cur->next;
  }
  printf("\\n");
}

int main() {
  int n;
  char S[20];
  int key;

  init();
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s %d", S, &key);
    if (S[0] == 'i')
      insert(key);
    if (S[0] == 'd') {
      if (S[6] == 'F')
        deleteFirst();
      else if (S[6] == 'L')
        deleteLast();
      else
        deleteKey(key);
    }
  }

  print();
  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

const int INF = 1001001000;
const int MOD = (int)1e9 + 7;

using ll = long long;

using namespace std;

int main() {
  list<int> lst;
  string str;
  int n;
  int x;
  list<int>::iterator itr;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> str;
    if (str == "insert") {
      cin >> x;
      lst.push_front(x);
    } else if (str == "delete") {
      cin >> x;
      for (itr = lst.begin(); itr != lst.end(); ++itr) {
        if (*itr == x) {
          lst.erase(itr);
          break;
        }
      }
    } else if (str == "deleteFirst") {
      lst.pop_front();
    } else {
      lst.pop_back();
    }
  }

  auto itr2 = lst.end();
  itr2--;

  for (itr = lst.begin(); itr != itr2; ++itr) {
    cout << *itr << " ";
  }
  cout << lst.back() << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

const int INF = 1001001000;
const int MOD = (int)1e9 + 7;

using ll = long long;

using namespace std;

int main() {
  list<int> lst;
  string str;
  int n;
  int x;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> str;
    if (str == "insert") {
      cin >> x;
      lst.push_front(x);
    } else if (str == "delete") {
      cin >> x;
      for (auto itr = lst.begin(); itr != lst.end(); ++itr) {
        if (*itr == x) {
          lst.erase(itr);
          break;
        }
      }
    } else if (str == "deleteFirst") {
      lst.pop_front();
    } else if (str == "deleteLast") {
      lst.pop_back();
    }
  }

  auto itr2 = lst.begin();
  for (int i = 0; i < lst.size() - 1; ++i) {
    ++itr2;
  }

  for (auto itr = lst.begin(); itr != itr2; ++itr) {
    cout << *itr << " ";
  }
  cout << lst.back() << endl;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <sstream>
using namespace std;

int main(int argc, char *argv[]) {
  std::ios_base::sync_with_stdio(false);
  int n, key;
  list<int> li;
  string str;
  list<int>::iterator itli;

  cin >> n;

  while (n--) {
    cin >> str;

    if (str[6] == 'F')
      li.pop_front();
    else if (str[6] == 'L')
      li.pop_back();
    else {
      cin >> key;
      if (str[0] == 'i')
        li.push_front(key);
      else {
        itli = find(li.begin(), li.end(), key);
        if (itli != li.end())
          li.erase(itli);
      }
    }
  }

  itli = li.begin();
  cout << *itli++;
  while (itli != li.end()) {
    cout << " " << *itli++;
  }
  cout << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <string>
using namespace std;

int main() {
  deque<int> a;
  int num, n;
  string command;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> command;
    if ((command.length() >= 6) && (command[6] == 'F')) {
      a.pop_front();
    } else if ((command.length() >= 6) && (command[6] == 'L')) {
      a.pop_back();
    } else if (command[0] == 'i') {
      cin >> num;
      a.push_front(num);
    } else if (command == "delete") {
      cin >> num;
      deque<int>::iterator iter;
      iter = find(a.begin(), a.end(), num);
      if (iter != a.end())
        a.erase(iter);
    }
  }

  for (int i = 0; i < a.size(); i++) {
    if (i != 0)
      cout << " ";
    cout << a[i];
  }
  cout << endl;

  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

struct Node {
  int key;
  Node *prev, *next;
};

Node *nil;

void init() {
  nil = (Node *)malloc(sizeof(Node));
  nil->next = nil;
  nil->prev = nil;
}

void insert(int key) {
  Node *x = (Node *)malloc(sizeof(Node));
  x->key = key;
  x->next = nil->next;
  nil->next->prev = x;
  nil->next = x;
  x->prev = nil;
}

Node *listsearch(int key) {
  Node *cur = nil->next;
  while (cur != nil && cur->key != key) {
    cur = cur->next;
  }
  return cur;
}

void deleteNode(Node *t) {
  if (t == nil) {
    return;
  }
  t->next->prev = t->prev;
  t->prev->next = t->next;
  free(t);
}

void deleteFirst() { deleteNode(nil->next); }

void deleteLast() { deleteNode(nil->prev); }

void deleteKey(int key) { deleteNode(listsearch(key)); }

void print() {
  int j = 0;
  Node *cur = nil->next;
  while (1) {
    if (cur == nil) {
      break;
    }
    if (j++ > 0) {
      printf(" ");
    }
    printf("%d", cur->key);
    cur = cur->next;
  }
  putchar('\\n');
}

int main() {
  char com[20];
  int n, i, key;
  init();
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%s %d", com, &key);
    if (com[0] == 'i') {
      insert(key);
    } else {
      if (com[6] == 'L') {
        deleteLast();
      } else if (com[6] == 'F') {
        deleteFirst();
      } else {
        deleteKey(key);
      }
    }
  }
  print();

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

struct Node {
  int key;
  Node *prev, *next;
};

Node *nil;

void init() {
  nil = (Node *)malloc(sizeof(Node));
  nil->next = nil;
  nil->prev = nil;
}

void insert(int key) {
  Node *x = (Node *)malloc(sizeof(Node));
  x->key = key;
  x->next = nil->next;
  nil->next->prev = x;
  nil->next = x;
  x->prev = nil;
}

Node *listsearch(int key) {
  Node *cur = nil->next;
  while (cur != NULL && cur->key != key) {
    cur = cur->next;
  }
  return cur;
}

void deleteNode(Node *t) {
  if (t == nil) {
    return;
  }
  t->next->prev = t->prev;
  t->prev->next = t->next;
  free(t);
}

void deleteFirst() { deleteNode(nil->next); }

void deleteLast() { deleteNode(nil->prev); }

void deleteKey(int key) { deleteNode(listsearch(key)); }

void print() {
  int j = 0;
  Node *cur = nil->next;
  while (1) {
    if (cur == nil) {
      break;
    }
    if (j++ > 0) {
      printf(" ");
    }
    printf("%d", cur->key);
    cur = cur->next;
  }
  putchar('\\n');
}

int main() {
  char com[20];
  int n, i, key;
  init();
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%s %d", com, &key);
    if (com[0] == 'i') {
      insert(key);
    } else {
      if (com[6] == 'L') {
        deleteLast();
      } else if (com[6] == 'F') {
        deleteFirst();
      } else {
        deleteKey(key);
      }
    }
  }
  print();

  return 0;
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
struct lis {
  int d;
  lis *pr;
  lis *ne;
};
lis he;
int n;
void ins();
void del();
void delf();
void dell();
int main() {
  cin >> n;
  string ord;
  lis bu = {-1, &he, &he};
  he = bu;
  for (int i = 0; i < n; i++) {

    cin >> ord;

    if (ord == "insert")
      ins();
    else if (ord == "delete")
      del();
    else if (ord == "deleteFirst")
      delf();
    else if (ord == "deleteLast")
      dell();
  }
  for (lis *i = he.ne; i != &he; i = i->ne) {
    cout << i->d;
    if (i->ne != &he) {
      cout << " ";
    }
  }
  cout << endl;
  return 0;
}
void ins() {
  int x;
  cin >> x;
  lis *p;
  p = new lis;
  p->d = x;
  p->ne = he.ne;
  p->pr = &he;
  he.ne->pr = p;
  he.ne = p;
}
void del() {
  int x;
  cin >> x;
  for (lis *i = he.ne; i != &he; i = i->ne) {
    if (i->d == x) {
      i->pr->ne = i->ne;
      i->ne->pr = i->pr;
      free(i);
      return;
    }
  }
}
void dell() {
  lis *i = he.pr;
  i->pr->ne = i->ne;
  i->ne->pr = i->pr;
  free(i);
}
void delf() {
  lis *i = he.ne;
  i->pr->ne = i->ne;
  i->ne->pr = i->pr;
  free(i);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct lis {
  int d;
  lis *pr;
  lis *ne;
};
lis he;
int n;
void ins();
void del();
void delf();
void dell();
int main() {
  cin >> n;
  string ord;
  lis bu = {-1, &he, &he};
  he = bu;
  for (int i = 0; i < n; i++) {

    cin >> ord;

    if (ord == "insert")
      ins();
    else if (ord == "delete")
      del();
    else if (ord == "deleteFirst")
      delf();
    else if (ord == "deleteLast")
      dell();
  }
  for (lis *i = he.ne; i != &he; i = i->ne) {
    cout << i->d;
    if (i->ne != &he) {
      cout << " ";
    }
  }
  cout << endl;
  return 0;
}
void ins() {
  int x;
  cin >> x;
  lis *p;
  p = new lis;
  p->d = x;
  p->ne = he.ne;
  p->pr = &he;
  he.ne->pr = p;
  he.ne = p;
}
void del() {
  int x;
  cin >> x;
  for (lis *i = he.ne; i != &he; i = i->ne) {
    if (i->d == x) {
      i->pr->ne = i->ne;
      i->ne->pr = i->pr;
      free(i);
      return;
    }
  }
}
void dell() {
  for (lis *i = he.ne; i != &he; i = i->ne) {
    if (i->ne == &he) {
      i->pr->ne = i->ne;
      i->ne->pr = i->pr;
      free(i);
    }
  }
}
void delf() {
  lis *i = he.ne;
  i->pr->ne = i->ne;
  i->ne->pr = i->pr;
  free(i);
}
Problem p02265 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
struct l {
  int v;
  l *prv;
  l *next;
};
void DeleteNode(l *del, l **first, l **last) {
  if (del->prv != NULL)
    del->prv->next = del->next;
  else
    *first = del->next;

  if (del->next != NULL)
    del->next->prv = del->prv;
  else
    *last = del->prv;

  delete del;
  del = NULL;
}
int main() {
  l *t;
  l *first = NULL;
  l *last = NULL;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long v;
    char s[12];
    cin >> s;
    if (s[0] == 'i') {
      l *t = new l();
      t->prv = NULL;
      t->next = NULL;
      cin >> t->v;
      if (last == NULL) {
        last = t;
      } else {
        first->prv = t;
      }
      t->next = first;
      first = t;
      continue;
    }

    if (first == NULL)
      continue;

    if (s[6] == 'F') {
      DeleteNode(first, &first, &last);
    } else if (s[6] == 'L') {
      DeleteNode(last, &first, &last);
    } else {
      long a;
      cin >> a;

      for (l *t = first; t != NULL; t = t->next) {
        if (t->v == a) {
          DeleteNode(t, &first, &last);
          break;
        }
      }
    }
  }

  t = first;
  while (t != NULL) {
    l *old = t;
    cout << t->v << (t->next != NULL ? " " : "\\n");
    t = t->next;
    delete old;
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
struct l {
  int v;
  l *prv;
  l *next;
};
void DeleteNode(l *del, l **first, l **last) {
  if (del->prv != NULL)
    del->prv->next = del->next;
  else
    *first = del->next;

  if (del->next != NULL)
    del->next->prv = del->prv;
  else
    *last = del->prv;

  delete del;
  del = NULL;
}
int main() {
  l *t;
  l *first = NULL;
  l *last = NULL;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long v;
    string s;
    cin >> s;
    if (s[0] == 'i') {
      l *t = new l();
      t->prv = NULL;
      t->next = NULL;
      cin >> t->v;
      if (last == NULL) {
        last = t;
      } else {
        first->prv = t;
      }
      t->next = first;
      first = t;
      continue;
    }

    if (first == NULL)
      continue;

    l *delPrv = NULL;
    if (s[6] == 'F') {
      DeleteNode(first, &first, &last);
    } else if (s[6] == 'L') {
      DeleteNode(last, &first, &last);
    } else {
      long a;
      cin >> a;

      for (l *t = first; t != NULL; t = t->next) {
        if (t->v == a) {
          DeleteNode(t, &first, &last);
          break;
        }
      }
    }
  }

  t = first;
  while (t != NULL) {
    l *old = t;
    cout << t->v << (t->next != NULL ? " " : "\\n");
    t = t->next;
    delete old;
  }
}