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

#include <algorithm>
#include <iostream>
#include <vector>
#define f first
#define s second
using namespace std;

int main() {

  while (1) {
    int n, w, d;
    cin >> n >> w >> d;
    if (n == 0 && w == 0 && d == 0)
      break;

    vector<pair<int, int>> id;
    id.clear();

    pair<int, int> t;

    t.f = w;
    t.s = d;
    id.push_back(t);

    for (int i = 0; i < n; i++) {
      int p, s;
      cin >> p >> s;
      p--;

      while (1) {
        if (s > 2 * id[p].f + 2 * id[p].s)
          s -= 2 * id[p].f + 2 * id[p].s;
        else
          break;
      }
      int flg = 0;
      /*while(1) {
        if(flg == 0 && s > id[p].f) s-= id[p].f,flg = 1;
        else if(flg == 0)break;
        if(flg == 1 && s > id[p].s) s-= id[p].s,flg = 0;
        else if(flg == 1)break;
        } */

      int s1, s2;

      // if(flg == 0) {
      if (s < id[p].f || (s > id[p].f + id[p].s && s < 2 * id[p].f + id[p].s)) {
        if (s > id[p].f)
          s -= (id[p].f + id[p].s);
        s1 = s * id[p].s;
        s2 = (id[p].f - s) * id[p].s;

        t.f = id[p].f - s;
        t.s = id[p].s;
        id[p].f = s;
        id.push_back(t);
        // cout << "tate"<<endl;
        // cout <<"s="<<s<<endl;
        // cout << " s1="<<s1<<" s2="<<s2<<endl;

      } else {

        s -= id[p].f;
        if (s > id[p].s)
          s -= (id[p].f + id[p].s);

        s1 = s * id[p].f;
        s2 = (id[p].s - s) * id[p].f;

        // cout << "yoko"<<endl;
        // cout <<"s="<<s<<endl;
        // cout << " s1="<<s1<<" s2="<<s2<<endl;

        t.s = id[p].s - s;
        t.f = id[p].f;
        id[p].s = s;
        id.push_back(t);
      }
      id.push_back(id[p]);
      if (id[id.size() - 1].f * id[id.size() - 1].s <
          id[id.size() - 2].f * id[id.size() - 2].s)
        swap(id[id.size() - 1], id[id.size() - 2]);
      id.erase(id.begin() + p);

      // for(int i=0;i<id.size();i++) cout << id[i].f*id[i].s <<" ";
      // cout << endl;
    }

    for (int i = 0; i < id.size(); i++)
      for (int j = id.size() - 1; j > i; j--)
        if (id[j].f * id[j].s < id[j - 1].f * id[j - 1].s)
          swap(id[j], id[j - 1]);

    for (int i = 0; i < id.size(); i++) {
      cout << id[i].f * id[i].s;
      if (i != id.size() - 1)
        cout << " ";
    }
    cout << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define f first
#define s second
using namespace std;
int mp[101][101];

int main() {

  while (1) {
    int n, w, d;
    cin >> n >> w >> d;
    if (n == 0 && w == 0 && d == 0)
      break;

    vector<pair<int, int>> id;

    pair<int, int> t;

    t.f = w;
    t.s = d;
    id.push_back(t);

    for (int i = 0; i < n; i++) {
      int p, s;
      cin >> p >> s;
      p--;
      s %= (2 * id[p].f + 2 * id[p].s);
      int s1, s2;
      int tw, td;

      if (s < id[p].f || (s > id[p].f + id[p].s && s < 2 * id[p].f + id[p].s)) {
        s %= (id[p].f + id[p].s);
        s1 = s * id[p].s;
        s2 = (id[p].f - s) * id[p].s;
        t.f = id[p].f - s;
        t.s = id[p].s;
        id[p].f = s;

        id.push_back(t);
        //   cout << "tate"<<endl;
        // cout <<"s="<<s<<endl;
        // cout << " s1="<<s1<<" s2="<<s2<<endl;

        if (s2 < s1)
          swap(id[p], id[id.size() - 1]);

      } else {

        s -= id[p].f;
        s %= (id[p].f + id[p].s);
        s1 = s * id[p].f;
        s2 = (id[p].s - s) * id[p].f;
        // cout << "yoko"<<endl;
        // cout <<"s="<<s<<endl;
        // cout << " s1="<<s1<<" s2="<<s2<<endl;

        t.s = id[p].s - s;
        t.f = id[p].f;
        id[p].s = s;
        id.push_back(t);
        if (s2 < s1)
          swap(id[p], id[id.size() - 1]);
      }

      // for(int i=0;i<id.size();i++) cout << id[i].f*id[i].s <<" ";
      // cout << endl;
    }

    for (int i = 0; i < id.size(); i++)
      for (int j = id.size() - 1; j > i; j--)
        if (id[j].f * id[j].s < id[j - 1].f * id[j - 1].s)
          swap(id[j], id[j - 1]);

    for (int i = 0; i < id.size(); i++) {
      cout << id[i].f * id[i].s;
      if (i != id.size() - 1)
        cout << " ";
    }
    cout << endl;
  }

  return 0;
}