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

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

struct cake {
  // int num;
  int x;
  int y;
};

bool operation(const cake &l, const cake &r) {
  return (l.x * l.y) < (r.x * r.y);
}

int main() {
  int n, w, d;
  int p, s, num;
  int x, y;
  vector<cake> v;
  cake c, c2;
  while (1) {
    cin >> n >> w >> d;
    if (w == 0)
      break;
    v.clear();
    // c.num = 1;
    c.x = w;
    c.y = d;
    v.push_back(c);
    num = 1;
    for (int i = 0; i < n; i++) {
      cin >> p >> s;
      p--;
      s = s % (v[p].x * 2 + v[p].y * 2);
      if (s > 0 && s < v[p].x) {
        c.x = v[p].x - s;
        c.y = v[p].y;
        v[p].x = s;
      } else if (s > v[p].x + v[p].y && s < v[p].x * 2 + v[p].y) {
        c.x = v[p].x - (s - (v[p].x + v[p].y));
        c.y = v[p].y;
        v[p].x = v[p].x - c.x;
      } else if (s > v[p].x && s < v[p].x + v[p].y) {
        c.x = v[p].x;
        c.y = v[p].y - (s - v[p].x);
        v[p].y = v[p].y - c.y;
      } else if (s > v[p].x * 2 + v[p].y && s < v[p].x * 2 + v[p].y * 2) {
        c.x = v[p].x;
        c.y = v[p].y - (s - (v[p].x * 2 + v[p].y));
        v[p].y = v[p].y - c.y;
      }
      c2.x = v[p].x;
      c2.y = v[p].y;
      v.erase(v.begin() + p);
      if (c.x * c.y <= c2.x * c2.y) {
        v.push_back(c);
        v.push_back(c2);
      } else {
        v.push_back(c2);
        v.push_back(c);
      }
    }
    sort(v.begin(), v.end(), operation);
    for (int i = 0; i < v.size(); i++) {
      if (i != v.size() - 1)
        cout << v[i].x * v[i].y << " ";
      else
        cout << v[i].x * v[i].y << endl;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

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

struct cake {
  int num;
  int x;
  int y;
};

bool operation(const cake &l, const cake &r) { return l.x * l.y < r.x * r.y; }

int main() {
  int n, w, d;
  int p, s, num;
  int x, y;
  vector<cake> v;
  cake c;
  while (1) {
    cin >> n >> w >> d;
    if (w == 0)
      break;
    v.clear();
    c.num = 1;
    c.x = w;
    c.y = d;
    v.push_back(c);
    num = 1;
    for (int i = 0; i < n; i++) {
      cin >> p >> s;
      for (int j = 0; j < v.size(); j++) {
        if (v[j].num == p) {
          s %= v[j].x * 2 + v[j].y * 2;
          // cout << s << endl;
          if (s > 0 && s < v[j].x) {
            c.x = v[j].x - s;
            c.y = v[j].y;
            v[j].x = s;
          } else if (s > v[j].x + v[j].y && s < v[j].x * 2 + v[j].y) {
            c.x = v[j].x - (s - (v[i].x + v[j].y));
            c.y = v[j].y;
            v[j].x = v[j].x - c.x;
          } else if (s > v[j].x && s < v[j].x + v[j].y) {
            c.x = v[j].x;
            c.y = v[j].y - (s - v[j].x);
            v[j].y = v[j].y - c.y;
          } else if (s > v[j].x * 2 + v[j].y && s < v[j].x * 2 + v[j].y * 2) {
            c.x = v[j].x;
            c.y = v[j].y - (s - (v[j].x * 2 + v[j].y));
            v[j].y = v[j].y - c.y;
          }
          if (v[j].num != num)
            num++;
          if (c.x * c.y <= v[j].x * v[j].y) {
            c.num = num;
            num++;
            v[j].num = num;
          } else {
            v[j].num = num;
            num++;
            c.num = num;
          }
          v.push_back(c);
          break;
        }
      }
    }
    sort(v.begin(), v.end(), operation);
    for (int i = 0; i < v.size(); i++) {
      if (i != v.size() - 1)
        cout << v[i].x * v[i].y << " ";
      else
        cout << v[i].x * v[i].y << " " << endl;
    }
  }
  return 0;
}