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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

struct Cake {
  int x;
  int y;
  int wi;
  int hi;
};
Cake cake[200], newcake[200];

int S[200];

int n, w, d, p[200], s[200];

void solve() {
  cake[0].x = cake[0].y = 0;
  cake[0].wi = w;
  cake[0].hi = d;

  for (int i = 0; i < n; i++) {
    int c = 0;
    for (int j = 0; j <= i; j++) {
      if (p[i] != j) {
        newcake[c++] = cake[j];
        continue;
      }
    }
    for (int j = 0; j <= i; j++) {
      if (p[i] != j)
        continue;
      Cake a, b;

      int start = s[i] % (2 * (cake[j].wi + cake[j].hi));
      if (start > cake[j].wi + cake[j].hi) {
        if (start < cake[j].wi * 2 + cake[j].hi) {
          start %= cake[j].wi + cake[j].hi;
          start = cake[j].wi - start;
        } else {
          start %= cake[j].wi + cake[j].hi;
          start -= cake[j].wi;
          start = cake[j].wi + cake[j].hi - start;
        }
      }

      a.x = cake[j].x;
      a.y = cake[j].y;
      if (start < cake[j].wi) {
        a.wi = start;
        a.hi = cake[j].hi;
        b.x = start + cake[j].x;
        b.y = cake[j].y;
        b.wi = cake[j].wi - start;
        b.hi = cake[j].hi;
      } else {

        a.hi = start - cake[j].wi;
        a.wi = cake[j].wi;
        b.y = start - cake[j].wi + cake[j].y;
        b.x = cake[j].x;
        b.hi = cake[j].hi - start + cake[j].wi;
        b.wi = cake[j].wi;
      }

      if (a.wi * a.hi > b.wi * b.hi) {
        Cake c = a;
        a = b;
        b = c;
      }

      newcake[c++] = a;
      newcake[c++] = b;
    }
    memcpy(cake, newcake, sizeof(cake));
  }
  REP(i, n + 1) { S[i] = cake[i].hi * cake[i].wi; }
  sort(S, S + n + 1);
  REP(i, n)
  printf("%d ", S[i]);
  printf("%d\\n", S[n]);
}

bool input() {
  scanf("%d%d%d", &n, &w, &d);
  if (n == 0 && w == 0 && d == 0)
    return false;

  REP(i, n) {
    scanf("%d%d", &p[i], &s[i]);
    p[i]--;
  }
  return true;
}

int main() {
  while (input()) {
    solve();
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)

struct Cake {
  int x;
  int y;
  int wi;
  int hi;
};
Cake cake[200], newcake[200];

int S[200];

int n, w, d, p[200], s[200];

void solve() {
  cake[0].x = cake[0].y = 0;
  cake[0].wi = w;
  cake[0].hi = d;

  for (int i = 0; i < n; i++) {
    int c = 0;
    for (int j = 0; j <= i; j++) {
      if (p[i] != j) {
        newcake[c++] = cake[j];
        continue;
      }
      Cake a, b;

      int start = s[i] % (2 * (cake[j].wi + cake[j].hi));
      if (start > cake[j].wi + cake[j].hi) {
        if (start < cake[j].wi * 2 + cake[j].hi) {
          start %= cake[j].wi + cake[j].hi;
          start = cake[j].wi - start;
        } else {
          start %= cake[j].wi + cake[j].hi;
          start -= cake[j].wi;
          start = cake[j].wi + cake[j].hi - start;
        }
      }

      a.x = cake[j].x;
      a.y = cake[j].y;
      if (start < cake[j].wi) {
        a.wi = start;
        a.hi = cake[j].hi;
        b.x = start + cake[j].x;
        b.y = cake[j].y;
        b.wi = cake[j].wi - start;
        b.hi = cake[j].hi;
      } else if (start < cake[j].wi + cake[j].hi) {

        a.hi = start - cake[j].wi;
        a.wi = cake[j].wi;
        b.y = start - cake[j].wi + cake[j].y;
        b.x = cake[j].x;
        b.hi = cake[j].hi - start + cake[j].wi;
        b.wi = cake[j].wi;
      }

      if (a.wi * a.hi > b.wi * b.hi) {
        Cake c = a;
        a = b;
        b = c;
      }

      newcake[c++] = a;
      newcake[c++] = b;
    }
    memcpy(cake, newcake, sizeof(cake));
  }
  REP(i, n + 1) { S[i] = cake[i].hi * cake[i].wi; }
  sort(S, S + n + 1);
  REP(i, n)
  printf("%d ", S[i]);
  printf("%d\\n", S[n]);
}

bool input() {
  scanf("%d%d%d", &n, &w, &d);
  if (n == 0 && w == 0 && d == 0)
    return false;

  REP(i, n) {
    scanf("%d%d", &p[i], &s[i]);
    p[i]--;
  }
  return true;
}

int main() {
  while (input()) {
    solve();
  }
}