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

#include <algorithm>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

#define For(i, a, n) for (int i = a; i < n; i++)
#define rep(i, n) For(i, 0, n)
#define clr(n) memset(n, 0, sizeof n)
#define all(n) (n).begin(), (n).end()

int main() {
  int n, w, d;

  while (cin >> n >> w >> d && (n || w || d)) {

    vector<int> x, y;
    x.push_back(w), y.push_back(d);

    rep(i, n) {
      int p, s;
      cin >> p >> s;
      p--;

      int d[4] = {x[p], y[p], x[p], y[p]};
      int t = -1;

      s = s % (x[p] * 2 + y[p] * 2);

      for (int j = 0; j < 4; j++) {
        if (s < d[j]) {
          t = j;
          break;
        } else {
          s -= d[j];
        }
      }
      int nx1, nx2;
      if (t % 2 == 0) {
        nx1 = x[p] - s;
        nx2 = s;

        int ty = y[p];
        x.erase(x.begin() + p);
        y.erase(y.begin() + p);
        x.push_back(min(nx1, nx2));
        x.push_back(max(nx1, nx2));
        y.push_back(ty);
        y.push_back(ty);
      } else {
        nx1 = y[p] - s;
        nx2 = s;

        int tx = x[p];
        x.erase(x.begin() + p);
        y.erase(y.begin() + p);
        y.push_back(min(nx1, nx2));
        y.push_back(max(nx1, nx2));
        x.push_back(tx);
        x.push_back(tx);
      }
    }

    vector<int> ans;
    rep(i, x.size()) { ans.push_back(x[i] * y[i]); }
    sort(all(ans));
    rep(i, ans.size()) {
      if (i == ans.size() - 1)
        cout << ans[i] << endl;
      else
        cout << ans[i] << " ";
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

#define For(i, a, n) for (int i = a; i < n; i++)
#define rep(i, n) For(i, 0, n)
#define clr(n) memset(n, 0, sizeof n)
#define all(n) (n).begin(), (n).end()

int main() {
  int n, w, d;

  while (cin >> n >> w >> d && (n || w || d)) {

    int x[1001], y[1001];
    int count = 1;
    x[1] = w, y[1] = d;

    rep(i, n) {
      int p, s;
      cin >> p >> s;

      int d[4] = {x[p], y[p], x[p], y[p]};
      int t = -1;

      s = s % (x[p] * 2 + y[p] * 2);

      for (int j = 0; j < 4; j++) {
        if (s < d[j]) {
          t = j;
          break;
        } else {
          s -= d[j];
        }
      }
      int nx1, nx2;
      if (t % 2 == 0) {
        nx1 = x[p] - s;
        nx2 = s;

        if (p == count) {
          x[p] = min(nx1, nx2);
        } else {
          x[p] = 0;
          x[++count] = min(nx1, nx2);
          y[count] = y[p];
        }
        x[++count] = max(nx1, nx2);
        y[count] = y[p];
      } else {
        nx1 = y[p] - s;
        nx2 = s;

        if (p == count) {
          y[p] = min(nx1, nx2);
        } else {
          y[p] = 0;
          y[++count] = min(nx1, nx2);
          x[count] = x[p];
        }
        y[++count] = max(nx1, nx2);
        x[count] = x[p];
      }

      bool f = true;
      while (f) {
        f = false;
        For(i, 1, count) {
          if (!x[i] || !y[i]) {
            x[i] = x[i + 1];
            y[i] = y[i + 1];
            x[i + 1] = 0;
            y[i + 1] = 0;
            f = true;
          }
        }
      }
    }

    vector<int> ans;
    For(i, 1, count + 1) {
      if (x[i] && y[i])
        ans.push_back(x[i] * y[i]);
    }
    sort(all(ans));
    rep(i, ans.size()) {
      if (i == ans.size() - 1)
        cout << ans[i] << endl;
      else
        cout << ans[i] << " ";
    }
  }
  return 0;
}