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

#include <algorithm>
#include <cmath>
#include <stdio.h>

using namespace std;

struct Node {
  int data;
  Node *prev;
  Node *next;
};

class List {
public:
  List() {
    motherNode = new Node();
    motherNode->data = -1;
    motherNode->prev = motherNode; //?????°?§?????????????
    motherNode->next = motherNode;
    num_of_node = 0;
    current = 0;
  };

  void insertFromLast(int x) { //???????????§??°????????§????°??????????
    Node *tmpNode = motherNode->prev;
    if (tmpNode == motherNode) { //???????????????????????????????????´???
      motherNode->next = new Node();
      motherNode->next->data = x;
      motherNode->next->next = motherNode;
      motherNode->next->prev = motherNode;
      motherNode->prev = motherNode->next;
      current = motherNode->next; //???current????????????????¨???????
    } else { //??¢?????????????????????????????????????????¨????????´???
      while (tmpNode != motherNode && tmpNode->data > x) {
        tmpNode = tmpNode->prev;
      }
      Node *nextNode = tmpNode->next;
      tmpNode->next = new Node();
      tmpNode->next->data = x;
      tmpNode->next->next = nextNode;
      tmpNode->next->prev = tmpNode;
      nextNode->prev = tmpNode->next;
    }
    num_of_node++;
  }

  //????¨????????????????a?§???????????§??????????????????????????????????????????????????????´????????¢??°
  int upDist(int a) {
    int to_move = a % num_of_node, count = 0, ret;

    if (to_move >
        num_of_node -
            (to_move -
             3)) { //???????¨???¨????????????????????????????????????????????????
      return downDist(num_of_node - to_move);
    }

    Node *tmp = current;
    while (count < to_move) {
      tmp = tmp->next;
      if (tmp != motherNode) {
        count++;
      }
    }

    // motherNode??????count????¢???????????????§???count ==
    // to_move??¨?????£????????????motherNode??§???????????¨??????????????????
    ret = tmp->data;
    if (tmp->next == motherNode) {
      current = motherNode->next;
    } else {
      current = tmp->next;
    }

    tmp->prev->next = tmp->next;
    tmp->next->prev = tmp->prev;
    delete tmp;
    num_of_node--;
    return ret;
  }

  //???????¨???¨??????a?§???????????§??????????????????????????????????????????????????????´????????¢??°
  int downDist(int a) {
    int to_move = a % num_of_node, count = 0, ret;

    if (to_move >
        num_of_node -
            (to_move -
             3)) { //???????¨???¨????????????????????????????????????????????????
      return upDist(num_of_node - to_move);
    }

    Node *tmp = current;
    while (count < to_move) {
      tmp = tmp->prev;
      if (tmp != motherNode) {
        count++;
      }
    }

    // motherNode??????count????¢???????????????§???count ==
    // to_move??¨?????£????????????motherNode??§???????????¨??????????????????
    ret = tmp->data;
    if (tmp->next == motherNode) {
      current = motherNode->next;
    } else {
      current = tmp->next;
    }

    tmp->prev->next = tmp->next;
    tmp->next->prev = tmp->prev;
    delete tmp;
    num_of_node--;
    return ret;
  }

  void showList() {
    Node *tmpNode = motherNode->next;
    printf("%d", tmpNode->data);
    tmpNode = tmpNode->next;
    while (tmpNode != motherNode) {
      printf(" %d", tmpNode->data);
      tmpNode = tmpNode->next;
    }
    printf("\\n");
  }

private:
  Node *motherNode;
  Node *
      current; //?????????????????£????????????????????????????????????????????????????????????
  int num_of_node;
};

int main() {

  int N, M, Q, a;

  scanf("%d %d %d", &N, &M, &Q);
  int checkTable[N];
  List list;

  for (int i = 0; i < N; i++) {
    checkTable[i] = 1;
    list.insertFromLast(i); // 0???N-1?????????????????????
  }

  for (int i = 0; i < M; i++) {
    scanf("%d", &a);

    if (a % 2 == 0) { //????¨???????:??°????????§?????????????????????
      checkTable[list.upDist(a)] = 0;
    } else { //??°???????°??????????????????????
      checkTable[list.downDist(a)] = 0;
    }
  }

  int query;
  for (int i = 0; i < Q; i++) {
    scanf("%d", &query);

    printf("%d\\n", checkTable[query]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <stdio.h>

using namespace std;

int main() {

  int N, M, Q, a, current, count;

  scanf("%d %d %d", &N, &M, &Q);
  int checkTable[N];

  for (int i = 0; i < N; i++)
    checkTable[i] = 1;

  current = 0;
  for (int i = 0; i < M; i++) {
    scanf("%d", &a);

    if (a % 2 == 0) { //????¨???????:??°????????§?????????????????????
      for (count = 0;;) {
        current = (current + 1) % N;
        if (checkTable[current] == 1) {
          count++;

          if (count == a) {
            checkTable[current] = 0;

            for (int next = 1;; next++) {
              if (checkTable[(current + next) % N] == 1) {
                current = (current + next) % N;
                break;
              }
            }

            break;
          }
        }
      }
    } else { //??°???????°??????????????????????
      for (count = 0;;) {
        if (current == 0)
          current = N - 1;
        else {
          current--;
        }

        if (checkTable[current] == 1) {
          count++;

          if (count == a) {
            checkTable[current] = 0;
            for (int next = 1;; next++) {
              if (checkTable[(current + next) % N] == 1) {
                current = (current + next) % N;
                break;
              }
            }
            break;
          }
        }
      }
    }
  }

  int query;
  for (int i = 0; i < Q; i++) {
    scanf("%d", &query);

    printf("%d\\n", checkTable[query]);
  }

  return 0;
}