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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;

struct cake {
  int w, h;
  int area;
  cake(int aw, int ah) {
    w = aw;
    h = ah;
    area = w * h;
  }
};

bool cake_cmp(const cake &lhs, const cake &rhs) { return lhs.area < rhs.area; }

int main() {
  while (true) {
    int n, w, h;
    cin >> n >> w >> h;
    if (n == 0 and w == 0 and h == 0)
      break;
    vector<cake> cakes;
    cakes.push_back(cake(w, h));
    for (int i = 0; i < n; i++) {
      int p, s;
      cin >> p >> s;
      p--;
      cake c = cakes[p];
      cakes.erase(cakes.begin() + p);
      vector<cake> ps(2, cake(0, 0));
      s %= (2 * (c.w + c.h));
      if (0 <= s and s <= c.w) {
        ps[0] = cake(c.w - s, c.h);
        ps[1] = cake(s, c.h);
      } else if (c.w <= s and s <= c.w + c.h) {
        int rest = s - c.w;
        // s <= c.w + c.h
        // rest <= c.h
        ps[0] = cake(c.w, c.h - (rest));
        ps[1] = cake(c.w, rest);
      } else if (c.w + c.h <= s and s <= c.w + c.h + c.w) {
        int rest = s - (c.w + c.h);
        ps[0] = cake(rest, c.h);
        ps[1] = cake(c.w - rest, c.h);
      } else {
        int rest = s - (c.w + c.h + c.w);
        ps[0] = cake(c.w, rest);
        ps[1] = cake(c.w, c.h - rest);
      }
      if (ps[0].area > ps[1].area) {
        swap(ps[0], ps[1]);
      }
      for (int i = 0; i < 2; i++) {
        cakes.push_back(ps[i]);
      }
    }

    sort(all(cakes), cake_cmp);
    for (int i = 0; i < cakes.size(); i++) {
      cout << cakes[i].area;
      if (i == cakes.size() - 1) {
        cout << endl;
      } else {
        cout << " ";
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;

struct cake {
  int w, h;
  int area;
  cake(int aw, int ah) {
    w = aw;
    h = ah;
    area = w * h;
  }
};

bool cake_cmp(const cake &lhs, const cake &rhs) { return lhs.area < rhs.area; }

int main() {
  while (true) {
    int n, w, h;
    cin >> n >> w >> h;
    if (n == 0 and w == 0 and h == 0)
      break;
    vector<cake> cakes;
    cakes.push_back(cake(w, h));
    for (int i = 0; i < n; i++) {
      cerr << endl;
      int p, s;
      cin >> p >> s;
      p--;
      cake c = cakes[p];
      cakes.erase(cakes.begin() + p);
      vector<cake> ps(2, cake(0, 0));
      s %= (2 * (c.w + c.h));
      if (0 <= s and s <= c.w) {
        ps[0] = cake(c.w - s, c.h);
        ps[1] = cake(s, c.h);
      } else if (c.w <= s and s <= c.w + c.h) {
        int rest = s - c.w;
        // s <= c.w + c.h
        // rest <= c.h
        ps[0] = cake(c.w, c.h - (rest));
        ps[1] = cake(c.w, rest);
      } else if (c.w + c.h <= s and s <= c.w + c.h + c.w) {
        int rest = s - (c.w + c.h);
        ps[0] = cake(rest, c.h);
        ps[1] = cake(c.w - rest, c.h);
      } else {
        int rest = s - (c.w + c.h + c.w);
        ps[0] = cake(c.w, rest);
        ps[1] = cake(c.w, c.h - rest);
      }
      if (ps[0].area > ps[1].area) {
        swap(ps[0], ps[1]);
      }
      for (int i = 0; i < 2; i++) {
        cakes.push_back(ps[i]);
      }
    }

    sort(all(cakes), cake_cmp);
    for (int i = 0; i < cakes.size(); i++) {
      cout << cakes[i].area;
      if (i == cakes.size() - 1) {
        cout << endl;
      } else {
        cout << " ";
      }
    }
  }
  return 0;
}