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

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

using namespace std;

struct Node {
  Node() {
    prev = 0;
    next = 0;
    left = width = cat_id = 0;
  }
  int left, width, cat_id;
  Node *prev;
  Node *next;
};

class List {
public:
  List(int W) {
    motherNode = new Node();

    motherNode->next = new Node();
    motherNode->next->left = 0;
    motherNode->next->width = W;
    motherNode->next->cat_id = -1;
    motherNode->prev = motherNode->next;
    motherNode->next->prev = motherNode;
    motherNode->next->next = motherNode;
  };

  int sleepCat(int arg_id, int arg_width) {
    bool FLG = false;
    Node *tmpNode = motherNode->next;
    int ret;

    while (tmpNode != motherNode) {
      if (tmpNode->cat_id != -1) {
        tmpNode = tmpNode->next;
      } else {
        if (tmpNode->width >= arg_width) {
          FLG = true;
          ret = tmpNode->left;
          if (tmpNode->width == arg_width) {
            tmpNode->cat_id = arg_id;
            break;
          } else {

            Node *newNode = new Node();
            newNode->cat_id = -1;
            newNode->width = tmpNode->width - arg_width;
            newNode->left = tmpNode->left + arg_width;
            newNode->prev = tmpNode;
            newNode->next = tmpNode->next;
            tmpNode->next->prev = newNode;
            tmpNode->next = newNode;
            tmpNode->width = arg_width;
            tmpNode->cat_id = arg_id;
            break;
          }
        } else {
          tmpNode = tmpNode->next;
        }
      }
    }

    if (!FLG)
      return -1;
    else {
      return ret;
    }
  }

  void awakeCat(int arg_id) {
    Node *tmpNode = motherNode->next;
    while (tmpNode != motherNode) {
      if (tmpNode->cat_id != arg_id) {
        tmpNode = tmpNode->next;
      } else {
        if ((tmpNode->prev == motherNode || tmpNode->prev->cat_id != -1) &&
            (tmpNode->next == motherNode || tmpNode->next->cat_id != -1)) {
          tmpNode->cat_id = -1;
          break;
        } else if ((tmpNode->prev == motherNode ||
                    tmpNode->prev->cat_id != -1) &&
                   (tmpNode->next != motherNode &&
                    tmpNode->next->cat_id == -1)) {
          tmpNode->cat_id = -1;
          tmpNode->width += tmpNode->next->width;
          tmpNode->next->next->prev = tmpNode;
          tmpNode->next = tmpNode->next->next;
          break;
        } else if ((tmpNode->prev != motherNode &&
                    tmpNode->prev->cat_id == -1) &&
                   (tmpNode->next == motherNode ||
                    tmpNode->next->cat_id != -1)) {
          tmpNode->prev->width += tmpNode->width;
          tmpNode->prev->next = tmpNode->next;
          tmpNode->next->prev = tmpNode->prev;
          break;
        } else if ((tmpNode->prev != motherNode &&
                    tmpNode->prev->cat_id == -1) &&
                   (tmpNode->next != motherNode &&
                    tmpNode->next->cat_id == -1)) {
          tmpNode->prev->width += (tmpNode->width + tmpNode->next->width);
          tmpNode->prev->next = tmpNode->next->next;
          tmpNode->next->next->prev = tmpNode->prev;
          break;
        }
      }
    }
  }

private:
  Node *motherNode;
};

void func(int W, int Q) {

  List list(W);

  char type[2];
  int id, width, tmp;

  for (int loop = 0; loop < Q; loop++) {
    scanf("%s", type);

    if (type[0] == 'w') {
      scanf("%d", &id);
      list.awakeCat(id);
    } else {
      scanf("%d %d", &id, &width);
      tmp = list.sleepCat(id, width);

      if (tmp == -1)
        printf("impossible\\n");
      else {
        printf("%d\\n", tmp);
      }
    }
  }
}

int main() {

  int W, Q;

  while (true) {
    scanf("%d %d", &W, &Q);
    if (W == 0 && Q == 0)
      break;

    func(W, Q);
    printf("END\\n");
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

struct Info {
  Info() {
    exist = false;
    cat_id = 0;
  }
  bool exist;
  int cat_id;
};

void func(int W, int Q) {
  Info info[W];

  char type[2];
  int id, need_w, count, left, right;

  for (int i = 0; i < Q; i++) {
    scanf("%s", type);

    if (type[0] == 's') {
      scanf("%d %d", &id, &need_w);

      for (left = 0; left <= W - need_w;) {
        count = 0;
        for (right = 0; left + right < W && info[left + right].exist == false;
             right++) {
          count++;
          if (count >= need_w)
            break;
        }
        if (count >= need_w) {
          for (int loop = 0; loop < count; loop++) {
            info[left + loop].exist = true;
            info[left + loop].cat_id = id;
          }
          printf("%d\\n", left);
          break;
        } else {
          if (left == W - need_w)
            break;

          for (left = right; left <= W - need_w; left++) {
            if (info[left].exist == false)
              break;
          }
        }
      }
      if (count < need_w) {
        printf("impossible\\n");
      }
    } else { // w
      scanf("%d", &id);

      for (int i = 0; i < W; i++) {
        if (info[i].cat_id == id) {
          info[i].exist = false;
          info[i].cat_id = 0;
        }
      }
    }
  }
}

int main() {

  int W, Q;

  while (true) {
    scanf("%d %d", &W, &Q);
    if (W == 0 && Q == 0)
      break;

    func(W, Q);
    printf("END\\n");
  }

  return 0;
}