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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

using namespace std;
typedef long long ll;
const int MAX = 1000100;

int head, tail;
int T[MAX];

void insertx(int x) {
  if (head != 0)
    T[head - 1] = x;
  else
    T[MAX - 1] = x;
  if (head != 0)
    head--;
  else
    head = MAX - 1;
}

void deletex(int x) {
  for (int i = head;;) {
    if (T[i] == x) {
      if (tail == 0) {
        for (int j = i; j != MAX - 1;) {
          T[j] = T[(j + 1) % MAX];
          j = (j + 1) % MAX;
        }
      } else {
        for (int j = i; j != tail - 1;) {
          T[j] = T[(j + 1) % MAX];
          j = (j + 1) % MAX;
        }
      }
      if (tail != 0)
        tail--;
      else
        tail = MAX - 1;
      return;
    }
    i = (i + 1) % MAX;
    if (i == tail)
      return;
  }
}

void deleteFirst() { head = (head + 1) % MAX; }

void deleteLast() {
  if (tail != 0)
    tail--;
  else
    tail = MAX - 1;
}

int main() {
  int n, x;
  char s[20];

  cin >> n;
  head = 1;
  tail = 1;
  rep(i, n) {
    scanf("%s", s);
    if (strcmp(s, (const char *)"insert") == 0) {
      scanf("%d", &x);
      insertx(x);
    } else if (strcmp(s, (const char *)"delete") == 0) {
      scanf("%d", &x);
      deletex(x);
    } else if (strcmp(s, (const char *)"deleteFirst") == 0) {
      deleteFirst();
    } else if (strcmp(s, (const char *)"deleteLast") == 0) {
      deleteLast();
    }
  }

  for (int i = head;;) {
    if (i == tail - 1) {
      printf("%d\\n", T[i]);
      break;
    } else if (i == MAX - 1 && tail == 0) {
      printf("%d\\n", T[i]);
      break;
    } else {
      printf("%d ", T[i]);
    }
    i = (i + 1) % MAX;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)

using namespace std;
typedef long long ll;
const int MAX = 1000100;

int head, tail;
int T[MAX];

void insertx(int x) {
  if (head != 0)
    T[head - 1] = x;
  else
    T[MAX - 1] = x;
  if (head != 0)
    head--;
  else
    head = MAX - 1;
}

void deletex(int x) {
  for (int i = head;;) {
    if (T[i] == x) {
      if (tail == 0) {
        for (int j = i; j != MAX - 1;) {
          T[j] = T[(j + 1) % MAX];
          j = (j + 1) % MAX;
        }
      } else {
        for (int j = i; j != tail - 1;) {
          T[j] = T[(j + 1) % MAX];
          j = (j + 1) % MAX;
        }
      }
      if (tail != 0)
        tail--;
      else
        tail = MAX - 1;
      return;
    }
    i = (i + 1) % MAX;
  }
}

void deleteFirst() { head = (head + 1) % MAX; }

void deleteLast() {
  if (tail != 0)
    tail--;
  else
    tail = MAX - 1;
}

int main() {
  int n, x;
  char s[20];

  cin >> n;
  head = 1;
  tail = 1;
  rep(i, n) {
    scanf("%s", s);
    if (strcmp(s, (const char *)"insert") == 0) {
      scanf("%d", &x);
      insertx(x);
    } else if (strcmp(s, (const char *)"delete") == 0) {
      scanf("%d", &x);
      deletex(x);
    } else if (strcmp(s, (const char *)"deleteFirst") == 0) {
      deleteFirst();
    } else if (strcmp(s, (const char *)"deleteLast") == 0) {
      deleteLast();
    }
  }

  for (int i = head;;) {
    if (i == tail - 1) {
      printf("%d\\n", T[i]);
      break;
    } else {
      printf("%d ", T[i]);
    }
    i = (i + 1) % MAX;
  }
  return 0;
}