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

#ifndef _WIN32
#include <iostream>
#endif
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <queue>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

struct P {
  int id;
  int x, y;

  bool operator<(P &a) const {
    if (id != a.id) {
      return id < a.id;
    }
    return x * y < a.x * a.y;
  }
};

int N, W, D;
P C[10010];
vector<int> X;

int main() {
  while (true) {
    cin >> N >> W >> D;
    if (!N && !W && !D)
      break;
    C[0].id = 0;
    C[0].x = W;
    C[0].y = D;

    for (int i = 0; i < N; ++i) {
      int w, h;
      int p, s;

      int p1[2], p2[2];

      cin >> p >> s;
      --p;

      w = C[p].x;
      h = C[p].y;

      s = s % (2 * w + 2 * h);

      if (s < w) {
        p1[0] = s;
        p1[1] = h;
        p2[0] = w - s;
        p2[1] = h;
      } else if (w < s && s < w + h) {
        p1[0] = w;
        p1[1] = s - w;
        p2[0] = w;
        p2[1] = h - (s - w);
      } else if (w + h < s && s < w + w + h) {
        p1[0] = w - (s - (w + h));
        p1[1] = h;
        p2[0] = s - (w + h);
        p2[1] = h;
      } else {
        p1[0] = w;
        p1[1] = h - (s - (w + w + h));
        p2[0] = w;
        p2[1] = s - (w + w + h);
      }
      if (p1[0] * p1[1] > p2[0] * p2[1]) {
        p1[0] ^= p2[0] ^= p1[0] ^= p2[0];
        p1[1] ^= p2[1] ^= p1[1] ^= p2[1];
      }
      C[p].id = i + 1;
      C[p].x = p1[0];
      C[p].y = p1[1];
      C[i + 1].id = i + 1;
      C[i + 1].x = p2[0];
      C[i + 1].y = p2[1];

      sort(C, C + (i + 1));
    }

    X.clear();
    for (int i = 0; i <= N; ++i) {
      X.push_back(C[i].x * C[i].y);
    }
    sort(X.begin(), X.end());

    cout << X[0];
    for (int i = 1; i <= N; ++i) {
      cout << " " << X[i];
    }
    cout << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#ifndef _WIN32
#include <iostream>
#endif
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <queue>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

int N, W, D;
int C[10010][2];
vector<int> X;

int main() {
  while (true) {
    cin >> N >> W >> D;
    if (!N && !W && !D)
      break;
    C[0][0] = W;
    C[0][1] = D;

    for (int i = 0; i < N; ++i) {
      int w, h;
      int p, s;

      int p1[2], p2[2];

      cin >> p >> s;
      --p;

      w = C[p][0];
      h = C[p][1];

      s = s % (2 * w + 2 * h);

      if (s < w) {
        p1[0] = s;
        p1[1] = h;
        p2[0] = w - s;
        p2[1] = h;
      } else if (w < s && s < w + h) {
        p1[0] = w;
        p1[1] = s - w;
        p2[0] = w;
        p2[1] = h - (s - w);
      } else if (w + h < s && s < w + w + h) {
        p1[0] = w - (s - (w + h));
        p1[1] = h;
        p2[0] = s - (w + h);
        p2[1] = h;
      } else {
        p1[0] = w;
        p1[1] = h - (s - (w + w + h));
        p2[0] = w;
        p2[1] = s - (w + w + h);
      }
      if (p1[0] * p1[1] > p2[0] * p2[1]) {
        p1[0] ^= p2[0] ^= p1[0] ^= p2[0];
        p1[1] ^= p2[1] ^= p1[1] ^= p2[1];
      }
      C[p][0] = p1[0];
      C[p][1] = p1[1];
      C[i + 1][0] = p2[0];
      C[i + 1][1] = p2[1];
    }

    X.clear();
    for (int i = 0; i <= N; ++i) {
      X.push_back(C[i][0] * C[i][1]);
    }
    sort(X.begin(), X.end());

    cout << X[0];
    for (int i = 1; i <= N; ++i) {
      cout << " " << X[i];
    }
    cout << endl;
  }

  return 0;
}