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

#include <bits/stdc++.h>

using namespace std;
using pii = pair<int, int>;

struct Piece {
  Piece(pii ul_, int h_, int w_) : ul{ul_}, h{h_}, w{w_} {}
  pii ul;
  int h;
  int w;
  pair<Piece, Piece> cut(int s) {
    s = s % (2 * h + 2 * w);
    if (s < w) {
      Piece pieces[2] = {Piece{ul, h, s},
                         Piece{make_pair(ul.first, ul.second + s), h, w - s}};
      sort(pieces, pieces + 2);
      return make_pair(pieces[0], pieces[1]);
    }
    s -= w;
    if (s < h) {
      Piece pieces[2] = {Piece{ul, s, w},
                         Piece{make_pair(ul.first + s, ul.second), h - s, w}};
      sort(pieces, pieces + 2);
      return make_pair(pieces[0], pieces[1]);
    }
    s -= h;
    if (s < w) {
      const int s_ = w - s;
      Piece pieces[2] = {Piece{ul, h, s_},
                         Piece{make_pair(ul.first, ul.second + s_), h, w - s_}};
      sort(pieces, pieces + 2);
      return make_pair(pieces[0], pieces[1]);
    }
    s -= w;
    if (s < h) {
      const int s_ = h - s;
      Piece pieces[2] = {Piece{ul, s_, w},
                         Piece{make_pair(ul.first + s_, ul.second), h - s_, w}};
      sort(pieces, pieces + 2);
      return make_pair(pieces[0], pieces[1]);
    }
  }
  bool operator<(const Piece &p) { return h * w < p.h * p.w; }
};

int main() {
  while (true) {
    int n, w, d;
    cin >> n >> w >> d;
    if (n == 0 and w == 0 and d == 0) {
      break;
    }
    vector<Piece> piece;
    piece.push_back(Piece{make_pair(0, 0), d, w});
    for (int i = 0; i < n; i++) {
      int p, s;
      cin >> p >> s;
      p--;
      const auto pieces = piece[p].cut(s);
      piece.erase(piece.begin() + p);
      piece.push_back(pieces.first);
      piece.push_back(pieces.second);
    }
    sort(piece.begin(), piece.end());
    for (int i = 0; i < piece.size(); i++) {
      const auto &p = piece[i];
      if (i > 0) {
        cout << " ";
      }
      cout << p.w * p.h;
    }
    cout << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  while (true) {
    int n, w, d;
    cin >> n >> w >> d;
    if (n == 0 and w == 0 and d == 0) {
      break;
    }
    vector<pair<int, int>> lu;
    vector<pair<int, int>> rd;
    lu.push_back(make_pair(0, 0));
    rd.push_back(make_pair(d, w));
    for (int i = 0; i < n; i++) {
      int p, s;
      cin >> p >> s;
      p--;
      const int sy = lu[p].first;
      const int sx = lu[p].second;
      const int ey = rd[p].first;
      const int ex = rd[p].second;
      const int y = std::abs(ey - sy);
      const int x = std::abs(ex - sx);
      int dir = 0;
      s = s % (2 * y + 2 * x);
      for (dir = 0; dir < 4; dir++) {
        const int l = (dir % 2 == 0) ? x : y;
        if (s > l) {
          s -= l;
        } else {
          break;
        }
      }
      dir = dir % 4;
      if (dir == 1) {
        if (s < y - s) {
          lu[p] = make_pair(sy, sx);
          rd[p] = make_pair(sy + s, ex);
          lu.push_back(make_pair(sy + s, sx));
          rd.push_back(make_pair(ey, ex));
        } else {
          lu[p] = make_pair(sy + s, sx);
          rd[p] = make_pair(ey, ex);
          lu.push_back(make_pair(sy, sx));
          rd.push_back(make_pair(sy + s, ex));
        }
      } else if (dir == 0) {
        if (s < x - s) {
          lu[p] = make_pair(sy, sx);
          rd[p] = make_pair(ey, sx + s);
          lu.push_back(make_pair(sy, sx + s));
          rd.push_back(make_pair(ey, ex));
        } else {
          lu[p] = make_pair(sy, sx + s);
          rd[p] = make_pair(ey, ex);
          lu.push_back(make_pair(sy, sx));
          rd.push_back(make_pair(ey, sx + s));
        }
      } else if (dir == 3) {
        s = y - s;
        if (s < y - s) {
          lu[p] = make_pair(sy, sx);
          rd[p] = make_pair(sy + s, ex);
          lu.push_back(make_pair(sy + s, sx));
          rd.push_back(make_pair(ey, ex));
        } else {
          lu[p] = make_pair(sy + s, sx);
          rd[p] = make_pair(ey, ex);
          lu.push_back(make_pair(sy, sx));
          rd.push_back(make_pair(sy + s, ex));
        }
      } else {
        s = x - s;
        if (s < x - s) {
          lu[p] = make_pair(sy, sx);
          rd[p] = make_pair(ey, sx + s);
          lu.push_back(make_pair(sy, sx + s));
          rd.push_back(make_pair(ey, ex));
        } else {
          lu[p] = make_pair(sy, sx + s);
          rd[p] = make_pair(ey, ex);
          lu.push_back(make_pair(sy, sx));
          rd.push_back(make_pair(ey, sx + s));
        }
      }
    }
    vector<int> sq;
    for (int i = 0; i < rd.size(); i++) {
      sq.push_back(abs(rd[i].first - lu[i].first) *
                   abs(rd[i].second - lu[i].second));
    }
    sort(sq.begin(), sq.end());
    for (int i = 0; i < sq.size(); i++) {
      cout << sq[i];
      if (i < n) {
        cout << " ";
      }
    }
    cout << endl;
  }
  return 0;
}