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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P; // w, h

int n, w, d;
int id, pos;
vector<P> rects;

void cut(int old_id, int new_id, int type, int pos) {
  if (type == 0) { // x???
    rects[new_id].first = rects[old_id].first - pos;
    rects[new_id].second = rects[old_id].second;
    rects[old_id].first = pos;
  } else { // y???
    rects[new_id].first = rects[old_id].first;
    rects[new_id].second = rects[old_id].second - pos;
    rects[old_id].second = pos;
  }

  //????????????????????±????????????????????????????????????
  for (int i = old_id; i < new_id - 1; i++) {
    swap(rects[i], rects[i + 1]);
  }
}

signed main() {
  rects.resize(200);

  while (cin >> n >> w >> d) {
    if (!n && !w && !d)
      break;
    rects[0] = P(w, d);

    for (int i = 0; i < n; i++) {
      cin >> id >> pos;
      id--;

      // cut
      int qw = rects[id].first;
      int qh = rects[id].second;

      pos %= (2 * (qw + qh)); // 0???????????¨???

      if (pos < qw)
        cut(id, i + 1, 0, min(pos, qw - pos));
      else if (pos < qw + qh)
        cut(id, i + 1, 1, min(qw + qh - pos, qh - (qw + qh - pos)));
      else if (pos < 2 * qw + qh)
        cut(id, i + 1, 0, min(2 * qw + qh - pos, qw - (2 * qw + qh - pos)));
      else
        cut(id, i + 1, 1, min(2 * (qw + qh) - pos, qh - (2 * (qw + qh) - pos)));
    }

    vector<int> areas;
    for (int i = 0; i < n + 1; i++)
      areas.push_back(rects[i].first * rects[i].second);
    sort(areas.begin(), areas.end());
    for (int i = 0; i < n + 1; i++) {
      cout << areas[i];
      if (i < n)
        cout << " ";
    }
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P; // w, h

int n, w, d;
int id, pos;
vector<P> rects;

void cut(int old_id, int new_id, int type, int pos) {
  if (type == 0) { // x???
    rects[new_id].first = rects[old_id].first - pos;
    rects[new_id].second = rects[old_id].second;
    rects[old_id].first = pos;
  } else { // y???
    rects[new_id].first = rects[old_id].first;
    rects[new_id].second = rects[old_id].second - pos;
    rects[old_id].second = pos;
  }
}

signed main() {
  rects.resize(200);

  while (cin >> n >> w >> d) {
    if (!n && !w && !d)
      break;
    rects[0] = P(w, d);

    for (int i = 0; i < n; i++) {
      cin >> id >> pos;
      id--;

      // cut
      int qw = rects[id].first;
      int qh = rects[id].second;

      pos %= 2 * (qw + qh); // 0???????????¨???

      if (pos < qw)
        cut(id, i + 1, 0, min(pos, qw - pos));
      else if (pos < qw + qh)
        cut(id, i + 1, 1, min(qw + qh - pos, qh - (qw + qh - pos)));
      else if (pos < 2 * qw + qh)
        cut(id, i + 1, 0, min(2 * qw + qh - pos, qw - (2 * qw + qh - pos)));
      else
        cut(id, i + 1, 1, min(2 * (qh + qw) - pos, qh - (2 * (qh + qw) - pos)));
    }

    vector<int> areas;
    for (int i = 0; i < n + 1; i++)
      areas.push_back(rects[i].first * rects[i].second);
    sort(areas.begin(), areas.end());
    for (int i = 0; i < n + 1; i++) {
      cout << areas[i];
      if (i < n)
        cout << " ";
    }
    cout << endl;
  }
  return 0;
}