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


#include <iostream>

using namespace std;

int n, w, d, p[101], s[101], hnum = 0, buf;
typedef struct {
  int x;
  int y;
} Hako;
Hako hako[101], wake1, wake2;

int main() {

  cin >> n;
  cin >> w;
  cin >> d;

  while (n != 0 || w != 0 || d != 0) {
    for (int lop = 0; lop < 101; lop++) {
      p[lop] = 0;
      s[lop] = 0;
      hako[lop].x = 0;
      hako[lop].y = 0;
    }

    for (int lop = 0; lop < n; lop++) {
      cin >> p[lop];
      cin >> s[lop];
    }
    hnum = 0;
    hako[0].x = w;
    hako[0].y = d;

    for (int lop = 0; lop < n; lop++) {
      s[lop] %= (hako[p[lop] - 1].x + hako[p[lop] - 1].y) * 2;
      if (s[lop] - hako[p[lop] - 1].x < 0) {
        wake1.x = s[lop];
        wake1.y = hako[p[lop] - 1].y;
        wake2.x = hako[p[lop] - 1].x - s[lop];
        wake2.y = hako[p[lop] - 1].y;
      } else if (s[lop] - hako[p[lop] - 1].x - hako[p[lop] - 1].y < 0) {
        wake1.x = hako[p[lop] - 1].x;
        wake1.y = s[lop] - hako[p[lop] - 1].x;
        wake2.x = hako[p[lop] - 1].x;
        wake2.y = hako[p[lop] - 1].y - (s[lop] - hako[p[lop] - 1].x);
      } else if (s[lop] - hako[p[lop] - 1].x - hako[p[lop] - 1].x -
                     hako[p[lop] - 1].y <
                 0) {
        wake1.x = s[lop] - hako[p[lop] - 1].x - hako[p[lop] - 1].y;
        wake1.y = hako[p[lop] - 1].y;
        wake2.x = hako[p[lop] - 1].x -
                  (s[lop] - hako[p[lop] - 1].x - hako[p[lop] - 1].y);
        wake2.y = hako[p[lop] - 1].y;
      } else {
        wake1.x = hako[p[lop] - 1].x;
        wake1.y = s[lop] - (hako[p[lop] - 1].x * 2) - hako[p[lop] - 1].y;
        wake2.x = hako[p[lop] - 1].x;
        wake2.y = hako[p[lop] - 1].y -
                  (s[lop] - (hako[p[lop] - 1].x * 2) - hako[p[lop] - 1].y);
      }
      // cout<<p[lop]-1<<"aaa"<<hnum<<endl;
      for (int lop2 = p[lop] - 1; lop2 < hnum; lop2++) {
        hako[lop2].x = hako[lop2 + 1].x;
        hako[lop2].y = hako[lop2 + 1].y;
      }
      if (wake1.x * wake1.y < wake2.x * wake2.y) {
        hako[hnum].x = wake1.x;
        hako[hnum].y = wake1.y;
        hako[hnum + 1].x = wake2.x;
        hako[hnum + 1].y = wake2.y;
      } else {
        hako[hnum].x = wake2.x;
        hako[hnum].y = wake2.y;
        hako[hnum + 1].x = wake1.x;
        hako[hnum + 1].y = wake1.y;
      }
      hnum++;

      /*hako[p[lop]-1].x=wake1.x;
      hako[p[lop]-1].y=wake1.y;
      for(int lop2=0;lop2<hnum+1;lop2++){
              if(hako[lop2].x==0){
                      hako[lop2].x=wake2.x;
                      hako[lop2].y=wake2.y;
              }
      }*/
    }

    for (int lop = 0; lop < hnum + 1; lop++) {
      for (int lop2 = 0; lop2 < hnum; lop2++) {
        if (hako[lop2].x * hako[lop2].y > hako[lop2 + 1].x * hako[lop2 + 1].y) {

          buf = hako[lop2].x;
          hako[lop2].x = hako[lop2 + 1].x;
          hako[lop2 + 1].x = buf;

          buf = hako[lop2].y;
          hako[lop2].y = hako[lop2 + 1].y;
          hako[lop2 + 1].y = buf;
        }
      }
    }

    cout << hako[0].x * hako[0].y;
    for (int lop2 = 1; lop2 < hnum + 1; lop2++) {
      cout << ' ' << hako[lop2].x * hako[lop2].y;
    }
    cout << endl;

    cin >> n;
    cin >> w;
    cin >> d;
  }

  return 0;
}

