Problem p00730 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define MAX 100

typedef struct {
  int width;
  int height;
} CAKE;

bool operator<(const CAKE &left, const CAKE &right) {
  return left.width * left.height < right.width * right.height;
}

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

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

    vector<CAKE> cake;
    CAKE tmp = {w, d};
    cake.push_back(tmp);
    int p, s;
    while (n--) {
      cin >> p >> s;
      p--;

      int cnt = 0;
      while (true) {
        assert(s != cake[p].width);
        if (s - cake[p].width <= 0)
          break;
        s -= cake[p].width;
        cnt++;

        assert(s != cake[p].height);
        if (s - cake[p].height <= 0)
          break;
        s -= cake[p].height;
        cnt++;
      }

      cnt %= 4;
      pair<CAKE, CAKE> P;
      if (cnt % 2 == 0) {

        CAKE tmp = {s, cake[p].height};
        P.first = tmp;
        tmp.width = cake[p].width - s;
        P.second = tmp;

      } else {
        CAKE tmp = {cake[p].width, s};
        P.first = tmp;
        tmp.height = cake[p].height - s;
        P.second = tmp;
      }

      if (P.first.width * P.first.height < P.second.width * P.second.height) {
        cake.push_back(P.second);
        cake.push_back(P.first);
        cake.erase(cake.begin() + p);
      } else {
        cake.push_back(P.first);
        cake.push_back(P.second);
        cake.erase(cake.begin() + p);
      }
    }

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

    for (int i = 0; i < cake.size() - 1; i++) {
      cout << cake[i].width * cake[i].height << " ";
    }
    cout << cake[cake.size() - 1].width * cake[cake.size() - 1].height << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MAX 100

typedef struct {
  int width;
  int height;
} CAKE;

bool operator<(const CAKE &left, const CAKE &right) {
  return left.width * left.height < right.width * right.height;
}

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

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

    vector<CAKE> cake;
    CAKE tmp = {w, d};
    cake.push_back(tmp);
    int p, s;
    while (n--) {
      cin >> p >> s;
      p--;

      int cnt = 0;
      while (true) {
        assert(s != cake[p].width);
        if (s - cake[p].width <= 0)
          break;
        s -= cake[p].width;
        cnt++;

        assert(s != cake[p].height);
        if (s - cake[p].height <= 0)
          break;
        s -= cake[p].height;
        cnt++;
      }

      cnt %= 4;
      pair<CAKE, CAKE> P;
      if (cnt % 2 == 0) {

        CAKE tmp = {s, cake[p].height};
        P.first = tmp;
        tmp.width = cake[p].width - s;
        P.second = tmp;

      } else {
        CAKE tmp = {cake[p].width, s};
        P.first = tmp;
        tmp.height = cake[p].height - s;
        P.second = tmp;
      }

      if (P.first.width * P.first.height > P.second.width * P.second.height) {
        cake.push_back(P.second);
        cake.push_back(P.first);
        cake.erase(cake.begin() + p);
      } else {
        cake.push_back(P.first);
        cake.push_back(P.second);
        cake.erase(cake.begin() + p);
      }
    }

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

    for (int i = 0; i < cake.size() - 1; i++) {
      cout << cake[i].width * cake[i].height << " ";
    }
    cout << cake[cake.size() - 1].width * cake[cake.size() - 1].height << endl;
  }

  return 0;
}