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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

P dat[100010];
bool f[100010];
int n, w, d;

int main() {
  while (cin >> n >> w >> d && n + w + d) {
    int k = 1;
    fill(f, f + 100010, 0);
    dat[0] = {w, d};
    REP(i, n) {
      int p, s;
      cin >> p >> s;
      int cnt = 0;
      int pos = 0;
      while (1) {
        if (!f[pos]) {
          cnt++;
          if (cnt == p)
            break;
        }
        pos++;
      }
      p = pos;
      f[p] = 1;
      int nw = dat[p].first, nd = dat[p].second;

      s %= (2 * nw + 2 * nd);
      // cout << s << " " << nw << " " << nd << endl;
      if (s <= nw) {
        dat[k] = {min(nw - s, s), nd};
        dat[k + 1] = {max(nw - s, s), nd};
      } else if (s <= nw + nd) {
        dat[k] = {nw, min(nd - (s - nw), s - nw)};
        dat[k + 1] = {nw, max(nd - (s - nw), s - nw)};
      } else if (s <= 2 * nw + nd) {
        dat[k] = {min(nw - (s - nw - nd), s - nw - nd), nd};
        dat[k + 1] = {max(nw - (s - nw - nd), s - nw - nd), nd};
      } else {
        dat[k] = {nw, min(nd - (s - 2 * nw - nd), s - 2 * nw - nd)};
        dat[k + 1] = {nw, max(nd - (s - 2 * nw - nd), s - 2 * nw - nd)};
      }
      k += 2;
      // REP(j, k) {
      //	if(!f[j])cout << dat[j].first << " " << dat[j].second << endl;
      // }
      // cout << endl;
    }
    vector<int> vi;
    REP(i, n + 1) {
      if (!f[i])
        vi.push_back(dat[i].first * dat[i].second);
      else
        n++;
    }
    sort(ALL(vi));
    REP(i, vi.size()) {
      if (i != 0)
        cout << " " << vi[i];
      else
        cout << vi[i];
    }
    cout << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

P dat[100010];
bool f[100010];
int n, w, d;

int main() {
  while (cin >> n >> w >> d && n + w + d) {
    int k = 1;
    fill(f, f + 100010, 0);
    dat[0] = {w, d};
    REP(i, n) {
      int p, s;
      cin >> p >> s;
      int cnt = 0;
      int pos = 0;
      while (1) {
        if (!f[pos]) {
          cnt++;
          if (cnt == p)
            break;
        }
        pos++;
      }
      p = pos;
      f[p] = 1;
      int nw = dat[p].first, nd = dat[p].second;

      s %= (2 * nw + 2 * nd);
      // cout << s << " " << nw << " " << nd << endl;
      if (s <= nw) {
        dat[k] = {min(nw - s, s), nd};
        dat[k + 1] = {max(nw - s, s), nd};
      } else if (s <= nw + nd) {
        dat[k] = {nw, min(nd - (s - w), s - w)};
        dat[k + 1] = {nw, max(nd - (s - w), s - w)};
      } else if (s <= 2 * nw + nd) {
        dat[k] = {min(nw - (s - w - d), s - w - d), nd};
        dat[k + 1] = {max(nw - (s - w - d), s - w - d), nd};
      } else {
        dat[k] = {nw, min(nd - (s - 2 * w - d), s - 2 * w - d)};
        dat[k + 1] = {nw, max(nd - (s - 2 * w - d), s - 2 * w - d)};
      }
      k += 2;
      // REP(j, k) {
      //	cout << dat[j].first << " " << dat[j].second << endl;
      // }
      // cout << endl;
    }
    vector<int> vi;
    REP(i, n + 1) {
      if (!f[i])
        vi.push_back(dat[i].first * dat[i].second);
      else
        n++;
    }
    sort(ALL(vi));
    REP(i, vi.size()) { cout << vi[i] << " "; }
    cout << endl;
  }
}