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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val)                                                      \\
  for (int i = 0; i < n; ++i)                                                  \\
    cout << val << " ";                                                        \\
  cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 10000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
// typedef pair<ll, ll> P;
typedef pair<P, int> PI;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;

#define N 1005
P sz[N];

int main() {
  int n, w, d;
  while (cin >> n >> w >> d && (n + w + d)) {
    sz[0] = P(w, d);
    int idx = 1;
    rep(i, n) {
      int p, s;
      cin >> p >> s;
      int c = -1;
      while (p) {
        if (sz[++c].first != -1) {
          --p;
        }
      }
      P &e = sz[c];
      s %= (e.first + e.second);
      // cout << "e" SP c SP s SP e.first SP e.second << endl;
      if (s == 0 || s == e.first) {
        cout << "Failed" << endl;
        exit(1);
      }
      if (0 <= s && s < e.first) {
        int nw = maxd(s, e.first - s);
        // cout << "w" SP s SP nw SP e.first SP e.second << endl;
        sz[idx] = P(e.first - nw, e.second);
        sz[idx + 1] = P(nw, e.second);
      } else {
        s -= e.first;
        int nw = maxd(s, e.second - s);
        // cout << "d" SP s SP nw SP e.first SP e.second << endl;
        sz[idx] = P(e.first, e.second - nw);
        sz[idx + 1] = P(e.first, nw);
      }
      sz[c].first = -1;
      idx += 2;
    }
    vector<int> v;
    rep(i, idx) {
      P &e = sz[i];
      if (e.first != -1) {
        v.push_back(e.first * e.second);
      }
    }
    sort(ALL(v));
    rep(i, n + 1) { cout << (i ? " " : "") << v[i]; }
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mind(a, b) (a > b ? b : a)
#define maxd(a, b) (a > b ? a : b)
#define absd(x) (x < 0 ? -(x) : x)
#define pow2(x) ((x) * (x))
#define rep(i, n) for (int i = 0; i < n; ++i)
#define repr(i, n) for (int i = n - 1; i >= 0; --i)
#define repl(i, s, n) for (int i = s; i <= n; ++i)
#define replr(i, s, n) for (int i = n; i >= s; --i)
#define repf(i, s, n, j) for (int i = s; i <= n; i += j)
#define repe(e, obj) for (auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X, Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i, n, val)                                                      \\
  for (int i = 0; i < n; ++i)                                                  \\
    cout << val << " ";                                                        \\
  cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 10000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
// typedef pair<ll, ll> P;
typedef pair<P, int> PI;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P>> pvqueue;

#define N 103

int main() {
  int n, w, d;
  P sz[N];
  while (cin >> n >> w >> d && n + w + d) {
    sz[0] = P(w, d);
    rep(i, n) {
      int p, s;
      cin >> p >> s;
      --p;
      P &e = sz[p];
      s %= 2 * (e.first + e.second);
      if (0 <= s && s <= e.first) {
        int nw = maxd(s, e.first - s);
        sz[p] = P(e.first - nw, e.second);
        sz[i + 1] = P(nw, e.second);
      } else if (e.first <= s && s <= e.first + e.second) {
        int nw = e.first + e.second - s;
        nw = maxd(nw, e.second - nw);
        sz[p] = P(e.first, e.second - nw);
        sz[i + 1] = P(e.first, nw);
      } else if (e.first + e.second <= s && s <= e.first * 2 + e.second) {
        int nw = nw - e.first - e.second;
        ;
        nw = maxd(nw, e.first - nw);
        sz[p] = P(e.first - nw, e.second);
        sz[i + 1] = P(nw, e.second);
      } else {
        int nw = 2 * (e.first + e.second) - s;
        nw = maxd(nw, e.second - nw);
        sz[p] = P(e.first, e.second - nw);
        sz[i + 1] = P(e.first, nw);
      }
    }
    vector<int> v;
    rep(i, n + 1) {
      P &e = sz[i];
      v.push_back(e.first * e.second);
    }
    sort(ALL(v));
    rep(i, n + 1) { cout << (i ? " " : "") << v[i]; }
    cout << endl;
  }
  return 0;
}