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

#include <algorithm>
#include <cstdio>
#include <tuple>
#include <vector>

struct Node {
  int w, h;
  Node *next;
};

Node *head;

int main() {
  int N, W, D;

  while (scanf("%d %d %d", &N, &W, &D), N || W || D) {
    head = new Node{W, D, nullptr};

    for (int i = 0; i < N; i++) {
      int p, s;
      scanf("%d %d", &p, &s);

      Node *prev = head, *target = head;
      for (int j = 1; j < p; j++) {
        prev = target;
        target = target->next;
      }

      int w = target->w, h = target->h;
      int w1, h1, w2, h2;

      s %= 2 * (w + h);

      if (0 <= s && s <= w) {
        w1 = s;
        w2 = w - w1;
        h1 = h;
        h2 = h;
      } else if (w <= s && s <= w + h) {
        w1 = w;
        w2 = w;
        h1 = s - w;
        h2 = h - h1;
      } else if (w + h <= s && s <= 2 * w + h) {
        w1 = s - (w + h);
        w2 = w - w1;
        h1 = h;
        h2 = h;
      } else {
        w1 = w;
        w2 = w;
        h1 = 2 * (w + h) - s;
        h2 = h - h1;
      }

      if (w1 * h1 > w2 * h2) {
        std::swap(w1, w2);
        std::swap(h1, h2);
      }

      if (p == 1) {
        head = head->next;
      } else {
        prev->next = target->next;
        delete target;
      }

      if (i == 0) {
        head = new Node{w1, h1, new Node{w2, h2, nullptr}};
      } else {
        target = head;
        while (target->next != nullptr) {
          target = target->next;
        }
        target->next = new Node{w1, h1, new Node{w2, h2, nullptr}};
      }
    }

    std::vector<int> v;
    while (head != nullptr) {
      v.push_back(head->w * head->h);

      Node *next = head->next;
      delete head;
      head = next;
    }
    std::sort(v.begin(), v.end());

    for (int i = 0; i < N + 1; i++) {
      printf("%d%c", v[i], " \\n"[i + 1 == N + 1]);
    }
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <tuple>
#include <vector>

struct Node {
  int w, h;
  Node *next;
};

Node *head;

int main() {
  int N, W, D;

  while (scanf("%d %d %d", &N, &W, &D), N || W || D) {
    head = new Node{W, D, nullptr};

    for (int i = 0; i < N; i++) {
      int p, s;
      scanf("%d %d", &p, &s);

      Node *target = head;
      for (int j = 1; j < p; j++) {
        target = target->next;
      }

      int w = target->w, h = target->h;
      int w1, h1, w2, h2;

      s %= 2 * (w + h);

      if (0 <= s && s <= w) {
        w1 = s;
        w2 = w - s;
        h1 = h;
        h2 = h;
      } else if (w <= s && s <= w + h) {
        w1 = w;
        w2 = w;
        h1 = s - w;
        h2 = h - (s - w);
      } else if (w + h <= s && s <= 2 * w + h) {
        w1 = s - (w + h);
        w2 = w - (s - (w + h));
        h1 = h;
        h2 = h;
      } else {
        w1 = w;
        w2 = w;
        h1 = 2 * (w + h) - s;
        h2 = h - (2 * (w + h) - s);
      }

      if (w1 * h1 > w2 * h2) {
        std::swap(w1, w2);
        std::swap(h1, h2);
      }
      if (w1 <= 0 || h1 <= 0 || w2 <= 0 || h2 <= 0) {
        exit(-1);
      }

      target->w = 100; // w1;
      target->h = 100; // h1;

      target->next = new Node{100, 100, target->next}; // w2, h2, target->next};
    }

    std::vector<int> v;
    while (head != nullptr) {
      v.push_back(head->w * head->h);

      Node *next = head->next;
      delete head;
      head = next;
    }
    std::sort(v.begin(), v.end());

    for (int i = 0; i < N + 1; i++) {
      printf("%d%c", v[i], " \\n"[i + 1 == N + 1]);
    }
  }
}