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

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {

  while (1) {
    int n, w, d;
    int posfx, posfy, poslx, posly;
    int up, right;
    int cutx, cuty;
    int flag;
    int now;

    cin >> n >> w >> d;
    if (w == 0) {
      break;
    }
    vector<int> p(n + 1), s(n + 1);
    vector<vector<int>> dp(2 * d + 1, vector<int>(2 * w + 1, 0));
    vector<int> count(n + 1, 0);

    FOR(i, 1, n) { cin >> p[i] >> s[i]; }

    for (int i = 1; i <= 2 * d; i += 2) {
      for (int j = 1; j <= 2 * w; j += 2) {
        dp[i][j] = 1;
      }
    }
    FOR(i, 0, 2 * w) {
      dp[0][i] = 1;
      dp[2 * d][i] = 1;
    }
    FOR(i, 0, 2 * d) {
      dp[i][0] = 1;
      dp[i][2 * w] = 1;
    }

    FOR(i, 1, n) {
      posfx = -1;
      posfy = -1;
      poslx = -1;
      posly = -1;
      flag = -1;
      now = -1;

      // 周回する辺の確定
      for (int j = 1; j <= 2 * w - 1; j += 2) { // 左上
        for (int k = 1; k <= 2 * d - 1; k += 2) {
          if (dp[k][j] == p[i]) {
            posfx = j - 1;
            posfy = k - 1;
            break;
          }
        }
        if (posfx != -1) {
          break;
        }
      }

      for (int j = 2 * w - 1; j >= 1; j -= 2) { // 右下
        for (int k = 2 * d - 1; k >= 1; k -= 2) {
          if (dp[k][j] == p[i]) {
            poslx = j + 1;
            posly = k + 1;
            break;
          }
        }
        if (poslx != -1) {
          break;
        }
      }

      // cout << "a" << endl;
      // 幅
      up = poslx - posfx;
      right = posly - posfy;
      // cout << i << endl;
      // cout << posfx << " " << poslx << endl;
      // cout << posfy << " " << posly << endl;

      s[i] *= 2;

      // どの部分から切り込むかの判定
      if ((s[i] / (up + right)) % 2 == 0) { // 左上から
        s[i] = s[i] % (up + right);
        if (s[i] <= up - 1) { // 上辺
          cuty = posfy;
          cutx = posfx + s[i];
          flag = 1;
        } else { // 右辺
          // cout << s[i] << endl;
          cutx = poslx;
          cuty = posfy + (s[i] - up);
          flag = 2;
        }

      } else { // 右上から
        s[i] = s[i] % (up + right);
        if (s[i] <= up - 1) { // 下辺
          cuty = posly;
          cutx = poslx - (s[i]);
          flag = 3;
        } else { // 左辺
          // cout << s[i] << " " << up << endl;
          cutx = posfx;
          cuty = posly - (s[i] - up);
          flag = 4;
        }
      }

      // cout << "b" << endl;
      // cout << flag << " " << cutx << " " << cuty << endl;
      // cout << i << endl;

      /*if (i == 19) {
              cout << posfx << " " << poslx << endl;
              cout << posfy << " " << posly << endl;
              cout << s[i] << " " << up << " " << right << endl;
              cout << flag << " " << cutx << " " << cuty << endl;
      }*/

      // 切り込みを入れる 面の番号を変える
      switch (flag) {
      case 1: // 上辺
        FOR(j, 1, INF) {
          if (dp[cuty + j][cutx] == 0) {
            dp[cuty + j][cutx] = 1;
          } else {
            if (cutx - posfx > poslx - cutx) {
              for (int k = posfx + 1; k <= cutx - 1; k += 2) {
                for (int l = posfy + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
              if (dp[posfy + 1][cutx + 1] != i) {
                now = dp[posfy + 1][cutx + 1];
              }
              for (int k = cutx + 1; k <= poslx - 1; k += 2) {
                for (int l = posfy + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i;
                }
              }
            } else {
              for (int k = cutx + 1; k <= poslx - 1; k += 2) {
                for (int l = posfy + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
              if (dp[posfy + 1][cutx - 1] != i) {
                now = dp[posfy + 1][cutx - 1];
              }
              for (int k = posfx + 1; k <= cutx - 1; k += 2) {
                for (int l = posfy + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i;
                }
              }
            }
            break;
          }
        }
        break;
      case 2: // 右辺
        FOR(j, 1, INF) {
          if (dp[cuty][cutx - j] == 0) {
            dp[cuty][cutx - j] = 1;
          } else {
            if (cuty - posfy > posly - cuty) {
              for (int k = posfx + 1; k <= poslx - 1; k += 2) {
                for (int l = posfy + 1; l <= cuty - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
              if (dp[cuty + 1][posfx + 1] != i) {
                now = dp[cuty + 1][posfx + 1];
              }
              for (int k = posfx + 1; k <= poslx - 1; k += 2) {
                for (int l = cuty + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i;
                }
              }
            } else {
              for (int k = posfx + 1; k <= poslx - 1; k += 2) {
                for (int l = cuty + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
              if (dp[cuty - 1][posfx + 1] != i) {
                now = dp[cuty - 1][posfx + 1];
              }
              for (int k = posfx + 1; k <= poslx - 1; k += 2) {
                for (int l = posfy + 1; l <= cuty - 1; l += 2) {
                  dp[l][k] = i;
                }
              }
            }
            break;
          }
        }
        break;
      case 3: // 下辺
        FOR(j, 1, INF) {
          if (dp[cuty - j][cutx] == 0) {
            dp[cuty - j][cutx] = 1;
          } else {
            if (cutx - posfx > poslx - cutx) {
              for (int k = posfx + 1; k <= cutx - 1; k += 2) {
                for (int l = posfy + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
              if (dp[posfy + 1][cutx + 1] != i) {
                now = dp[posfy + 1][cutx + 1];
              }
              for (int k = cutx + 1; k <= poslx - 1; k += 2) {
                for (int l = posfy + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i;
                }
              }
            } else {
              for (int k = cutx + 1; k <= poslx - 1; k += 2) {
                for (int l = posfy + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
              if (dp[posfy + 1][cutx - 1] != i) {
                now = dp[posfy + 1][cutx - 1];
              }
              for (int k = posfx + 1; k <= cutx - 1; k += 2) {
                for (int l = posfy + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i;
                }
              }
            }
            break;
          }
        }
        break;
      case 4: // 左辺
        FOR(j, 1, INF) {
          if (dp[cuty][cutx + j] == 0) {
            dp[cuty][cutx + j] = 1;
          } else {
            // cout << j << endl;
            if (cuty - posfy > posly - cuty) {
              for (int k = posfx + 1; k <= poslx - 1; k += 2) {
                for (int l = posfy + 1; l <= cuty - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
              if (dp[cuty + 1][posfx + 1] != i) {
                now = dp[cuty + 1][posfx + 1];
              }
              for (int k = posfx + 1; k <= poslx - 1; k += 2) {
                for (int l = cuty + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i;
                }
              }
            } else {
              for (int k = posfx + 1; k <= poslx - 1; k += 2) {
                for (int l = cuty + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
              if (dp[cuty - 1][posfx + 1] != i) {
                now = dp[cuty - 1][posfx + 1];
              }
              for (int k = posfx + 1; k <= poslx - 1; k += 2) {
                for (int l = posfy + 1; l <= cuty - 1; l += 2) {
                  dp[l][k] = i;
                }
              }
            }
            break;
          }
        }
        break;
      }

      // cout << now << endl;
      if (now != -1) {
        for (int k = 1; k <= 2 * w - 1; k += 2) {
          for (int l = 1; l <= 2 * d - 1; l += 2) {
            if (dp[l][k] > now && dp[l][k] <= i) {
              dp[l][k]--;
            }
          }
        }
      }

      switch (flag) {
      case 1:
        if (cutx - posfx > poslx - cutx) {
          for (int k = cutx + 1; k <= poslx - 1; k += 2) {
            for (int l = posfy + 1; l <= posly - 1; l += 2) {
              dp[l][k] = i;
            }
          }
        } else {
          for (int k = posfx + 1; k <= cutx - 1; k += 2) {
            for (int l = posfy + 1; l <= posly - 1; l += 2) {
              dp[l][k] = i;
            }
          }
        }
        break;
      case 2:
        if (cuty - posfy > posly - cuty) {
          for (int k = posfx + 1; k <= poslx - 1; k += 2) {
            for (int l = cuty + 1; l <= posly - 1; l += 2) {
              dp[l][k] = i;
            }
          }
        } else {
          for (int k = posfx + 1; k <= poslx - 1; k += 2) {
            for (int l = posfy + 1; l <= cuty - 1; l += 2) {
              dp[l][k] = i;
            }
          }
        }
        break;
      case 3:
        if (cutx - posfx > poslx - cutx) {
          for (int k = cutx + 1; k <= poslx - 1; k += 2) {
            for (int l = posfy + 1; l <= posly - 1; l += 2) {
              dp[l][k] = i;
            }
          }
        } else {
          for (int k = posfx + 1; k <= cutx - 1; k += 2) {
            for (int l = posfy + 1; l <= posly - 1; l += 2) {
              dp[l][k] = i;
            }
          }
        }
        break;
      case 4:
        if (cuty - posfy > posly - cuty) {
          for (int k = posfx + 1; k <= poslx - 1; k += 2) {
            for (int l = cuty + 1; l <= posly - 1; l += 2) {
              dp[l][k] = i;
            }
          }
        } else {
          for (int k = posfx + 1; k <= poslx - 1; k += 2) {
            for (int l = posfy + 1; l <= cuty - 1; l += 2) {
              dp[l][k] = i;
            }
          }
        }
        break;
      }

      /*
      cout << endl;
      FOR(k, 0, 2 * d) {
              FOR(j, 0, 2 * w) {
                      printf("%1d ", dp[k][j]);
              }
              cout << endl;
      }
      cout << endl;
      */

      /*
      int point = 0;
      for (int k = 1; k <= 2 * w - 1; k += 2) {
              for (int j = 1; j <= 2 * d - 1; j += 2) {
                      //cout << i << " " << j << endl;
                      if (dp[j][k] == 2) {
                              point++;
                      }
              }
      }
      cout << i << " " << point << endl;
      */
    }

    for (int i = 1; i <= 2 * w - 1; i += 2) {
      for (int j = 1; j <= 2 * d - 1; j += 2) {
        // cout << i << " " << j << endl;
        count[dp[j][i] - 1]++;
      }
    }

    sort(count.begin(), count.end());

    // cout << "c" << endl;

    FOR(i, 0, n) {
      cout << count[i];
      if (i != n) {
        cout << " ";
      } else {
        cout << endl;
      }
    }

    /*FOR(i, 0, 2 * d) {
            FOR(j, 0, 2 * w) {
                    cout << dp[i][j] << " ";
            }
            cout << endl;
    }*/
  }
  return 0;
}


Error: 0

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (int i = (a); i >= (b); i--)
#define LFOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define LRFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000            // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000

using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;

int main(void) {

  while (1) {
    int n, w, d;
    int posfx, posfy, poslx, posly;
    int up, right;
    int cutx, cuty;
    int flag;

    cin >> n >> w >> d;
    if (w == 0) {
      break;
    }
    vector<int> p(n + 1), s(n + 1);
    vector<vector<int>> dp(2 * d + 1, vector<int>(2 * w + 1, 0));
    vector<int> count(n + 1, 0);

    FOR(i, 1, n) { cin >> p[i] >> s[i]; }

    for (int i = 1; i <= 2 * d; i += 2) {
      for (int j = 1; j <= 2 * w; j += 2) {
        dp[i][j] = 1;
      }
    }
    FOR(i, 0, 2 * w) {
      dp[0][i] = 1;
      dp[2 * d][i] = 1;
    }
    FOR(i, 0, 2 * d) {
      dp[i][0] = 1;
      dp[i][2 * w] = 1;
    }

    FOR(i, 1, n) {
      posfx = -1;
      posfy = -1;
      poslx = -1;
      posly = -1;
      flag = -1;

      // 周回する辺の確定
      for (int j = 1; j <= 2 * w - 1; j += 2) { // 左上
        for (int k = 1; k <= 2 * d - 1; k += 2) {
          if (dp[k][j] == p[i]) {
            posfx = j - 1;
            posfy = k - 1;
            break;
          }
        }
        if (posfx != -1) {
          break;
        }
      }

      for (int j = 2 * w - 1; j >= 1; j -= 2) { // 右下
        for (int k = 2 * d - 1; k >= 1; k -= 2) {
          if (dp[k][j] == p[i]) {
            poslx = j + 1;
            posly = k + 1;
            break;
          }
        }
        if (poslx != -1) {
          break;
        }
      }

      // cout << "a" << endl;
      // 幅
      up = poslx - posfx;
      right = posly - posfy;
      // cout << i << endl;
      // cout << posfx << " " << poslx << endl;
      // cout << posfy << " " << posly << endl;

      s[i] *= 2;

      // どの部分から切り込むかの判定
      if ((s[i] / (up + right)) % 2 == 0) { // 左上から
        s[i] = s[i] % (up + right);
        if (s[i] <= up - 1) { // 上辺
          cuty = posfy;
          cutx = posfx + s[i];
          flag = 1;
        } else { // 右辺
          // cout << s[i] << endl;
          cutx = poslx;
          cuty = posfy + (s[i] - up);
          flag = 2;
        }

      } else { // 右上から
        s[i] = s[i] % (up + right);
        if (s[i] <= up - 1) { // 下辺
          cuty = posly;
          cutx = poslx - (s[i]);
          flag = 3;
        } else { // 左辺
          // cout << s[i] << " " << up << endl;
          cutx = posfx;
          cuty = posly - (s[i] - up);
          flag = 4;
        }
      }

      // cout << "b" << endl;
      // cout << flag << " " << cutx << " " << cuty << endl;
      // cout << i << endl;

      /*if (i == 19) {
              cout << posfx << " " << poslx << endl;
              cout << posfy << " " << posly << endl;
              cout << s[i] << " " << up << " " << right << endl;
              cout << flag << " " << cutx << " " << cuty << endl;
      }*/

      // 切り込みを入れる 面の番号を変える
      switch (flag) {
      case 1: // 上辺
        FOR(j, 1, INF) {
          if (dp[cuty + j][cutx] == 0) {
            dp[cuty + j][cutx] = 1;
          } else {
            if (cutx - posfx > poslx - cutx) {
              for (int k = posfx + 1; k <= cutx - 1; k += 2) {
                for (int l = posfy + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
            } else {
              for (int k = cutx + 1; k <= poslx - 1; k += 2) {
                for (int l = posfy + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
            }
            break;
          }
        }
        break;
      case 2: // 右辺
        FOR(j, 1, INF) {
          if (dp[cuty][cutx - j] == 0) {
            dp[cuty][cutx - j] = 1;
          } else {
            if (cuty - posfy > posly - cuty) {
              for (int k = posfx + 1; k <= poslx - 1; k += 2) {
                for (int l = posfy + 1; l <= cuty - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
            } else {
              for (int k = posfx + 1; k <= poslx - 1; k += 2) {
                for (int l = cuty + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
            }
            break;
          }
        }
        break;
      case 3: // 下辺
        FOR(j, 1, INF) {
          if (dp[cuty - j][cutx] == 0) {
            dp[cuty - j][cutx] = 1;
          } else {
            if (cutx - posfx > poslx - cutx) {
              for (int k = posfx + 1; k <= cutx - 1; k += 2) {
                for (int l = posfy + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
            } else {
              for (int k = cutx + 1; k <= poslx - 1; k += 2) {
                for (int l = posfy + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
            }
            break;
          }
        }
        break;
      case 4: // 左辺
        FOR(j, 1, INF) {
          if (dp[cuty][cutx + j] == 0) {
            dp[cuty][cutx + j] = 1;
          } else {
            // cout << j << endl;
            if (cuty - posfy > posly - cuty) {
              for (int k = posfx + 1; k <= poslx - 1; k += 2) {
                for (int l = posfy + 1; l <= cuty - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
            } else {
              for (int k = posfx + 1; k <= poslx - 1; k += 2) {
                for (int l = cuty + 1; l <= posly - 1; l += 2) {
                  dp[l][k] = i + 1;
                }
              }
            }
            break;
          }
        }
        break;
      }

      /*
      cout << endl;
      FOR(k, 0, 2 * d) {
              FOR(j, 0, 2 * w) {
                      printf("%1d ", dp[k][j]);
              }
              cout << endl;
      }
      cout << endl;
      */

      int point = 0;
      for (int k = 1; k <= 2 * w - 1; k += 2) {
        for (int j = 1; j <= 2 * d - 1; j += 2) {
          // cout << i << " " << j << endl;
          if (dp[j][k] == 7) {
            point++;
          }
        }
      }
      // cout << i << " " << point << endl;
    }

    for (int i = 1; i <= 2 * w - 1; i += 2) {
      for (int j = 1; j <= 2 * d - 1; j += 2) {
        // cout << i << " " << j << endl;
        count[dp[j][i] - 1]++;
      }
    }

    sort(count.begin(), count.end());

    // cout << "c" << endl;

    FOR(i, 0, n) {
      cout << count[i];
      if (i != n) {
        cout << " ";
      } else {
        cout << endl;
      }
    }

    /*FOR(i, 0, 2 * d) {
            FOR(j, 0, 2 * w) {
                    cout << dp[i][j] << " ";
            }
            cout << endl;
    }*/
  }
  return 0;
}