Problem p00730 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>

using namespace std;

#define dump(n) cerr << "# " << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

struct Rect {
  int x, y, w, h;
  Rect() {}
  Rect(int x, int y, int w, int h) : x(x), y(y), w(w), h(h) {}
};

int main() {
  for (int n, w, h; cin >> n >> w >> h, n | w | h;) {
    list<Rect> rs;
    rs.push_back(Rect(0, 0, w, h));
    rep(i, n) {
      int p, s;
      cin >> p >> s;

      iter(rs) it = rs.begin();
      advance(it, p - 1);
      int len = (it->w + it->h) * 2;
      Rect r1, r2;
      if ((s %= len) < it->w) {
        r1 = Rect(it->x, it->y, s, it->h);
        r2 = Rect(it->x + s, it->y, it->w - s, it->h);
      } else if ((s -= it->w) < it->h) {
        r1 = Rect(it->x, it->y, it->w, s);
        r2 = Rect(it->x, it->y + s, it->w, it->h - s);
      } else if ((s -= it->h) < it->w) {
        r1 = Rect(it->x, it->y, it->h - s, it->h);
        r2 = Rect(it->x + it->w - s, it->y, s, it->h);
      } else if ((s -= it->w) < it->h) {
        r1 = Rect(it->x, it->y, it->w, it->h - s);
        r2 = Rect(it->x, it->y + it->h - s, it->w, s);
      }

      if (r1.w * r1.h > r2.w * r2.h)
        swap(r1, r2);
      rs.push_back(r1);
      rs.push_back(r2);
      rs.erase(it);
    }

    vi as;
    foreach (i, rs)
      as.push_back(i->w * i->h);
    sort(all(as));
    rep(i, as.size()) printf("%d%c", as[i], i == as.size() - 1 ? '\n' : ' ');
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>

using namespace std;

#define dump(n) cerr << "# " << #n << "=" << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

struct Rect {
  int x, y, w, h;
  Rect() {}
  Rect(int x, int y, int w, int h) : x(x), y(y), w(w), h(h) {}
};

int main() {
  for (int n, w, h; cin >> n >> w >> h, n | w | h;) {
    list<Rect> rs;
    rs.push_back(Rect(0, 0, w, h));
    rep(i, n) {
      int p, s;
      cin >> p >> s;

      iter(rs) it = rs.begin();
      advance(it, p - 1);
      int len = (it->w + it->h) * 2;
      Rect r1, r2;
      if ((s %= len) < it->w) {
        r1 = Rect(it->x, it->y, s, it->h);
        r2 = Rect(it->x + s, it->y, it->w - s, it->h);
      } else if ((s -= it->w) < it->h) {
        r1 = Rect(it->x, it->y, it->w, s);
        r2 = Rect(it->x, it->y + s, it->w, it->h - s);
      } else if ((s -= it->h) < it->w) {
        r1 = Rect(it->x, it->y, it->w - s, it->h);
        r2 = Rect(it->x + it->w - s, it->y, s, it->h);
      } else if ((s -= it->w) < it->h) {
        r1 = Rect(it->x, it->y, it->w, it->h - s);
        r2 = Rect(it->x, it->y + it->h - s, it->w, s);
      }

      if (r1.w * r1.h > r2.w * r2.h)
        swap(r1, r2);
      rs.push_back(r1);
      rs.push_back(r2);
      rs.erase(it);
    }

    vi as;
    foreach (i, rs)
      as.push_back(i->w * i->h);
    sort(all(as));
    rep(i, as.size()) printf("%d%c", as[i], i == as.size() - 1 ? '\n' : ' ');
  }

  return 0;
}