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

#define _USE_MATH_DEFINES
#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()

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// typedef
typedef long long lint;
typedef unsigned long long ull;
typedef complex<long double> Complex;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;

// constant
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};

// 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();
}

//
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//

struct cake {
  int width;
  int height;
  int m;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, w, d;
  while (true) {
    cin >> n >> w >> d;
    if (n == 0 && w == 0 && d == 0)
      break;
    map<int, cake> mp;
    mp[1].width = w;
    mp[1].height = d;
    mp[1].m = w * d;
    rep(i, n) {
      int p, s;
      cin >> p >> s;
      cake tmp = mp[p];
      s %= 2 * (tmp.width + tmp.height);

      if (s - tmp.width < 0) {
        tmp.width -= s;
        tmp.m = tmp.height * tmp.width;
        cake c;
        c.width = s;
        c.height = tmp.height;
        c.m = s * tmp.height;
        if (tmp.width < s) {
          mp[i + 2] = c;
          mp[p] = tmp;
        } else {
          mp[p] = c;
          mp[i + 2] = tmp;
        }

      } else if (s - tmp.width - tmp.height < 0) {
        s -= tmp.width;
        tmp.height -= s;
        tmp.m = tmp.height * tmp.width;
        cake c;
        c.width = tmp.width;
        c.height = s;
        c.m = tmp.width * s;
        if (tmp.height < s) {
          mp[i + 2] = c;
          mp[p] = tmp;
        } else {
          mp[p] = c;
          mp[i + 2] = tmp;
        }
      } else if (s - 2 * tmp.width - tmp.height < 0) {
        s -= (tmp.width + tmp.height);
        tmp.width -= s;
        tmp.m = tmp.height * tmp.width;
        cake c;
        c.width = s;
        c.height = tmp.height;
        c.m = s * tmp.height;
        if (tmp.width < s) {
          mp[i + 2] = c;
          mp[p] = tmp;
        } else {
          mp[p] = c;
          mp[i + 2] = tmp;
        }
      } else {
        s -= (2 * tmp.width + tmp.height);
        tmp.height -= s;
        tmp.m = tmp.height * tmp.width;
        cake c;
        c.width = tmp.width;
        c.height = s;
        c.m = tmp.width * s;
        if (tmp.height < s) {
          mp[i + 2] = c;
          mp[p] = tmp;
        } else {
          mp[p] = c;
          mp[i + 2] = tmp;
        }
      }

      /*番号の付け替え*/
      if (p != i + 1) {
        cake a, b;
        a = mp[p];
        b = mp[i + 2];
        FOR(j, p, i + 1) { mp[j] = mp[j + 1]; }
        mp[i + 1] = a;
        mp[i + 2] = b;
      }
    }

    vector<int> ans;
    FOR(i, 1, n + 2) { ans.push_back(mp[i].m); }
    sort(all(ans));
    rep(i, ans.size()) { cout << ans[i] << (i + 1 == ans.size() ? "" : " "); }
    cout << "\\n";
  }

  return 0;
}


Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#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()

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

// typedef
typedef long long lint;
typedef unsigned long long ull;
typedef complex<long double> Complex;
typedef pair<int, int> P;
typedef tuple<int, int, int> TP;
typedef vector<int> vec;
typedef vector<vec> mat;

// constant
const int MOD = (int)1e9 + 7;
const int INF = (int)1e18;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const int ddx[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[] = {1, 1, 0, -1, -1, -1, 0, 1};

// 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();
}

//
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//

struct cake {
  int width;
  int height;
  int m;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, w, d;
  while (true) {
    cin >> n >> w >> d;
    if (n == 0 && w == 0 && d == 0)
      break;
    map<int, cake> mp;
    mp[1].width = w;
    mp[1].height = d;
    mp[1].m = w * d;
    rep(i, n) {
      int p, s;
      cin >> p >> s;
      cake tmp = mp[p];
      s %= 2 * (tmp.width + tmp.height);

      if (s - tmp.width <= 0) {
        tmp.width -= s;
        tmp.m = tmp.height * tmp.width;
        cake c;
        c.width = s;
        c.height = tmp.height;
        c.m = s * tmp.height;
        if (tmp.width < s) {
          mp[i + 2] = c;
          mp[p] = tmp;
        } else {
          mp[p] = c;
          mp[i + 2] = tmp;
        }

      } else if (s - tmp.width - tmp.height <= 0) {
        s -= w;
        tmp.height -= s;
        tmp.m = tmp.height * tmp.width;
        cake c;
        c.width = tmp.width;
        c.height = s;
        c.m = tmp.width * s;
        if (tmp.height < s) {
          mp[i + 2] = c;
          mp[p] = tmp;
        } else {
          mp[p] = c;
          mp[i + 2] = tmp;
        }
      } else if (s - 2 * tmp.width - tmp.height <= 0) {
        s -= w + d;
        tmp.width -= s;
        tmp.m = tmp.height * tmp.width;
        cake c;
        c.width = s;
        c.height = tmp.height;
        c.m = s * tmp.height;
        if (tmp.width < s) {
          mp[i + 2] = c;
          mp[p] = tmp;
        } else {
          mp[p] = c;
          mp[i + 2] = tmp;
        }
      } else {
        s -= 2 * w + d;
        tmp.height -= s;
        tmp.m = tmp.height * tmp.width;
        cake c;
        c.width = tmp.width;
        c.height = s;
        c.m = tmp.width * s;
        if (tmp.height < s) {
          mp[i + 2] = c;
          mp[p] = tmp;
        } else {
          mp[p] = c;
          mp[i + 2] = tmp;
        }
      }
    }

    vector<int> ans;
    FOR(i, 1, n + 2) { ans.push_back(mp[i].m); }
    sort(all(ans));
    rep(i, n + 1) { cout << ans[i] << (i == n ? "" : " "); }
    cout << "\\n";
  }

  return 0;
}