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

#include "bits/stdc++.h"
using namespace std;

struct rect {
  int w;
  int h;
};

int main() {
  while (1) {
    int n, W, D;
    cin >> n >> W >> D;
    if (!W)
      break;
    vector<rect> rects;
    rects.push_back(rect{W, D});
    for (int i = 0; i < n; ++i) {
      int p, s;
      cin >> p >> s;
      p--;
      rect arect(rects[p]);
      rects.erase(rects.begin() + p);
      s %= 2 * (arect.h + arect.w);
      rect a;
      rect b;
      for (int j = 0; j < 2; ++j) {
        if (s < arect.w) {
          a.h = arect.h;
          a.w = s;
          b.h = arect.h;
          b.w = arect.w - s;
          break;
        } else {
          s -= arect.w;
          if (s < arect.h) {
            a.h = s;
            a.w = arect.w;
            b.h = arect.h - s;
            b.w = arect.w;
            break;
          } else {
            s -= arect.h;
          }
        }
      }
      if (a.h * a.w > b.h * b.w) {
        swap(a, b);
      }
      rects.push_back(a);
      rects.push_back(b);
    }
    sort(rects.begin(), rects.end(),
         [](const rect &l, const rect &r) { return l.h * l.w < r.h * r.w; });
    for (int i = 0; i < rects.size(); ++i) {
      cout << rects[i].h * rects[i].w;
      if (i != rects.size() - 1)
        cout << " ";
    }
    cout << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()
int id;
struct Cake {
  int id;
  int w;
  int d;
  Cake(int a, int b, int c) {
    id = a;
    w = b;
    d = c;
  }
};
vector<Cake> CakeCol;
void Cut(int aCakenum, int length) {
  Cake aC = CakeCol[aCakenum];
  length %= 2 * (aC.d + aC.w);
  CakeCol.erase(CakeCol.begin() + aCakenum);

  if (length < aC.w) {
    if (length * 2 < aC.w) {
      CakeCol.push_back({id, length, aC.d});
      id++;
      CakeCol.push_back({id, aC.w - length, aC.d});
      id++;
    } else {
      CakeCol.push_back({id, aC.w - length, aC.d});
      id++;
      CakeCol.push_back({id, length, aC.d});
      id++;
    }
    return;
  }
  length -= aC.w;
  if (length < aC.d) {
    if (length * 2 < aC.d) {
      CakeCol.push_back({id, aC.w, length});
      id++;
      CakeCol.push_back({id, aC.w, aC.d - length});
      id++;
    } else {
      CakeCol.push_back({id, aC.w, aC.d - length});
      id++;
      CakeCol.push_back({id, aC.w, length});
      id++;
    }
    return;
  }
  length -= aC.d;

  if (length < aC.w) {
    if (length * 2 < aC.w) {
      CakeCol.push_back({id, length, aC.d});
      id++;
      CakeCol.push_back({id, aC.w - length, aC.d});
      id++;
    } else {
      CakeCol.push_back({id, aC.w - length, aC.d});
      id++;
      CakeCol.push_back({id, length, aC.d});
      id++;
    }
  }
  length -= aC.w;
  if (length * 2 < aC.d) {
    CakeCol.push_back({id, aC.w, length});
    id++;
    CakeCol.push_back({id, aC.w, aC.d - length});
    id++;
  } else {
    CakeCol.push_back({id, aC.w, aC.d - length});
    id++;
    CakeCol.push_back({id, aC.w, length});
    id++;
  }
  return;
}
int main() {
  while (1) {
    id = 1;
    int N, W, D;
    cin >> N >> W >> D;
    if (!N && !W && !D)
      break;
    CakeCol.clear();
    CakeCol.push_back({id, W, D});
    id++;
    REP(i, N) {
      int aid, l;
      cin >> aid >> l;
      Cut(aid - 1, l);
      sort(CakeCol.begin(), CakeCol.end(),
           [](Cake aC, Cake bC) { return aC.id < bC.id; });
    }
    sort(CakeCol.begin(), CakeCol.end(),
         [](Cake aC, Cake bC) { return aC.d * aC.w < bC.d * bC.w; });
    REP(i, N + 1) { cout << CakeCol[i].d * CakeCol[i].w << " "; }
    cout << endl;
  }
  return 0;
}