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

#include <algorithm>
#include <climits>
#include <complex>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define EPS 1e-8

class cake {
public:
  int id, x, y;
  cake(int _id, int _x, int _y) {
    id = _id;
    x = _x;
    y = _y;
  }
  bool operator<(const cake &a) const { return x * y < a.x * a.y; }
};
int main() {
  int n, w, d, p, s;
  vector<cake> v;
  while (scanf("%d%d%d", &n, &w, &d), n | w | d) {
    v.clear();
    v.push_back(cake(1, w, d));

    rep(i, n) {
      scanf("%d%d", &p, &s);
      int vz = v.size();
      rep(j, vz) if (p == v[j].id) {
        // printf("%d , %d , %d  --> kirikuti:%d , %d\\n",s,v[j].x,v[j].y,
        //	s-(2*(v[j].x+v[j].y)),s%(2*(v[j].x+v[j].y)));
        /*
        if( 2*(v[j].x+v[j].y)<s ){
                s -= (2*(v[j].x+v[j].y));
        }
        */
        s %= (2 * (v[j].x + v[j].y));
        if (s < v[j].x ||
            v[j].x + v[j].y < s && s < 2 * v[j].x + v[j].y) { // tate
          if (s > v[j].x) {
            s = s - (v[j].x + v[j].y);
          }
          int pe1_x = s;
          int pe2_x = v[j].x - s;
          int sz = v.size();
          // v[j].x = min(pe1_x, pe2_x);
          v.push_back(cake(sz + 1, min(pe1_x, pe2_x), v[j].y));
          v.push_back(cake(sz + 2, max(pe1_x, pe2_x), v[j].y));
          v.erase(v.begin() + j);
          rep(j, v.size()) v[j].id = j + 1;

          /*
          printf("x:%d,%d\\n",pe1_x, pe2_x);
          rep(k,v.size()){
                  printf("%d,%d,%d\\n",v[k].id, v[k].x, v[k].y);
          }
          */

        } else {
          // printf("YYYY: s:%d , %d\\n",s,2*v[j].x+v[j].y);
          if (s > 2 * v[j].x + v[j].y) {
            s = 2 * (v[j].x + v[j].y) - s;
            // printf("YYYY: after_1s:%d\\n",s);
          } else {
            s -= v[j].x;
            // printf("YYYY: after_2s:%d\\n",s);
          }
          int pe1_y = s;
          int pe2_y = v[j].y - s;
          if (pe1_y == -1 || pe2_y == -1) {
            printf("__________________________________\\n");
          }
          int sz = v.size();
          // v[j].y = min(pe1_y, pe2_y);
          v.push_back(cake(sz + 1, v[j].x, min(pe1_y, pe2_y)));
          v.push_back(cake(sz + 2, v[j].x, max(pe1_y, pe2_y)));
          v.erase(v.begin() + j);

          rep(j, v.size()) v[j].id = j + 1;

          /*
          printf("y:%d,%d\\n",pe1_y, pe2_y);
          rep(k,v.size()){
                  printf("%d,%d,%d\\n",v[k].id, v[k].x, v[k].y);
          }
          */
        }
        break;
      }
    }
    sort(all(v));
    rep(i, v.size()) {
      if (i != 0)
        putchar(' ');
      printf("%d", v[i].x * v[i].y);
    }
    puts("");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <complex>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, a, n) for (int i = (a); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define EPS 1e-8

class cake {
public:
  int id, x, y;
  cake(int _id, int _x, int _y) {
    id = _id;
    x = _x;
    y = _y;
  }
  bool operator<(const cake &a) const { return x * y < a.x * a.y; }
};
int main() {
  int n, w, d, p, s;
  vector<cake> v;
  while (scanf("%d%d%d", &n, &w, &d), n | w | d) {
    v.clear();
    v.push_back(cake(1, w, d));

    rep(i, n) {
      scanf("%d%d", &p, &s);
      rep(j, v.size()) if (p == v[j].id) {
        s %= (2 * (v[j].x + v[j].y));
        if (s < v[j].x ||
            v[j].x + v[j].y < s && s < 2 * v[j].x + v[j].y) { // tate
          if (s > v[j].x) {
            s = v[j].x - (s - (v[j].x + v[j].y));
          }
          int pe1_x = s;
          int pe2_x = v[j].x - s;
          v[j].x = min(pe1_x, pe2_x);
          int sz = v.size();
          v.push_back(cake(sz + 1, max(pe1_x, pe2_x), v[j].y));

          /*
          printf("x:%d,%d\\n",pe1_x, pe2_x);
          rep(k,v.size()){
                  printf("%d,%d,%d\\n",v[k].id, v[k].x, v[k].y);
          }
          */

        } else {
          if (s > 2 * v[j].x + v[j].y) {
            s = 2 * (v[j].x + v[j].y) - s;
          } else {
            s -= v[j].x;
          }
          int pe1_y = s;
          int pe2_y = v[j].y - s;
          int sz = v.size();
          v[j].y = min(pe1_y, pe2_y);
          v.push_back(cake(sz + 1, v[j].x, max(pe1_y, pe2_y)));

          /*
          printf("y:%d,%d\\n",pe1_y, pe2_y);
          rep(k,v.size()){
                  printf("%d,%d,%d\\n",v[k].id, v[k].x, v[k].y);
          }
          */
        }
        break;
      }
    }
    sort(all(v));
    rep(i, v.size()) {
      if (i != 0)
        putchar(' ');
      printf("%d", v[i].x * v[i].y);
    }
    puts("");
  }
  return 0;
}