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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
struct cake {
  int w, h;
  cake(int aa, int bb) {
    w = aa;
    h = bb;
  }
  int area() const { return w * h; }
  void pri() {
    cout << "(a,b) = " << w << ", " << h << ",are = " << area() << endl;
  }
};

bool comp(const cake &c1, const cake &c2) { return c1.area() < c2.area(); }
int main() {
  int n, w, d;
  while (cin >> n >> w >> d, n || w || d) {
    vector<cake> c(2 * (n + 1), cake(0, 0));
    c[0] = cake(w, d);
    REP(i, n) {
      int num;
      int s;
      cin >> num >> s;
      num--;
      int a = c[num].w; // width
      int b = c[num].h; // height
      int add_i = i + 1;
      s %= 2 * (a + b);
      if (a > s || a * 2 + b > s && a + b < s) {
        if (a * 2 + b > s && a + b < s)
          s = a * 2 + b - s;
        c[add_i] = cake(s, b);
        c[add_i + 1] = cake(a - s, b);
      } else {
        if (a + b > s) {
          s -= a;
        } else {
          s = a * 2 + b * 2 - s;
        }
        c[add_i] = cake(a, s);
        c[add_i + 1] = cake(a, b - s);
      }
      if (c[add_i].area() > c[add_i + 1].area())
        swap(c[add_i], c[add_i + 1]);
      // c[add_i].pri();c[add_i+1].pri();
      c.erase(c.begin() + num);
      //			cout << "i = " << i <<
      //endl;c[0].pri();c[1].pri();c[2].pri();
    }
    sort(c.begin(), c.end(), comp);
    //	c[0].pri();c[1].pri();c[2].pri();
    while (c[0].area() == 0)
      c.erase(c.begin());
    REP(i, n + 1) {
      if (i)
        cout << ' ';
      cout << c[i].area();
    }
    cout << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
struct cake {
  int dx, dy, vx, vy;
  cake(int a, int b, int c, int d) {
    dx = a;
    dy = b;
    vx = c;
    vy = d;
  }
  int area() const { return (vx - dx) * (vy - dy); }
  int width() { return vx - dx; }
  int height() { return vy - dy; }
  void pri() {
    cout << "(dx,dy) = " << dx << ", " << dy << "(vx,vy) = " << vx << ", " << vy
         << endl;
  }
};

bool comp(const cake &c1, const cake &c2) { return c1.area() < c2.area(); }
int main() {
  int n, w, d;
  while (cin >> n >> w >> d, n || w || d) {
    vector<int> p(n), s(n);
    vector<cake> c(n + 1, cake(0, 0, 0, 0));
    c[0] = cake(0, 0, w, d);
    REP(i, n) {
      cin >> p[i] >> s[i];
      int num = p[i];
      num--;
      while (s[i] > c[num].width() * 2 + c[num].height() * 2) {
        s[i] -= c[num].width() * 2 + c[num].height() * 2;
      }
      if (c[num].width() > s[i] ||
          c[num].width() * 2 + c[num].height() > s[i] &&
              c[num].width() + (c[num].height()) < s[i]) {
        if ((c[num].width()) * 2 + (c[num].height()) > s[i] &&
            (c[num].width()) + (c[num].height()) < s[i])
          s[i] = (c[num].width()) * 2 + (c[num].height()) - s[i];
        int tmp_x = c[num].dx + s[i];
        int tmp_y = c[num].height();
        int add_i = i + 1;
        c[add_i] = cake(c[num].dx, c[num].dy, tmp_x, c[num].vy);
        c[num] = cake(tmp_x, c[num].dy, c[num].vx, c[num].vy);
        if (c[add_i].area() < c[num].area())
          swap(c[add_i], c[num]);
      } else {
        if (c[num].width() + (c[num].height()) > s[i]) {
          s[i] -= c[num].width();
        } else {
          s[i] = (c[num].width()) * 2 + (c[num].height()) * 2 - s[i];
        }
        int tmp_x = c[num].width();
        int tmp_y = c[num].dy + s[i];
        int add_i = i + 1;
        c[add_i] = cake(c[num].dx, c[num].dy, c[num].vx, tmp_y);
        c[num] = cake(c[num].dx, tmp_y, c[num].vx, c[num].vy);
        if (c[add_i].area() < c[num].area())
          swap(c[add_i], c[num]);
      }
      //			cout << "i = " << i <<
      //endl;c[0].pri();c[1].pri();c[2].pri();
    }
    sort(c.begin(), c.end(), comp);
    //	c[0].pri();c[1].pri();c[2].pri();
    REP(i, n + 1) {
      if (i)
        cout << ' ';
      cout << c[i].area();
    }
    cout << '\\n';
  }
  return 0;
}