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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

struct cake {
  int w, d, p;
  cake(int w_, int d_, int p_) {
    w = w_;
    d = d_;
    p = p_;
  }
  cake() {
    w = 0;
    d = 0;
    p = 0;
  }
};
typedef pair<cake, cake> P;

// c := 切ろうとしているケーキ, s := 切る位置
P cut(cake c, int s) {
  int w1, w2, d1, d2;

  s %= c.w + c.d;
  if (s < c.w) {
    w1 = s;
    w2 = c.w - s;
    d1 = d2 = c.d;
  } else {
    s -= c.w;
    d1 = s;
    d2 = c.d - s;
    w1 = w2 = c.w;
  }
  if (w1 * d1 > w2 * d2) {
    swap(w1, w2);
    swap(d1, d2);
  }
  // firstが小さいケーキ, secondが大きいケーキ
  cake c1(w1, d1, 0);
  cake c2(w2, d2, 0);
  P result(c1, c2);
  return result;
}

int main() {
  int n, w, d;
  while (cin >> n >> w >> d, n || w || d) {
    vector<cake> now;
    vector<cake> next;

    next.push_back(cake(w, d, 1));
    for (int i = 1; i <= n; i++) {
      int p, s;
      now = next;
      next.clear();

      cin >> p >> s;
      for (int j = 0; j < now.size(); j++) {
        if (now[j].p == p) {
          P p_cake = cut(now[j], s);
          // 小さいケーキ
          p_cake.first.p = i;
          next.push_back(p_cake.first);
          // 大きいケーキ
          p_cake.second.p = i + 1;
          next.push_back(p_cake.second);
        } else {
          if (now[j].p > p)
            now[j].p--;
          next.push_back(now[j]);
        }
      }
    }
    vector<int> ans;
    for (int i = 0; i < next.size(); i++) {
      ans.push_back(next[i].w * next[i].d);
    }
    sort(ans.begin(), ans.end());
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i];
      if (i == ans.size() - 1)
        cout << endl;
      else
        cout << " ";
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

const int MAX_N = 300;

struct cake {
  int w, d, p;
  cake(int w_, int d_, int p_) {
    w = w_;
    d = d_;
    p = p_;
  }
  cake() {
    w = 0;
    d = 0;
    p = 0;
  }
};

// ケーキ
cake c[MAX_N];

void init() {
  for (int i = 0; i < MAX_N; i++) {
    c[i] = cake();
  }
}

// c := 切ろうとしているケーキ, s := 切る位置
pair<cake, cake> cut_2(cake c, int s) {
  int w1, w2, d1, d2;

  s %= c.w + c.d;
  if (s < c.w) {
    w1 = s;
    w2 = c.w - s;
    d1 = d2 = c.d;
  } else {
    s -= c.w;
    d1 = s;
    d2 = c.d - s;
    w1 = w2 = c.w;
  }
  if (w1 * d1 > w2 * d2) {
    swap(w1, w2);
    swap(d1, d2);
  }
  // firstが小さいケーキ, secondが大きいケーキ
  cake c1(w1, d1, 0);
  cake c2(w2, d2, 0);
  pair<cake, cake> result(c1, c2);
  return result;
}

// n := 今のケーキの個数-1, 番号 p のケーキの位置 s から切る
void cut(int n, int p, int s) {
  for (int i = 0; i <= n; i++) {
    if (c[i].p == p) {
      pair<cake, cake> p_cake = cut_2(c[i], s);
      // 小さいケーキ
      c[i] = p_cake.first;
      c[i].p = p;
      // 大きいケーキ
      c[n + 1] = p_cake.second;
      c[n + 1].p = n + 2;
      break;
    }
  }
}

int main() {
  int n, w, d;
  while (cin >> n >> w >> d, n || w || d) {
    // 初期化
    init();

    c[0].w = w;
    c[0].d = d;
    c[0].p = 1;
    for (int i = 0; i < n; i++) {
      int p, s;
      cin >> p >> s;
      cut(i, p, s);
    }
    vector<int> ans;
    for (int i = 0; i <= n; i++) {
      ans.push_back(c[i].w * c[i].d);
    }
    sort(ans.begin(), ans.end());
    for (int i = 0; i < ans.size(); i++) {
      cout << ans[i];
      if (i == ans.size() - 1)
        cout << endl;
      else
        cout << " ";
    }
  }
}