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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

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

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

int main() {
  int w, q;
  while (cin >> w >> q, w | q) {
    int p[256];
    fill(p, p + w, -1);
    p[w] = -2;
    while (q--) {
      char c;
      cin >> c;
      if (c == 's') {
        int id, width;
        cin >> id >> width;
        int i;
        for (i = 0; i < w; ++i)
          if (count(p + i, p + i + width, -1) == width)
            break;
        if (i != w) {
          fill(p + i, p + i + width, id);
          cout << i << endl;
        } else
          cout << "impossible" << endl;
      } else {
        int id;
        cin >> id;
        replace(p, p + w, id, -1);
      }

      getchar();
    }
    cout << "END" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

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

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

int main() {
  int w, q;
  while (scanf("%d%d", &w, &q), w | q) {
    getchar();

    int p[256];
    fill(p, p + w, -1);
    p[w] = -2;
    while (q--) {
      char c = getchar();
      if (c == 's') {
        int id, width;
        scanf("%d%d", &id, &width);
        int i;
        for (i = 0; i < w; ++i)
          if (count(p + i, p + i + width, -1) == width)
            break;
        if (i != w) {
          fill(p + i, p + i + width, id);
          printf("%d\\n", i);
        } else
          puts("impossible");
      } else {
        int id;
        scanf("%d", &id);
        replace(p, p + w, id, -1);
      }

      getchar();
    }
    puts("END");
  }
}