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

#include <bits/stdc++.h>
using namespace std;
#define mk make_pair
#define f first
#define s second
int main() {
  int n, w, h, q1, q2;
  while (cin >> n >> w >> h, h) {
    vector<pair<int, int>> v;
    v.push_back(mk(w, h));
    for (int i = 0; i < n; i++) {
      cin >> q1 >> q2;
      q1--;
      q2 = q2 % (v[q1].f + v[q1].s);
      if (q2 <= v[q1].f) {
        v.push_back(mk(q2, v[q1].s));
        v.push_back(mk(v[q1].f - q2, v[q1].s));
        if (q2 > v[q1].f - q2)
          swap(v[v.size() - 1], v[v.size() - 2]);
        v.erase(v.begin() + q1);
      } else {
        q2 -= v[q1].f;
        v.push_back(mk(v[q1].f, q2));
        v.push_back(mk(v[q1].f, v[q1].s - q2));
        if (q2 > v[q1].s - q2)
          swap(v[v.size() - 1], v[v.size() - 2]);
        v.erase(v.begin() + q1);
      }
    }
    vector<int> p;
    for (int i = 0; i < v.size(); i++)
      p.push_back(v[i].f * v[i].s);
    sort(p.begin(), p.end());
    for (int i = 0; i < p.size(); i++) {
      if (i)
        cout << ' ';
      cout << p[i];
    }
    cout << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define mk make_pair
#define s second
#define f first
using namespace std;
int main() {
  pair<pair<int, int>, int> p1, p2;
  int n, w, h, p, k, o;
  while (cin >> n >> w >> h, h) {
    vector<pair<pair<int, int>, int>> v;
    v.push_back(mk(mk(w, h), 1));
    while (n--) {
      int qq = 0;
      cin >> p >> k;
      for (int i = 0; i < v.size(); i++)
        if (v[i].s == p)
          o = i;
      k = k % (v[o].f.f * 2 + v[o].f.s * 2);
      if (v[o].f.f < k)
        k -= v[o].f.f, qq++;
      if (v[o].f.s < k)
        k -= v[o].f.s, qq++;
      if (v[o].f.f < k)
        k -= v[o].f.f, qq++;
      if (v[o].f.s < k)
        k -= v[o].f.s, qq++;
      if (qq % 2) {
        p1.f.s = k, p1.f.f = v[o].f.f;
        p2.f.s = v[o].f.s - k, p2.f.f = v[o].f.f;
        if (k > v[o].f.s)
          p1.s = v.size() + 1, p2.s = v.size();
        else
          p1.s = v.size(), p2.s = v.size() + 1;
        v.erase(v.begin() + o);
        vector<pair<int, int>> u;
        for (int j = 0; j < v.size(); j++)
          u.push_back(mk(v[j].s, j));
        sort(u.begin(), u.end());
        for (int j = 0; j < u.size(); j++)
          if (u[j].f != j + 1)
            v[u[j].s].s--;
        v.push_back(p1);
        v.push_back(p2);
      } else {
        p1.f.s = v[o].f.s, p1.f.f = k;
        p2.f.s = v[o].f.s, p2.f.f = v[o].f.f - k;
        if (k > v[o].f.f)
          p1.s = v.size() + 1, p2.s = v.size();
        else
          p1.s = v.size(), p2.s = v.size() + 1;
        v.erase(v.begin() + o);
        vector<pair<int, int>> u;
        for (int j = 0; j < v.size(); j++)
          u.push_back(mk(v[j].s, j));
        sort(u.begin(), u.end());
        for (int j = 0; j < u.size(); j++)
          if (u[j].f != j + 1)
            v[u[j].s].s--;
        v.push_back(p1);
        v.push_back(p2);
      }
    }
    vector<int> vv;
    for (int i = 0; i < v.size(); i++)
      vv.push_back(v[i].f.f * v[i].f.s);
    sort(vv.begin(), vv.end());
    for (int i = 0; i < vv.size(); i++) {
      if (i)
        cout << ' ';
      cout << vv[i];
    }
    cout << endl;
  }
}