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

#include <bits/stdc++.h>
using namespace std;

int aidx, ah, bidx, bh;
vector<int> pile[105];
int m;

void put_floor(int x) {

  for (int i = 0; i < m; i++)
    if (!pile[i].size()) {
      pile[i].push_back(x);
      return;
    }
}

void get_place_a(int x) {

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < pile[i].size(); j++) {

      if (pile[i][j] == x) {

        aidx = i;
        ah = j;
        return;
      }
    }
  }
}

void get_place_b(int x) {

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < pile[i].size(); j++) {

      if (pile[i][j] == x) {

        bidx = i;
        bh = j;
        return;
      }
    }
  }
}

int main() {

  while (1) {

    cin >> m;
    if (!m)
      break;

    for (int i = 0; i < m; i++)
      pile[i].clear();

    for (int i = 0; i < m; i++)
      pile[i].push_back(i + 1);

    while (1) {

      int a, b;

      cin >> a >> b;
      if (!a)
        break;

      if (!b) {

        get_place_a(a);

        if (ah == 0)
          continue;

        for (int i = pile[aidx].size() - 1; i >= ah; i--) {

          put_floor(pile[aidx][i]);

          pile[aidx].erase(pile[aidx].end() - 1);
        }

      } else {

        get_place_a(a);

        get_place_b(b);

        if (aidx == bidx && ah >= bh)
          continue;

        for (int i = pile[aidx].size() - 1; i > ah; i--) {

          put_floor(pile[aidx][i]);

          pile[aidx].erase(pile[aidx].end() - 1);
        }

        pile[aidx].erase(pile[aidx].end() - 1);
        // pile[aidx].erase( pile[aidx].begin()+ah );

        get_place_b(b);

        pile[bidx].push_back(a);
      }
    }

    vector<int> ans;

    for (int i = 0; i < m; i++)
      ans.push_back(pile[i].size());

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

    for (int i = 0; i < m; i++) {
      if (ans[i] == 0)
        continue;
      cout << ans[i] << endl;
    }
    cout << "end" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int aidx, ah, bidx, bh;
vector<int> pile[105];
int m;

void put_floor(int x) {

  for (int i = 0; i < m; i++)
    if (!pile[i].size())
      pile[i].push_back(x);
}

void get_place_a(int x) {

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < pile[i].size(); j++) {

      if (pile[i][j] == x) {

        aidx = i;
        ah = j;
        return;
      }
    }
  }
}

void get_place_b(int x) {

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < pile[i].size(); j++) {

      if (pile[i][j] == x) {

        bidx = i;
        bh = j;
        return;
      }
    }
  }
}

int main() {

  while (1) {

    cin >> m;
    if (!m)
      break;

    for (int i = 0; i < m; i++)
      pile[i].push_back(i + 1);

    while (1) {

      int a, b;

      cin >> a >> b;
      if (!a)
        break;

      if (!b) {

        get_place_a(a);

        if (ah == 0)
          continue;

        for (int i = pile[aidx].size() - 1; i >= ah; i--) {

          put_floor(pile[aidx][i]);

          pile[aidx].erase(pile[aidx].end() - 1);
        }

      } else {

        get_place_a(a);

        get_place_b(b);

        if (aidx == bidx && ah >= bh)
          continue;

        for (int i = pile[aidx].size() - 1; i > ah; i--) {

          put_floor(pile[aidx][i]);

          pile[aidx].erase(pile[aidx].end() - 1);
        }

        pile[aidx].erase(pile[aidx].begin() + ah);

        get_place_b(b);

        pile[bidx].push_back(a);
      }
    }

    vector<int> ans;

    for (int i = 0; i < m; i++)
      ans.push_back(pile[i].size());

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

    for (int i = 0; i < m; i++) {
      if (ans[i] == 0)
        continue;
      cout << ans[i] << endl;
    }
    cout << "end" << endl;

    for (int i = 0; i < m; i++)
      pile[i].clear();
  }

  return 0;
}