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

#include <iostream>
#include <string>

struct DLList {

  int before, next, name, x;
};

void insertx(DLList *a, int &first, int &end, int &cou, int &x) {
  if (first == end && first == -1) {
    a[cou].next = -1;
    a[cou].before = -1;
    first = cou;
    end = cou;
    a[cou].x = x;
  } else {
    a[first].before = cou;
    a[cou].next = first;
    a[cou].before = -1;
    first = cou;
    a[cou].x = x;
  }
}

void deletex(DLList *a, int &first, int &end, int &cou, int &x) {

  int i = first;
  int b, n;
  while (i != -1) {
    if (a[i].x == x) {
      b = a[i].before;
      n = a[i].next;
      if (i != first && i != end) {
        a[b].next = n;
        a[n].before = b;
      } else if (i == first) {
        a[n].before = -1;
        first = n;
      } else {
        a[b].next = -1;
        end = b;
      }
      break;
    } else {
      i = a[i].next;
    }
  }
}

void deleteFirst(DLList *a, int &first, int &end) {
  int n = a[first].next;
  if (first == end) {
    int b = a[end].before;
    if (b >= 0) {
      a[b].next = -1;
    }
    if (n >= 0) {
      a[n].before = -1;
    }
    end = b;
  } else {
    if (n >= 0) {
      a[n].before = -1;
    }
  }
  first = n;
}

void deleteLast(DLList *a, int &first, int &end) {
  int b = a[end].before;
  if (first == end) {
    if (b >= 0) {
      a[b].next = -1;
    }
    int n = a[first].next;
    if (n >= 0) {
      a[n].before = -1;
    }
    first = n;
  } else {
    if (b >= 0) {
      a[b].next = -1;
    }
  }
  end = b;
}

int main() {

  int n;
  std::cin >> n;
  int first = -1, end = -1, cou = 0, x;
  std::string str;
  DLList a[n];
  for (int i = 0; i < n; i++) {
    std::cin >> str;

    if (str == "insert") {
      std::cin >> x;
      insertx(a, first, end, cou, x);
      cou++;
    }
    if (str == "delete") {
      std::cin >> x;
      deletex(a, first, end, cou, x);
    }
    if (str == "deleteFirst") {
      deleteFirst(a, first, end);
    }
    if (str == "deleteLast") {
      deleteLast(a, first, end);
    }

    //		std::cout<<first<<" "<<a[first].x<<" "<<end<<"
    //"<<a[end].x<<"\\n";
  }

  int now = first;
  if (now != -1) {
    //	std::cout<<first<<end;
    while (now != -1) {
      if (a[now].next != -1) {
        std::cout << a[now].x << " ";
      } else {
        std::cout << a[now].x << "\\n";
      }
      now = a[now].next;
    }
    //		std::cout<<a[now].x<<"\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>

struct DLList {

  int before, next, name, x;
};

void insertx(DLList *a, int &first, int &end, int &cou, int &x) {

  a[first].before = cou;
  a[cou].next = first;
  a[cou].before = -1;
  first = cou;
  a[cou].x = x;
  if (cou == end) {
    end = cou;
  }
}

void deletex(DLList *a, int &first, int &end, int &cou, int &x) {

  int i = first;
  int b, n;
  while (i != -1) {
    if (a[i].x == x) {
      b = a[i].before;
      n = a[i].next;
      if (i != first && i != end) {
        a[b].next = n;
        a[n].before = b;
      } else if (i == first) {
        a[n].before = -1;
        first = n;
      } else {
        a[b].next = -1;
        end = b;
      }
      break;
    } else {
      i = a[i].next;
    }
  }
}

void deleteFirst(DLList *a, int &first) {
  int n = a[first].next;
  a[n].before = -1;
  first = n;
}

void deleteLast(DLList *a, int &end) {
  int b = a[end].before;
  a[b].next = -1;
  end = b;
}

int main() {

  int n;
  std::cin >> n;
  int first = 0, end = 0, cou = 0, x;
  std::string str;
  DLList a[n];
  for (int i = 0; i < n; i++) {
    std::cin >> str;

    if (str == "insert") {
      std::cin >> x;
      insertx(a, first, end, cou, x);
      cou++;
    }
    if (str == "delete") {
      std::cin >> x;
      deletex(a, first, end, cou, x);
    }
    if (str == "deleteFirst") {
      deleteFirst(a, first);
    }
    if (str == "deleteLast") {
      deleteLast(a, end);
    }

    //		std::cout<<a[first].x<<"\\n";
  }

  int now = first;

  while (now != end) {
    std::cout << a[now].x << " ";
    now = a[now].next;
  }
  std::cout << a[now].x << "\\n";
  return 0;
}