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

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

constexpr int INF = 1000000000; /* 1e+9a */
constexpr int MODULO = 1000000007;

typedef int64_t ll;

constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};

struct rect {
public:
  int w;
  int d;
  int old;

  int sqm() { return w * d; }
  int circ() { return w + d; }
};

bool rect_less(rect a, rect b) {
  if (a.old != b.old) {
    return a.old < b.old;
  }
  return (a.sqm()) < (b.sqm());
}

bool rect_less_sqm(rect a, rect b) { return (a.sqm()) < (b.sqm()); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, w, d;
  while (cin >> n >> w >> d) {
    if (w == 0)
      return 0;
    vector<rect> rects;
    rects.push_back(rect{w, d, 0});
    for (int i = 0; i < n; ++i) {
      int p, s;
      cin >> p >> s;
      p -= 1;
      s %= rects[p].circ();
      rect one, two;
      one = rects[p];
      two = rects[p];
      if (s < one.w) {
        one.w -= s;
        two.w = s;
      } else {
        s -= one.w;
        one.d -= s;
        two.d = s;
      }
      one.old = two.old = i;
      rects[p] = one;
      rects.push_back(two);
      sort(rects.begin(), rects.end(), &rect_less);
      //            for (auto && rc : rects) {
      //                cout << rc.w << ',' << rc.d << ' ';
      //            }
      //            cout << "DEBUGS" << endl;
    }
    sort(rects.begin(), rects.end(), &rect_less_sqm);
    cout << rects[0].sqm();
    for (int j = 1; j < rects.size(); ++j) {
      cout << ' ' << rects[j].sqm();
    }
    cout << endl;
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdint>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

constexpr int INF = 1000000000; /* 1e+9a */
constexpr int MODULO = 1000000007;

typedef int64_t ll;

constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};

struct rect {
public:
  int w;
  int d;
  int old;

  int sqm() { return w * d; }
  int circ() { return w + d; }
};

bool rect_less(rect a, rect b) {
  if (a.old != b.old) {
    return a.old < b.old;
  }
  return (a.sqm()) < (b.sqm());
}

bool rect_less_sqm(rect a, rect b) { return (a.sqm()) < (b.sqm()); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, w, d;
  while (cin >> n >> w >> d) {
    if (w == 0)
      return 0;
    vector<rect> rects;
    rects.push_back(rect{w, d, 0});
    for (int i = 0; i < n; ++i) {
      int p, s;
      cin >> p >> s;
      p -= 1;
      s %= rects[p].circ();
      rect one, two;
      one = rects[p];
      two = rects[p];
      if (s < rects[p].w) {
        one.w -= s;
        two.w = s;
      } else {
        s -= w;
        one.d -= s;
        two.d = s;
      }
      one.old = two.old = i;
      rects[p] = one;
      rects.push_back(two);
      sort(rects.begin(), rects.end(), &rect_less);
      //            for (auto && rc : rects) {
      //                cout << rc.sqm() << ' ';
      //            }
      //            cout << "DEBUGS\\n";
    }
    sort(rects.begin(), rects.end(), &rect_less_sqm);
    for (auto &&rc : rects) {
      cout << rc.sqm() << ' ';
    }
    cout << endl;
  }
}