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

#include <bits/stdc++.h>
using namespace std;

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// container util
#define all(x) (x).begin(), (x).end()

// typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

void wcut(ll s, int idx, ll cw, ll cd, vector<pair<ll, PLL>> &arr) {
  cw -= s;
  arr.erase(arr.begin() + idx);
  if (cw * cd < s * cd) {
    arr.push_back(make_pair(cw * cd, make_pair(cw, cd)));
    arr.push_back(make_pair(s * cd, make_pair(s, cd)));
  } else {
    arr.push_back(make_pair(s * cd, make_pair(s, cd)));
    arr.push_back(make_pair(cw * cd, make_pair(cw, cd)));
  }
}

void dcut(ll s, int idx, ll cw, ll cd, vector<pair<ll, PLL>> &arr) {
  cd -= s;
  arr.erase(arr.begin() + idx);
  if (cw * cd < s * cw) {
    arr.push_back(make_pair(cw * cd, make_pair(cw, cd)));
    arr.push_back(make_pair(cw * s, make_pair(cw, s)));
  } else {
    arr.push_back(make_pair(cw * s, make_pair(cw, s)));
    arr.push_back(make_pair(cw * cd, make_pair(cw, cd)));
  }
}

int main() {
  int n, w, d;
  int idx, s;
  while (true) {
    cin >> n >> w >> d;
    vector<pair<ll, PLL>> arr;
    arr.push_back(make_pair(w * d, make_pair(w, d)));
    if (n == 0 && w == 0 && d == 0)
      break;
    rep(i, n) {
      cin >> idx >> s;
      idx--;
      ll cw = arr[idx].second.first;
      ll cd = arr[idx].second.second;

      s %= (2LL * (cw + cd));

      if (s < cw) { // wカット
        wcut(s, idx, cw, cd, arr);
      } else if (s < (cw + cd)) { // dカット
        s -= cw;
        dcut(s, idx, cw, cd, arr);
      } else if (s < (cw * 2LL + cd)) { // wカット
        s -= (cw + cd);
        wcut(s, idx, cw, cd, arr);
      } else { // dカット
        s -= (cw + cw + cd);
        dcut(s, idx, cw, cd, arr);
      }
      // rep(j,arr.size()){
      //   if(j != arr.size()-1) cout << arr[j].first << ' ';
      //   else cout << arr[j].first << endl;
      // }
    }

    sort(all(arr));
    rep(i, arr.size()) {
      if (i != arr.size() - 1)
        cout << arr[i].first << ' ';
      else
        cout << arr[i].first << endl;
    }
    arr.clear();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

// container util
#define all(x) (x).begin(), (x).end()

// typedef
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

// conversion
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

int main() {
  int n, w, d;
  int idx, s;
  while (true) {
    cin >> n >> w >> d;
    vector<pair<ll, PLL>> arr;
    arr.push_back(make_pair(w * d, make_pair(w, d)));
    if (n == 0 && w == 0 && d == 0)
      break;
    rep(i, n) {
      cin >> idx >> s;
      idx--;
      int cw = arr[idx].second.first;
      int cd = arr[idx].second.second;
      s %= 2 * (cw + cd);
      if (s < cw) { // wカット
        cw -= s;
        arr[idx] = make_pair(1e15, make_pair(cw, cd));
        sort(all(arr));
        if (cw * cd < s * cd) {
          arr[arr.size() - 1] = make_pair(cw * cd, make_pair(cw, cd));
          arr.push_back(make_pair(s * cd, make_pair(s, cd)));
        } else {
          arr[arr.size() - 1] = make_pair(s * cd, make_pair(s, cd));
          arr.push_back(make_pair(cw * cd, make_pair(cw, cd)));
        }
      } else if (s < cw + cd) { // dカット
        s -= cw;
        cd -= s;
        arr[idx] = make_pair(1e15, make_pair(cw, cd));
        sort(all(arr));
        if (cw * cd < s * cw) {
          arr[arr.size() - 1] = make_pair(cw * cd, make_pair(cw, cd));
          arr.push_back(make_pair(cw * s, make_pair(cw, s)));
        } else {
          arr[arr.size() - 1] = make_pair(cw * s, make_pair(cw, s));
          arr.push_back(make_pair(cw * cd, make_pair(cw, cd)));
        }
      } else if (s < (cw * 2 + cd)) { // wカット
        s -= cw + cd;
        cw -= s;
        arr[idx] = make_pair(1e15, make_pair(cw, cd));
        sort(all(arr));
        if (cw * cd < s * cd) {
          arr[arr.size() - 1] = make_pair(cw * cd, make_pair(cw, cd));
          arr.push_back(make_pair(s * cd, make_pair(s, cd)));
        } else {
          arr[arr.size() - 1] = make_pair(s * cd, make_pair(s, cd));
          arr.push_back(make_pair(cw * cd, make_pair(cw, cd)));
        }
      } else { // dカット
        s -= cw + cw + cd;
        cd -= s;
        arr[idx] = make_pair(1e15, make_pair(cw, cd));
        sort(all(arr));
        if (cw * cd < s * cw) {
          arr[arr.size() - 1] = make_pair(cw * cd, make_pair(cw, cd));
          arr.push_back(make_pair(cw * s, make_pair(cw, s)));
        } else {
          arr[arr.size() - 1] = make_pair(cw * s, make_pair(cw, s));
          arr.push_back(make_pair(cw * cd, make_pair(cw, cd)));
        }
      }
    }
    sort(all(arr));
    rep(i, arr.size()) {
      if (i != arr.size() - 1)
        cout << arr[i].first << ' ';
      else
        cout << arr[i].first << endl;
    }
  }
  return 0;
}