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

#include <bits/stdc++.h>

using namespace std;

#define box pair<pair<int, int>, pair<int, int>>

pair<int, int> split(pair<int, int> p, int length) {
  if (p.first * 2 + p.second * 2 == 0)
    return make_pair(0, 0);
  length %= p.first * 2 + p.second * 2;
  if (length < p.first) {
    // north
    return make_pair(length, 0);
  } else if (length < p.first + p.second) {
    // east
    return make_pair(0, length - p.first);
  } else if (length < p.first * 2 + p.second) {
    // south
    return make_pair(p.first - (length - p.first - p.second), 0);
  } else if (length < p.first * 2 + p.second * 2) {
    // west
    return make_pair(0, p.second - (length - p.first * 2 - p.second));
  }
}

int main() {
  int n, w, d;
  while (cin >> n >> w >> d, n != 0 || w != 0 || d != 0) {
    vector<pair<pair<int, int>, pair<int, int>>> v;
    v.emplace_back(make_pair(make_pair(0, 0), make_pair(w, d)));
    for (int i = 0; i < n; i++) {
      int p, s;
      cin >> p >> s;
      pair<int, int> q =
          split(make_pair(v[p - 1].second.first - v[p - 1].first.first,
                          v[p - 1].second.second - v[p - 1].first.second),
                s);
      int x1 = v[p - 1].first.first;
      int y1 = v[p - 1].first.second;
      int x2 = q.first + v[p - 1].first.first;
      int y2 = q.second + v[p - 1].first.second;
      int x3 = v[p - 1].second.first;
      int y3 = v[p - 1].second.second;
      for (int i = p - 1; i < v.size() - 1; i++) {
        v[i] = v[i + 1];
      }
      if (q.second == 0) {
        box a = make_pair(make_pair(x1, y1), make_pair(x2, y3));
        box b = make_pair(make_pair(x2, y1), make_pair(x3, y3));
        if (x2 - x1 < x3 - x2) {
          v[v.size() - 1] = a;
          v.emplace_back(b);
        } else {
          v[v.size() - 1] = b;
          v.emplace_back(a);
        }
      } else {
        box a = make_pair(make_pair(x1, y1), make_pair(x3, y2));
        box b = make_pair(make_pair(x1, y2), make_pair(x3, y3));
        if (y2 - y1 < y3 - y2) {
          v[v.size() - 1] = a;
          v.emplace_back(b);
        } else {
          v[v.size() - 1] = b;
          v.emplace_back(a);
        }
      }
    }
    vector<int> ans;
    for (int unsigned i = 0; i < v.size(); i++) {
      ans.emplace_back((v[i].second.first - v[i].first.first) *
                       (v[i].second.second - v[i].first.second));
    }
    sort(ans.begin(), ans.end());
    for (int unsigned i = 0; i < ans.size(); i++) {
      if (i != 0)
        cout << " ";
      cout << ans[i];
    }
    cout << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

pair<int, int> split(pair<int, int> p, int length) {
  length %= p.first * 2 + p.second * 2;
  if (length < p.first) {
    // north
    return make_pair(length, 0);
  } else if (length < p.first + p.second) {
    // east
    return make_pair(0, length - p.first);
  } else if (length < p.first * 2 + p.second) {
    // south
    return make_pair(p.first - (length - p.first - p.second), 0);
  } else if (length < p.first * 2 + p.second * 2) {
    // west
    return make_pair(0, p.second - (length - p.first * 2 - p.second));
  }
}

void show(vector<pair<pair<int, int>, pair<int, int>>> v) {
  cout << "v(" << v.size() << ")" << endl;
  for (auto x : v) {
    cout << "(" << x.first.first << "," << x.first.second << " "
         << x.second.first << "," << x.second.second << ") ";
  }
  cout << endl;
}

int main() {
  int n, w, d;
  while (cin >> n >> w >> d, n != 0 || w != 0 || d != 0) {
    vector<pair<pair<int, int>, pair<int, int>>> v;
    v.emplace_back(make_pair(make_pair(0, 0), make_pair(w, d)));
    // show(v);
    for (int i = 0; i < n; i++) {
      int p, s;
      cin >> p >> s;
      pair<int, int> q =
          split(make_pair(v[p - 1].second.first - v[p - 1].first.first,
                          v[p - 1].second.second - v[p - 1].first.second),
                s);
      int x = q.first + v[p - 1].first.first;
      int y = q.second + v[p - 1].first.second;
      // cout << q.first << " " << q.second << endl;
      if (q.second == 0) {
        if (x - v[p - 1].first.first < v[p - 1].second.first - x) {
          v.emplace_back(
              make_pair(make_pair(x, v[p - 1].first.second), v[p - 1].second));
          v[p - 1] =
              make_pair(v[p - 1].first, make_pair(x, v[p - 1].second.second));
        } else {
          v.emplace_back(
              make_pair(v[p - 1].first, make_pair(x, v[p - 1].second.second)));
          v[p - 1] =
              make_pair(make_pair(x, v[p - 1].first.second), v[p - 1].second);
        }
      } else {
        if (y - v[p - 1].first.second < v[p - 1].second.second - y) {
          v.emplace_back(
              make_pair(make_pair(v[p - 1].first.first, y), v[p - 1].second));
          v[p - 1] =
              make_pair(v[p - 1].first, make_pair(v[p - 1].second.first, y));
        } else {
          v.emplace_back(v[p - 1].first, make_pair(v[p - 1].second.first, y));
          v[p - 1] =
              make_pair(make_pair(v[p - 1].first.first, y), v[p - 1].second);
        }
      }
      // show(v);
    }
    vector<int> ans;
    for (int unsigned i = 0; i < v.size(); i++) {
      ans.emplace_back((v[i].second.first - v[i].first.first) *
                       (v[i].second.second - v[i].first.second));
    }
    sort(ans.begin(), ans.end());
    for (int unsigned i = 0; i < ans.size(); i++) {
      if (i != 0)
        cout << " ";
      cout << ans[i];
    }
    cout << endl;
  }

  return 0;
}