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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

struct Cake {
  int w, d, s;
  Cake() {}
  Cake(int w, int d, int s) : w(w), d(d), s(s) {}
  bool operator<(Cake c) const { return s < c.s; }
};

int n, w, d, p, s;
vector<Cake> cake;

void solve() {
  int a = cake[p - 1].w;
  int b = cake[p - 1].d;
  cake.erase(cake.begin() + p - 1);
  int len = s % (2 * (a + b));
  if (0 < len && len < a) {
    if (len * b < (a - len) * b) {
      cake.push_back(Cake(len, b, len * b));
      cake.push_back(Cake(a - len, b, (a - len) * b));
    } else {
      cake.push_back(Cake(a - len, b, (a - len) * b));
      cake.push_back(Cake(len, b, len * b));
    }
    return;
  } else
    len -= a;
  if (0 < len && len < b) {
    if (a * len < a * (b - len)) {
      cake.push_back(Cake(a, len, a * len));
      cake.push_back(Cake(a, b - len, a * (b - len)));
    } else {
      cake.push_back(Cake(a, b - len, a * (b - len)));
      cake.push_back(Cake(a, len, a * len));
    }
    return;
  } else
    len -= b;
  if (0 < len && len < a) {
    if (len * b < (a - len) * b) {
      cake.push_back(Cake(len, b, len * b));
      cake.push_back(Cake(a - len, b, (a - len) * b));
    } else {
      cake.push_back(Cake(a - len, b, (a - len) * b));
      cake.push_back(Cake(len, b, len * b));
    }
    return;
  } else
    len -= a;
  if (0 < len && len < b) {
    if (a * len < a * (b - len)) {
      cake.push_back(Cake(a, len, a * len));
      cake.push_back(Cake(a, b - len, a * (b - len)));
    } else {
      cake.push_back(Cake(a, b - len, a * (b - len)));
      cake.push_back(Cake(a, len, a * len));
    }
    return;
  } else
    len -= b;
}

int main() {
  while (cin >> n >> w >> d, (n | w | d)) {
    cake.clear();
    cake.push_back(Cake(w, d, w * d));

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

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

    for (int i = 0; i < cake.size(); i++) {
      if (i + 1 != cake.size())
        cout << cake[i].s << " ";
      else
        cout << cake[i].s << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;

void solve() {
  int n, w, d;
  while (cin >> n >> w >> d, n || w || d) {
    PP pp;
    pp.first = w * d;
    pp.second.first = w;
    pp.second.second = d;
    vector<PP> cake;
    cake.push_back(pp);
    for (int i = 0; i < n; ++i) {
      int p, s;
      cin >> p >> s;
      PP ppCake = cake[p - 1];
      s = s % (2 * (ppCake.second.first + ppCake.second.second));
      PP nextCake;
      if (0 <= s && s < ppCake.second.first) {
        nextCake.second.first = s;
        ppCake.second.first -= s;
        nextCake.second.second = ppCake.second.second;

        nextCake.first = nextCake.second.first * nextCake.second.second;
        ppCake.first = ppCake.second.first * ppCake.second.second;
      } else if (0 <= s - ppCake.second.first &&
                 s - ppCake.second.first < ppCake.second.second) {
        s -= ppCake.second.first;
        nextCake.second.first = ppCake.second.first;
        ppCake.second.second -= s;
        nextCake.second.second = s;

        nextCake.first = nextCake.second.first * nextCake.second.second;
        ppCake.first = ppCake.second.first * ppCake.second.second;
      }
      if (0 <= s - ppCake.second.first - ppCake.second.second &&
          s - ppCake.second.first - ppCake.second.second <
              ppCake.second.first) {
        s -= ppCake.second.first + ppCake.second.second;
        nextCake.second.first = s;
        ppCake.second.first -= s;
        nextCake.second.second = ppCake.second.second;

        nextCake.first = nextCake.second.first * nextCake.second.second;
        ppCake.first = ppCake.second.first * ppCake.second.second;
      } else if (0 <= s - ppCake.second.first - ppCake.second.second -
                          ppCake.second.first &&
                 s - ppCake.second.first - ppCake.second.second -
                         ppCake.second.first <
                     ppCake.second.second) {
        s -= ppCake.second.first + ppCake.second.second + ppCake.second.first;
        nextCake.second.first = ppCake.second.first;
        ppCake.second.second -= s;
        nextCake.second.second = s;

        nextCake.first = nextCake.second.first * nextCake.second.second;
        ppCake.first = ppCake.second.first * ppCake.second.second;
      }
      if (ppCake > nextCake) {
        cake[p - 1] = nextCake;
        cake.push_back(ppCake);
      } else {
        cake[p - 1] = ppCake;
        cake.push_back(nextCake);
      }
    }
    sort(cake.begin(), cake.end());
    bool isFirst = true;
    for (int i = 0; i < cake.size(); ++i) {
      if (!isFirst) {
        cout << " ";
      }
      isFirst = false;
      cout << cake[i].first;
    }
    cout << endl;
  }
}

int main() {
  solve();
  return (0);
}