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

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

using pii = pair<int, int>;
using ll = long long;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define repeat(i, j, k) for (int i = (j); i < (int)(k); i++)
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << " : " << x << endl

template <class T> bool set_min(T &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <class T> bool set_max(T &a, const T &b) {
  return a < b ? a = b, true : false;
}
// vector
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &a : v)
    is >> a;
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v)
    os << "\\t" << t;
  return os << endl;
}
// pair
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
  return os << "<" << v.first << ", " << v.second << ">";
}

const int INF = 1 << 30;
const ll INFL = 1LL << 60;

struct Piece {
  int d, w;
};

class Solver {
public:
  pair<Piece, Piece> split(const Piece &p, int s) {
    s = s % (p.d + p.w);
    if (s < p.d) {
      return make_pair(Piece{s, p.w}, Piece{p.d - s, p.w});
    } else if (s < p.d + p.w) {
      s -= p.d;
      return make_pair(Piece{p.d, s}, Piece{p.d, p.w - s});
    }
    assert(0);
  }
  int S(const Piece &p) { return p.d * p.w; }
  bool solve() {
    int N, W, D;
    cin >> N >> W >> D;
    if (N == 0 and W == 0 and D == 0)
      return 0;
    vector<Piece> pieces;
    pieces.push_back(Piece{W, D});
    rep(i, N) {
      int p, s;
      cin >> p >> s;
      p--;
      auto pp = split(pieces[p], s);
      pieces.erase(pieces.begin() + p);
      if (S(pp.first) <= S(pp.second)) {
        pieces.push_back(pp.first);
        pieces.push_back(pp.second);
      } else {
        pieces.push_back(pp.second);
        pieces.push_back(pp.first);
      }
    }
    sort(all(pieces),
         [&](const Piece &a, const Piece &b) { return S(a) <= S(b); });
    rep(i, pieces.size()) {
      cout << S(pieces[i]) << (i == pieces.size() - 1 ? "\\n" : " ");
    }
    return 1;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (Solver().solve())
    ;
  return 0;
}

Error: 0

Fixed submission:

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

using pii = pair<int, int>;
using ll = long long;
#define rep(i, j) for (int i = 0; i < (int)(j); i++)
#define repeat(i, j, k) for (int i = (j); i < (int)(k); i++)
#define all(v) v.begin(), v.end()
#define debug(x) cerr << #x << " : " << x << endl

template <class T> bool set_min(T &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <class T> bool set_max(T &a, const T &b) {
  return a < b ? a = b, true : false;
}
// vector
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &a : v)
    is >> a;
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v)
    os << "\\t" << t;
  return os << endl;
}
// pair
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
  return os << "<" << v.first << ", " << v.second << ">";
}

const int INF = 1 << 30;
const ll INFL = 1LL << 60;

struct Piece {
  int d, w;
};

class Solver {
public:
  pair<Piece, Piece> split(const Piece &p, int s) {
    s = s % (p.d + p.w);
    if (s < p.d) {
      return make_pair(Piece{s, p.w}, Piece{p.d - s, p.w});
    } else if (s < p.d + p.w) {
      s -= p.d;
      return make_pair(Piece{p.d, s}, Piece{p.d, p.w - s});
    }
    assert(0);
  }
  int S(const Piece &p) { return p.d * p.w; }
  bool solve() {
    int N, W, D;
    cin >> N >> W >> D;
    if (N == 0 and W == 0 and D == 0)
      return 0;
    vector<Piece> pieces;
    pieces.push_back(Piece{W, D});
    rep(i, N) {
      int p, s;
      cin >> p >> s;
      p--;
      auto pp = split(pieces[p], s);
      if (S(pp.first) <= S(pp.second)) {
        pieces[p] = pp.first;
        pieces.push_back(pp.second);
      } else {
        pieces[p] = pp.second;
        pieces.push_back(pp.first);
      }
    }
    sort(all(pieces),
         [&](const Piece &a, const Piece &b) { return S(a) <= S(b); });
    rep(i, pieces.size()) {
      cout << S(pieces[i]) << (i == pieces.size() - 1 ? "\\n" : " ");
    }
    return 1;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (Solver().solve())
    ;
  return 0;
}