Problem p00730 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

struct Cake {
  int w, d;
  Cake(int w, int d) : w(w), d(d) {}
  Cake() {
    w = 0;
    d = 0;
  }
  void Push(int _w, int _d) {
    w = _w;
    d = _d;
  }
};

bool operator<(const Cake &a, const Cake &b) { return a.w * a.d < b.w * b.d; }

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

  while (cin >> n >> w >> d, n || w || d) {
    vector<Cake> cake(n + 1);
    int p, s;

    cake[0].Push(w, d);

    for (int i = 0; i < n; ++i) {
      cin >> p >> s;
      --p;

      w = cake[p].w, d = cake[p].d;
      int len = 2 * w + 2 * d;
      s = s % len;

      for (int j = p; j < i; ++j)
        swap(cake[j], cake[j + 1]);

      if ((0 < s && s < w) || (w + d < s && s < 2 * w + d)) {
        // 縦に分割
        if (w + d < s && s < 2 * w + d)
          s = s - w - d;

        if (s >= w - s) {
          cake[i + 1].Push(s, d);
          cake[i].w = w - s;
        } else {
          cake[i + 1].Push(w - s, d);
          cake[i].w = s;
        }
      } else if ((w < s && s < w + d) || (2 * w + d < s)) {
        // 横に分割
        if (2 * w + d < s)
          s = s - 2 * w - d;
        else
          s = s - w;

        if (s >= d - s) {
          cake[i + 1].Push(w, s);
          cake[i].d = d - s;
        } else {
          cake[i + 1].Push(w, d - s);
          cake[i].d = s;
        }
      }
    }

    sort(cake.begin(), cake.end());

    for (size_t i = 0; i < cake.size() - 1; ++i)
      cout << cake[i].w * cake[i].d << " ";
    cout << cake[cake.size() - 1].w * cake[cake.size() - 1].d << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

struct Cake {
  int w, d;
  Cake(int w, int d) : w(w), d(d) {}
  Cake() {
    w = 0;
    d = 0;
  }
};

bool operator<(const Cake &a, const Cake &b) { return a.w * a.d < b.w * b.d; }

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

  while (cin >> n >> w >> d, n || w || d) {
    vector<Cake> cake;
    int p, s;

    cake.push_back(Cake(w, d));

    for (int i = 0; i < n; ++i) {
      cin >> p >> s;
      --p;

      int len = 2 * cake[p].w + 2 * cake[p].d;

      if (len != 0)
        s = s % len;

      if ((s < cake[p].w) ||
          (cake[p].w + cake[p].d < s && s < 2 * cake[p].w + cake[p].d)) {
        // 縦に分割
        if (cake[p].w + cake[p].d < s && s < 2 * cake[p].w + cake[p].d)
          s = s - cake[p].w - cake[p].d;

        if (s > cake[p].w - s) {
          cake.push_back(Cake(s, cake[p].d));
          cake[p].w = cake[p].w - s;
        } else {
          cake.push_back(Cake(cake[p].w - s, cake[p].d));
          cake[p].w = s;
        }
      } else if ((cake[p].w < s && s < cake[p].w + cake[p].d) ||
                 (2 * cake[p].w + cake[p].d < s)) {
        // 横に分割
        if (2 * cake[p].w + cake[p].d < s)
          s = s - cake[p].w - cake[p].d;
        s = s - cake[p].w;

        if (s > cake[p].d - s) {
          cake.push_back(Cake(cake[p].w, s));
          cake[p].d = cake[p].d - s;
        } else {
          cake.push_back(Cake(cake[p].w, cake[p].d - s));
          cake[p].d = s;
        }
      }
    }

    sort(cake.begin(), cake.end());

    for (size_t i = 0; i < cake.size() - 1; ++i)
      cout << cake[i].w * cake[i].d << " ";
    cout << cake[cake.size() - 1].w * cake[cake.size() - 1].d << endl;
  }

  return 0;
}