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

#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
const int MOD = 1000000007;
const int INF = 1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps = 1e-9;
const int inf = 1e9;
typedef pair<int, int> P;
struct Point {
  double x, y, z;
};
Point operator-(const Point &a, const Point &b) {
  Point ret = {a.x - b.x, a.y - b.y, a.z - b.z};
  return ret;
}
double dot(Point a, Point b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
Point cross(Point a, Point b) {
  Point ret = {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z,
               a.x * b.y - a.y * b.x};
  return ret;
}
double norm(Point a) { return a.x * a.x + a.y * a.y + a.z * a.z; }

typedef pair<int, int> P;
int n, w, d;

class cake {
public:
  int w, h, squ;
  cake(){};
  cake(int w, int h) : w(w), h(h), squ(w * h) {}
  bool operator<(const cake &you) const { return squ < you.squ; };
};
int area(const cake &tmp) { return tmp.w * tmp.h; };
int main(int argc, char const *argv[]) {
  while (cin >> n >> w >> d && (n != 0 || w != 0 || d != 0)) {
    vector<cake> c;
    c.push_back(cake(w, d));
    int lastindex = 0;
    for (int i = 0; i < n; i++) {
      lastindex++;
      int id, s;
      cin >> id >> s;
      id--;
      int width = c[id].w;
      int height = c[id].h;
      s %= (c[id].w * 2 + c[id].h * 2);
      cake c1, c2;
      if (s > 0 && s < width) {
        c1 = cake(s, height);
        c2 = cake(width - s, height);

      } else if (s > width && s < (width + height)) {
        c1 = cake(width, s - width);
        c2 = cake(width, height - (s - width));
      } else if (s > (width + height) && s < (2 * width + height)) {
        c1 = cake(width - (s - width - height), height);
        c2 = cake(s - width - height, height);
      } else if (s > (2 * width + height)) {
        c1 = cake(width, s - 2 * width - height);
        c2 = cake(width, height - (s - 2 * width - height));
      }
      c.erase(c.begin() + id);
      if (area(c1) < area(c2)) {
        c.push_back(c1);
        c.push_back(c2);
      } else {
        c.push_back(c2);
        c.push_back(c1);
      }
    }
    sort(c.begin(), c.end());
    for (int i = 0; i < c.size(); i++) {
      if (i)
        cout << ' ';
      cout << c[i].squ;
    }
    cout << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
const int MOD = 1000000007;
const int INF = 1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps = 1e-9;
const int inf = 1e9;
typedef pair<int, int> P;
struct Point {
  double x, y, z;
};
Point operator-(const Point &a, const Point &b) {
  Point ret = {a.x - b.x, a.y - b.y, a.z - b.z};
  return ret;
}
double dot(Point a, Point b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
Point cross(Point a, Point b) {
  Point ret = {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z,
               a.x * b.y - a.y * b.x};
  return ret;
}
double norm(Point a) { return a.x * a.x + a.y * a.y + a.z * a.z; }

typedef pair<int, int> P;
int n, w, d;

class cake {
public:
  int id, w, h, squ;
  cake(int id, int w, int h) : id(id), w(w), h(h), squ(w * h) {}
  bool operator<(const cake &you) const { return squ < you.squ; };
};
int main(int argc, char const *argv[]) {
  while (cin >> n >> w >> d && (n != 0 || w != 0 || d != 0)) {
    vector<cake> c;
    c.push_back(cake(0, w, d));
    int lastindex = 0;
    for (int i = 0; i < n; i++) {
      lastindex++;
      int id, s;
      cin >> id >> s;
      id--;
      int width = c[id].w;
      int height = c[id].h;
      s %= (c[id].w * 2 + c[id].h * 2);
      if (s > 0 && s < width) {
        if (s >= width / 2) {
          c.erase(c.begin() + id);
          c.push_back(cake(lastindex, s, height));
          c.push_back(cake(id, width - s, height));
        } else {
          c.erase(c.begin() + id);
          c.push_back(cake(id, s, height));
          c.push_back(cake(lastindex, width - s, height));
        }
      } else if (s > width && s < (width + height)) {
        if ((s - width) >= height / 2) {
          c.erase(c.begin() + id);
          c.push_back(cake(lastindex, width, s - width));
          c.push_back(cake(id, width, height - (s - width)));
        } else {
          c.erase(c.begin() + id);
          c.push_back(cake(id, width, s - width));
          c.push_back(cake(lastindex, width, height - (s - width)));
        }
      } else if (s > (width + height) && s < (2 * width + height)) {
        if ((s - width - height) >= width / 2) {
          c.erase(c.begin() + id);
          c.push_back(cake(id, width - (s - width - height), height));
          c.push_back(cake(lastindex, s - width - height, height));
        } else {
          c.erase(c.begin() + id);
          c.push_back(cake(lastindex, width - (s - width - height), height));
          c.push_back(cake(id, s - width - height, height));
        }
      } else if (s > (2 * width + height)) {
        if ((s - 2 * width - height) >= height / 2) {
          c.erase(c.begin() + id);
          c.push_back(cake(lastindex, width, s - 2 * width - height));
          c.push_back(cake(id, width, height - (s - 2 * width - height)));
        } else {
          c.erase(c.begin() + id);
          c.push_back(cake(id, width, s - 2 * width - height));
          c.push_back(
              cake(lastindex, width, height - (s - 2 * width - height)));
        }
      }
    }
    sort(c.begin(), c.end());
    for (int i = 0; i < c.size(); i++) {
      if (i)
        cout << ' ';
      cout << c[i].squ;
    }
    cout << endl;
  }
  return 0;
}