Error: WA

Fixed submission:


#include <iostream>

using namespace std;

int n, w, d, p[101], s[101], hnum = 0, buf;
typedef struct {
  int x;
  int y;
} Hako;
Hako hako[101], wake1, wake2;

int main() {

  cin >> n;
  cin >> w;
  cin >> d;

  while (n != 0 || w != 0 || d != 0) {
    for (int lop = 0; lop < 101; lop++) {
      p[lop] = 0;
      s[lop] = 0;
      hako[lop].x = 0;
      hako[lop].y = 0;
    }

    for (int lop = 0; lop < n; lop++) {
      cin >> p[lop];
      cin >> s[lop];
    }
    hnum = 0;
    hako[0].x = w;
    hako[0].y = d;

    for (int lop = 0; lop < n; lop++) {
      s[lop] %= (hako[p[lop] - 1].x + hako[p[lop] - 1].y) * 2;
      if (s[lop] - hako[p[lop] - 1].x < 0) {
        wake1.x = s[lop];
        wake1.y = hako[p[lop] - 1].y;
        wake2.x = hako[p[lop] - 1].x - s[lop];
        wake2.y = hako[p[lop] - 1].y;
      } else if (s[lop] - hako[p[lop] - 1].x - hako[p[lop] - 1].y < 0) {
        wake1.x = hako[p[lop] - 1].x;
        wake1.y = s[lop] - hako[p[lop] - 1].x;
        wake2.x = hako[p[lop] - 1].x;
        wake2.y = hako[p[lop] - 1].y - (s[lop] - hako[p[lop] - 1].x);
      } else if (s[lop] - hako[p[lop] - 1].x - hako[p[lop] - 1].x -
                     hako[p[lop] - 1].y <
                 0) {
        wake1.x = s[lop] - hako[p[lop] - 1].x - hako[p[lop] - 1].y;
        wake1.y = hako[p[lop] - 1].y;
        wake2.x = hako[p[lop] - 1].x -
                  (s[lop] - hako[p[lop] - 1].x - hako[p[lop] - 1].y);
        wake2.y = hako[p[lop] - 1].y;
      } else {
        wake1.x = hako[p[lop] - 1].x;
        wake1.y = s[lop] - (hako[p[lop] - 1].x * 2) - hako[p[lop] - 1].y;
        wake2.x = hako[p[lop] - 1].x;
        wake2.y = hako[p[lop] - 1].y -
                  (s[lop] - (hako[p[lop] - 1].x * 2) - hako[p[lop] - 1].y);
      }
      hnum++;
      if (wake1.x * wake1.y < wake2.x * wake2.y) {
        hako[p[lop] - 1].x = wake1.x;
        hako[p[lop] - 1].y = wake1.y;
        hako[p[lop]].x = wake2.x;
        hako[p[lop]].y = wake2.y;
      } else {
        hako[p[lop] - 1].x = wake2.x;
        hako[p[lop] - 1].y = wake2.y;
        hako[p[lop]].x = wake1.x;
        hako[p[lop]].y = wake1.y;
      }

      /*hako[p[lop]-1].x=wake1.x;
      hako[p[lop]-1].y=wake1.y;
      for(int lop2=0;lop2<hnum+1;lop2++){
              if(hako[lop2].x==0){
                      hako[lop2].x=wake2.x;
                      hako[lop2].y=wake2.y;
              }
      }*/
    }

    for (int lop = 0; lop < hnum + 1; lop++) {
      for (int lop2 = 0; lop2 < hnum; lop2++) {
        if (hako[lop2].x * hako[lop2].y > hako[lop2 + 1].x * hako[lop2 + 1].y) {

          buf = hako[lop2].x;
          hako[lop2].x = hako[lop2 + 1].x;
          hako[lop2 + 1].x = buf;

          buf = hako[lop2].y;
          hako[lop2].y = hako[lop2 + 1].y;
          hako[lop2 + 1].y = buf;
        }
      }
    }

    for (int lop2 = 0; lop2 < hnum + 1; lop2++) {
      cout << hako[lop2].x * hako[lop2].y << ' ';
    }
    cout << endl;

    cin >> n;
    cin >> w;
    cin >> d;
  }

  return 0;
}