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

#include "bits/stdc++.h"
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h> //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1149&lang=jp
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-7
#define mp(a1, a2) make_pair(a1, a2)
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = 1e+18 + 1;
int INF = 1e+9 + 1;
int main() {
  while (1) {
    int n, w, d;
    scan(n >> w >> d);
    if (n + w + d == 0)
      break;
    vector<pair<pint, pint>> WD;
    WD.push_back({{-1, w * d}, {w, d}});
    rep(i, n) {
      int p, s;
      scan(p >> s);
      p--;
      sort(WD.begin(), WD.end());
      int yoko = WD[p].second.first, tate = WD[p].second.second;
      int nagasa = s % (2 * (yoko + tate));
      if ((nagasa < yoko) ^ (nagasa < yoko + tate) ^
          (nagasa < 2 * yoko + tate)) {
        int newyoko1 = nagasa % (yoko + tate);
        int newyoko2 = yoko - newyoko1;
        WD[p] = {{i, newyoko1 * tate}, {newyoko1, tate}};
        WD.push_back({{i, newyoko2 * tate}, {newyoko2, tate}});
      } else {
        int newtate1 = (nagasa - yoko) % (yoko + tate);
        int newtate2 = tate - newtate1;
        WD[p] = {{i, newtate1 * yoko}, {yoko, newtate1}};
        WD.push_back({{i, newtate2 * yoko}, {yoko, newtate2}});
      }
    }
    vint menseki;
    for (auto x : WD) {
      menseki.push_back(x.first.second);
    }
    sort(menseki.begin(), menseki.end());
    for (int i = 0; i < menseki.size(); i++) {
      if (i != 0)
        printf(" %d", menseki[i]);
      else
        printf("%d", menseki[i]);
    }
    kaigyo;
  }

  return 0;
}


Error: WA

Fixed submission:

#include "bits/stdc++.h"
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h> //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1149&lang=jp
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-7
#define mp(a1, a2) make_pair(a1, a2)
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = 1e+18 + 1;
int INF = 1e+9 + 1;
int main() {
  while (1) {
    int n, w, d;
    scan(n >> w >> d);
    if (n + w + d == 0)
      break;
    vpint WD;
    WD.push_back({w, d});
    rep(i, n) {
      int p, s;
      scan(p >> s);
      p--;
      int yoko = WD[p].first, tate = WD[p].second;
      int nagasa = s % (2 * (yoko + tate));
      if ((nagasa < yoko) ^ (nagasa < yoko + tate) ^
          (nagasa < 2 * yoko + tate)) {
        int newyoko = nagasa % (yoko + tate);
        // if(newyoko==0||newyoko==yoko) printf("error i:%d p:%d s:%d yoko:%d
        // tate:%d newyoko:%d\\n",i,p,s,yoko,tate,newyoko);
        WD[p].first = min(yoko - newyoko, newyoko);
        WD.push_back({max(yoko - newyoko, newyoko), tate});
      } else {
        int newtate = (nagasa - yoko) % (yoko + tate);
        // if(newtate==0||newtate==tate) printf("error i:%d p:%d s:%d yoko:%d
        // tate:%d newtate:%d\\n",i,p,s,yoko,tate,newtate);
        WD[p].second = min(tate - newtate, newtate);
        WD.push_back({yoko, max(tate - newtate, newtate)});
      }
    }
    vint menseki;
    for (auto x : WD) {
      menseki.push_back(x.first * x.second);
      // printf("w:%d d:%d\\n",x.first,x.second);
    }
    sort(menseki.begin(), menseki.end());
    for (auto x : menseki) {
      printf("%d ", x);
    }
    kaigyo;
  }

  return 0;
}