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

#include <algorithm>
#include <cassert>
#include <deque>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>

struct Piece {
  int width;
  int height;

  Piece(int width, int height) : width(width), height(height) {
    assert(width > 0 && height > 0);
  }

  int area() { return width * height; }
};

int main() {
  std::deque<Piece> pieces;

  while (true) {
    int num_cuts, width, height;
    std::cin >> num_cuts >> width >> height;

    if (width == 0)
      return 0;

    pieces.clear();
    pieces.push_back(Piece(width, height));

    for (int i = 0; i < num_cuts; i++) {
      int piece_num, cut_pos;
      std::cin >> piece_num >> cut_pos;

      auto pp = pieces.begin() + (piece_num - 1);
      Piece piece = *pp;
      pieces.erase(pp);

      // 1??±??????????????£???????????????????????§
      cut_pos %= (piece.width + piece.height) * 2;

      if (cut_pos > piece.width + piece.height &&
          cut_pos <= piece.width * 2 + piece.height) {
        // ?????¨??\\??????????????£???
        cut_pos = piece.width * 2 + piece.height - cut_pos;
      } else if (cut_pos > piece.width * 2 + piece.height) {
        cut_pos = piece.width * 3 + piece.height * 2 - cut_pos;
      }

      if (cut_pos <= piece.width) {
        // ????????????
        Piece p1(cut_pos, piece.height);
        Piece p2(piece.width - cut_pos, piece.height);

        if (cut_pos * 2 >= piece.width) {
          // ?????????????????§?????????
          pieces.push_back(p2);
          pieces.push_back(p1);
        } else {
          pieces.push_back(p1);
          pieces.push_back(p2);
        }
      } else {
        // ?¨??????????
        cut_pos -= piece.width;
        Piece p1(piece.width, cut_pos);
        Piece p2(piece.width, piece.height - cut_pos);

        if (cut_pos * 2 >= piece.height) {
          // ??????????????§??????
          pieces.push_back(p2);
          pieces.push_back(p1);
        } else {
          pieces.push_back(p1);
          pieces.push_back(p2);
        }
      }
    }

    std::sort(pieces.begin(), pieces.end(),
              [](Piece &x, Piece &y) { return x.area() < y.area(); });

    bool is_first = true;
    for (Piece &p : pieces) {
      if (is_first) {
        is_first = false;
      } else {
        std::cout << " ";
      }

      std::cout << p.area();
    }
    std::cout << std::endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>

struct Piece {
  int width;
  int height;

  int area() { return width * height; }
};

int main() {
  std::deque<Piece> pieces;

  while (true) {
    int num_cuts, width, height;
    std::cin >> num_cuts >> width >> height;

    if (width == 0)
      return 0;

    pieces.clear();
    pieces.push_back({width, height});

    for (int i = 0; i < num_cuts; i++) {
      int piece_num, cut_pos;
      std::cin >> piece_num >> cut_pos;

      auto pp = pieces.begin() + (piece_num - 1);
      Piece piece = *pp;
      pieces.erase(pp);

      // 1??±??????????????£???????????????????????§
      cut_pos %= (piece.width + piece.height) * 2;

      if (cut_pos > piece.width + piece.height &&
          cut_pos <= piece.width * 2 + piece.height) {
        // ?????¨??\\??????????????£???
        cut_pos = width * 2 + height - cut_pos;
      } else if (cut_pos > piece.width * 2 + piece.height) {
        cut_pos = width * 3 + height * 2 - cut_pos;
      }

      if (cut_pos <= piece.width) {
        // ????????????
        Piece p1 = {cut_pos, piece.height};
        Piece p2 = {piece.width - cut_pos, piece.height};

        if (cut_pos * 2 >= piece.width) {
          // ?????????????????§?????????
          pieces.push_back(p2);
          pieces.push_back(p1);
        } else {
          pieces.push_back(p1);
          pieces.push_back(p2);
        }
      } else {
        // ?¨??????????
        cut_pos -= piece.width;
        Piece p1 = {piece.width, cut_pos};
        Piece p2 = {piece.width, piece.height - cut_pos};

        if (cut_pos * 2 >= piece.height) {
          // ??????????????§??????
          pieces.push_back(p2);
          pieces.push_back(p1);
        } else {
          pieces.push_back(p1);
          pieces.push_back(p2);
        }
      }
    }

    std::sort(pieces.begin(), pieces.end(),
              [](Piece &x, Piece &y) { return x.area() < y.area(); });

    bool is_first = true;
    for (Piece &p : pieces) {
      if (is_first) {
        is_first = false;
      } else {
        std::cout << " ";
      }

      std::cout << p.area();
    }
    std::cout << std::endl;
  }
}