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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define INF 1 << 27
#define all(n) n.begin(), n.end()
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define F first
#define S second
#define pb push_back
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef long long ll;
typedef unsigned long long ull;

class Solve {
private:
  int n, w, d;
  vector<P> info;

public:
  Solve(int _n, int _w, int _d) : n(_n), w(_w), d(_d) { info.pb(P(w, d)); }
  ~Solve() { info.clear(); }

  int getN() { return n; }

  int getW() { return w; }

  int getD() { return d; }

  void printAll() {
    cout << "n = " << n << ",w = " << w << ",d = " << d << endl;
    cout << "info---" << endl;
    rep(i, info.size()) {
      cout << i << " - th : " << info[i].F << "," << info[i].S << endl;
    }
    cout << "end---" << endl;
  }

  void getAns() { sortVector(info); }

  void cutInfo(int p, int s) {
    P store = info[p];
    eraseMethod(p);
    int Mod = store.F * 2 + store.S * 2;
    s = s % Mod;

    P A, B;
    if (0 < s && s < store.F) {

      A = P(s, store.S);           // left
      B = P(store.F - s, store.S); // right

    } else if (store.F < s && s < store.F + store.S) {

      A = P(store.F, s - store.F);             // top
      B = P(store.F, (store.F + store.S) - s); // bottom

    } else if (store.F + store.S < s && s < store.F * 2 + store.S) {

      A = P(s - (store.F + store.S), store.S);
      B = P((store.F * 2 + store.S) - s, store.S);

    } else if (store.F * 2 + store.S < s && s < store.F * 2 + store.S * 2) {

      A = P(store.F, s - (store.F * 2 + store.S));
      B = P(store.F, (store.F * 2 + store.S * 2) - s);

    } else
      assert(false);

    if (A.F * A.S < B.F * B.S)
      info.pb(A), info.pb(B);
    else
      info.pb(B), info.pb(A);
  }

  void eraseMethod(int p) {
    vector<P>::iterator it = info.begin();
    for (int cnt = 0; cnt != p; it++, cnt++)
      if (cnt > info.size())
        assert(false);
    info.erase(it);
  }

  void sortVector(vector<P> vec) {
    vector<int> area;
    rep(i, vec.size()) area.pb(vec[i].F * vec[i].S);
    sort(all(area));
    printVector(area);
  }

  void printVector(vector<int> vec) {
    rep(i, vec.size()) {
      cout << vec[i];
      if (i != vec.size() - 1)
        cout << ' ';
    }
    cout << endl;
  }

  int getInfoSize() { return info.size(); }
};

int main() {

  while (true) {
    int _n, _w, _d;
    cin >> _n >> _w >> _d;
    // cerr << _n << " " << _w << " " << _d << endl;
    if (_n + _w + _d == 0)
      break;

    Solve ans(_n, _w, _d);

    rep(i, ans.getN()) {
      int p, s;
      cin >> p >> s;
      // cerr << p << " " << s << endl;
      ans.cutInfo(p - 1, s);
    }
    // cerr << endl;

    ans.getAns();
  }

  return 0;
}

Error: 0
input = 3 5 6
1 18
pre = 5 6
s =18, mod = 22
2 19
pre = 5 4
s =1, mod = 18
1 2
pre = 5 2
s =2, mod = 14
input = 3 4 1
1 1
pre = 4 1
s =1, mod = 10
2 1
pre = 3 1
s =1, mod = 8
3 1
pre = 2 1
s =1, mod = 6
input = 0 2 5
input = 0 0 0


Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define EPS 1e-10
#define F first
#define S second
#define all(n) n.begin(), n.end()
#define pb push_back
#define insert(a, b, c, d) PP(P(a, b), P(c, d))
#define INF (1 << 27)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef pair<int, P> IP;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int n, w, d;

bool cmp(const IP &p, const IP &q) {
  if (p.F != q.F)
    return p.F < q.F;
  return p.S.S < q.S.S;
}

void qrint(IP i) {
  cout << "i = " << i.F << " " << i.S.F << " " << i.S.S << endl;
}

int main() {

  while (true) {
    cin >> n >> w >> d;
    cerr << "input = " << n << " " << w << " " << d << endl;
    if (n + w + d == 0)
      break;
    // vector<IP> vec;
    // vec.pb(IP(w*d,P(w,d)));
    vector<P> vec;
    vec.pb(P(w, d));

    rep(i, n) {
      int p, s;
      cin >> p >> s;
      cerr << p << " " << s << endl;
      p--;
      // cout << "now! vec state is " << endl;
      // rep(i,vec.size()){
      //	cout << vec[i].F << " " << vec[i].S.F << " " << vec[i].S.S <<
      //endl;
      //}
      // cout<< "------" << endl;
      // IP pre = vec[p];
      P pre = vec[p];
      cerr << "pre = " << pre.F << " " << pre.S << endl;
      // vector<IP>::iterator it = vec.begin();
      vector<P>::iterator it = vec.begin();
      while (it != vec.end()) {
        if ((*it) == pre) {
          break;
        }
        it++;
      }
      vec.erase(it);

      // int fla = pre.S.F;
      int last = -1;
      // s = s%(pre.S.F*2+pre.S.S*2);
      s = s % (pre.F * 2 + pre.S * 2);
      cerr << "s =" << s << ", mod = " << pre.F * 2 + pre.S * 2 << endl;
      assert(s <= (pre.F * 2 + pre.S * 2));
      if (s <= pre.F) {
        last = 2;
      } else if (s <= pre.F + pre.S) {
        last = 1;
        s -= pre.F;
      } else if (s <= pre.F * 2 + pre.S) {
        last = 2;
        s = pre.F * 2 + pre.S - s;
      } else if (s <= pre.F * 2 + pre.S * 2) {
        last = 1;
        s = pre.F * 2 + pre.S * 2 - s;
      } else {
        cerr << "s = " << s << endl;
        assert(false);
      }
      assert(last != -1);

      if (last == 2) {
        int sum = s * (pre.S);
        // IP lef = IP(sum,P(s,pre.S.S));
        // IP rig = IP(pre.F-sum,P(pre.S.F-s,pre.S.S));
        P lef = P(s, pre.S);
        P rig = P(pre.F - s, pre.S);
        // cout << "lef " << endl;
        // qrint(lef);
        // cout << "rig " << endl;
        // qrint(rig);
        if (lef.F * lef.S < rig.F * rig.S) {
          vec.pb(lef);
          vec.pb(rig);
        } else {
          vec.pb(rig);
          vec.pb(lef);
        }
      } else if (last == 1) {
        // else if(s <= pre.S.F+pre.S.S){
        int sum = pre.F * (pre.S - s);
        // IP tp = IP(pre.F-sum,P(pre.S.F,s));
        // IP bm = IP(sum,P(pre.S.F,pre.S.S-s));
        P tp = P(pre.F, s);
        P bm = P(pre.F, pre.S - s);
        // cout << "tp " << endl;
        // qrint(tp);
        // cout << "bm " << endl;
        // qrint(bm);
        // cout <<"sum =" << sum << endl;
        if (tp.F * tp.S < bm.F * bm.S) {
          vec.pb(tp);
          vec.pb(bm);
        } else {
          vec.pb(bm);
          vec.pb(tp);
        }
      }
    }
    vector<int> vec2;
    rep(i, vec.size()) { vec2.pb(vec[i].F * vec[i].S); }
    sort(all(vec2));
    rep(i, vec2.size()) {
      cout << vec2[i];
      if (i != vec2.size() - 1)
        cout << " ";
      else
        cout << endl;
    }
  }
  return 0;
}