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

#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
struct piece {
  int d, w;
};

int area(struct piece p) { return p.d * p.w; }

int main(void) {
  for (int N, W, D; cin >> N >> W >> D, N || W || D;) {
    int P[N], S[N];
    REP(i, 0, N) cin >> P[i] >> S[i], P[i]--;

    struct piece first = {D, W};
    vector<struct piece> pieces;
    pieces.push_back(first);

    REP(i, 0, N) {
      int p = P[i], s = S[i];
      struct piece target = pieces[p];
      REP(i, p, pieces.size() - 1) pieces[i] = pieces[i + 1];
      int c = S[i] % (target.d * 2 + target.w * 2);
      int d1, w1, d2, w2;
      if (0 < c && c < target.w) {
        int v = c;
        d1 = target.d;
        w1 = v;
        d2 = target.d;
        w2 = target.w - v;
      } else if (target.w + target.d < c && c < target.w * 2 + target.d) {
        int v = c - (target.w + target.d);
        d1 = target.d;
        w1 = v;
        d2 = target.d;
        w2 = target.w - v;
      } else if (target.w < c && c < target.w + target.d) {
        int v = c - target.w;
        d1 = v;
        w1 = target.w;
        d2 = target.d - v;
        w2 = target.w;
      } else if (target.w * 2 + target.d < c &&
                 c < target.w * 2 + target.d * 2) {
        int v = c - (target.w * 2 + target.d);
        d1 = v;
        w1 = target.w;
        d2 = target.d - v;
        w2 = target.w;
      }
      struct piece new1 = {d1, w1};
      struct piece new2 = {d2, w2};
      if (area(new1) > area(new2))
        swap(new1, new2);
      pieces[pieces.size() - 1] = new1;
      pieces.push_back(new2);
    }

    vector<int> areas;
    REP(i, 0, pieces.size()) areas.push_back(area(pieces[i]));
    sort(areas.begin(), areas.end());

    REP(i, 0, areas.size())
        cout << areas[i] << (i + 1 != areas.size() ? " " : "");
    cout << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, a, n) for (int i = (a); i < (n); i++)
using namespace std;
struct piece {
  int d, w;
};

int area(struct piece p) { return p.d * p.w; }

int main(void) {
  for (int N, W, D; cin >> N >> W >> D, N || W || D;) {
    int P[N], S[N];
    REP(i, 0, N) cin >> P[i] >> S[i], P[i]--;

    struct piece first = {D, W};
    vector<struct piece> pieces;
    pieces.push_back(first);

    REP(i, 0, N) {
      int p = P[i], s = S[i];
      struct piece target = pieces[p];
      REP(i, p, pieces.size() - 1) pieces[i] = pieces[i + 1];
      int c = S[i] % (target.d * 2 + target.w * 2);
      int d1, w1, d2, w2;
      if (0 < c && c < target.w) {
        int v = c;
        d1 = target.d;
        w1 = v;
        d2 = target.d;
        w2 = target.w - v;
      } else if (target.w + target.d < c && c < target.w * 2 + target.d) {
        int v = c - (target.w + target.d);
        d1 = target.d;
        w1 = v;
        d2 = target.d;
        w2 = target.w - v;
      } else if (target.d < c && c < target.w + target.d) {
        int v = c - target.d;
        d1 = v;
        w1 = target.w;
        d2 = target.d - v;
        w2 = target.w;
      } else if (target.w * 2 + target.d < c &&
                 c < target.w * 2 + target.d * 2) {
        int v = c - (target.w * 2 + target.d);
        d1 = v;
        w1 = target.w;
        d2 = target.d - v;
        w2 = target.w;
      }
      struct piece new1 = {d1, w1};
      struct piece new2 = {d2, w2};
      if (area(new1) > area(new2))
        swap(new1, new2);
      pieces[pieces.size() - 1] = new1;
      pieces.push_back(new2);

      cout << "  " << pieces.size() << endl;
    }

    vector<int> areas;
    REP(i, 0, pieces.size()) areas.push_back(area(pieces[i]));
    sort(areas.begin(), areas.end());

    REP(i, 0, areas.size()) cout << areas[i] << " ";
    cout << endl;
  }

  return 0;
}