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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;
typedef pair<pii, pii> pp;

int main(void) {
  int n, w, h;
  while (cin >> n >> w >> h && w && h) {
    vector<pp> g;
    g.push_back(make_pair(make_pair(0, 0), make_pair(w, h)));
    vector<int> area;
    area.push_back(w * h);
    while (n--) {
      int a, b;
      cin >> a >> b;
      a--;
      pii lt = g[a].first, rb = g[a].second;
      int w1 = lt.first, h1 = lt.second;
      int w2 = rb.first, h2 = rb.second;
      int tw = w2 - w1, th = h2 - h1;
      int s = tw * 2 + th * 2;
      b %= s;
      int type;
      if (b < tw)
        type = 0;
      else if (b < tw + th)
        type = 1;
      else if (b < 2 * tw + th)
        type = 2;
      else
        type = 3;

      pp p1, p2;
      switch (type) {
      case 0:
        p1 = make_pair(make_pair(w1, h1), make_pair(w1 + b, h2));
        p2 = make_pair(make_pair(w1 + b, h1), make_pair(w2, h2));
        break;
      case 1:
        b -= tw;
        p1 = make_pair(make_pair(w1, h1), make_pair(w2, h1 + b));
        p2 = make_pair(make_pair(w1, h1 + b), make_pair(w2, h2));
        break;
      case 2:
        b -= tw + th;
        p1 = make_pair(make_pair(w1, h1), make_pair(w2 - b, h2));
        p2 = make_pair(make_pair(w2 - b, h1), make_pair(w2, h2));
        break;
      case 3:
        b -= 2 * tw + th;
        p1 = make_pair(make_pair(w1, h1), make_pair(w2, h2 - b));
        p2 = make_pair(make_pair(w1, h2 - b), make_pair(w2, h2));
        break;
      }

      int sum1 = (p1.second.first - p1.first.first) *
                 (p1.second.second - p1.first.second);
      int sum2 = (p2.second.first - p2.first.first) *
                 (p2.second.second - p2.first.second);

      area.erase(area.begin() + a);
      g.erase(g.begin() + a);
      if (sum1 > sum2) {
        swap(sum1, sum2);
        swap(p1, p2);
      }
      area.push_back(sum1);
      g.push_back(p1);
      area.push_back(sum2);
      g.push_back(p2);
    }
    sort(area.begin(), area.end());
    for (int i = 0; i < area.size(); i++)
      cout << area[i] << (i != area.size() - 1 ? " " : "\\n");
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;
typedef pair<pii, pii> pp;

int main(void) {
  int n, w, h;
  while (cin >> n >> w >> h && w && h) {
    vector<pp> g;
    g.push_back(make_pair(make_pair(0, 0), make_pair(w, h)));
    vector<int> area;
    area.push_back(w * h);
    while (n--) {
      int a, b;
      cin >> a >> b;
      a--;
      pii lt = g[a].first, rb = g[a].second;
      int w1 = lt.first, h1 = lt.second;
      int w2 = rb.first, h2 = rb.second;
      int tw = w2 - w1, th = h2 - h1;
      int s = tw * 2 + th * 2;
      b %= s;
      int type;
      if (b < tw)
        type = 0;
      else if (b < tw + th)
        type = 1;
      else if (b < 2 * tw + th)
        type = 2;
      else
        type = 3;

      pp p1, p2;
      switch (type) {
      case 0:
        p1 = make_pair(make_pair(w1, h1), make_pair(w1 + b, h2));
        p2 = make_pair(make_pair(w1 + b, h1), make_pair(w2, h2));
        break;
      case 1:
        b -= tw;
        p1 = make_pair(make_pair(w1, h1), make_pair(w2, h1 + b));
        p2 = make_pair(make_pair(w1, h1 + b), make_pair(w2, h2));
        break;
      case 2:
        b -= tw + th;
        p1 = make_pair(make_pair(w1, h1), make_pair(w2 - b, h2));
        p2 = make_pair(make_pair(w2 - b, h1), make_pair(w2, h2));
        break;
      case 3:
        b -= 2 * tw + th;
        p1 = make_pair(make_pair(w1, h1), make_pair(w2, h2 - b));
        p2 = make_pair(make_pair(w1, h2 - b), make_pair(w2, h2));
        break;
      }

      int sum1 = (p1.second.first - p1.first.first) *
                 (p1.second.second - p1.first.second);
      int sum2 = (p2.second.first - p2.first.first) *
                 (p2.second.second - p2.first.second);

      // printf("s=%d type=%d | sum1=%d sum2=%d\\n",s,type,sum1,sum2);
      if (sum1 <= sum2) {
        area[a] = sum1;
        area.push_back(sum2);
        g[a] = p1;
        g.push_back(p2);
      } else {
        area[a] = sum2;
        area.push_back(sum1);
        g[a] = p2;
        g.push_back(p1);
      }
    }
    sort(area.begin(), area.end());
    for (int i = 0; i < area.size(); i++)
      cout << area[i] << (i != area.size() - 1 ? " " : "\\n");
  }
}