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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

struct Button {
  int x1, y1, x2, y2;
  string name;
  bool include(int x, int y) {
    return x1 <= x && x <= x2 && y1 <= y && y <= y2;
  }
  void input() { cin >> x1 >> y1 >> x2 >> y2 >> name; }
};
int main() {
  int n, m, w, h;
  while (cin >> n, n) {
    cin >> w >> h;

    map<string, vector<Button>> page;
    vector<string> buffer;
    rep(i, n) {
      string name;
      int b;
      cin >> name >> b;
      if (i == 0)
        buffer.push_back(name);

      while (b--) {
        page[name].push_back(Button());
        page[name].back().input();
      }
    }

    cin >> m;
    int pointer = 0;
    while (m--) {
      string &b = buffer[pointer];

      string c;
      cin >> c;
      if (c[0] == 'c') {
        int x, y;
        cin >> x >> y;
        for (int i = 0; i < page[b].size(); ++i) {
          if (page[b][i].include(x, y)) {
            ++pointer;
            buffer.erase(buffer.begin() + pointer, buffer.end());
            buffer.push_back(page[b][i].name);
            break;
          }
        }
      } else if (c[0] == 'b')
        pointer = max(0, --pointer);
      else if (c[0] == 'f')
        pointer = min((int)buffer.size() - 1, ++pointer);
      else if (c[0] == 's')
        cout << b << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

struct Button {
  int x1, y1, x2, y2;
  string name;
  bool include(int x, int y) {
    return x1 <= x && x <= x2 && y1 <= y && y <= y2;
  }
  void input() { cin >> x1 >> y1 >> x2 >> y2 >> name; }
};
int main() {
  int n, m, w, h;
  while (cin >> n, n) {
    cin >> w >> h;

    map<string, vector<Button>> page;
    vector<string> buffer;
    rep(i, n) {
      string name;
      int b;
      cin >> name >> b;
      if (i == 0)
        buffer.push_back(name);

      while (b--) {
        page[name].push_back(Button());
        page[name].back().input();
      }
    }

    cin >> m;
    int pointer = 0;
    while (m--) {
      string &b = buffer[pointer];

      string c;
      cin >> c;
      if (c[0] == 'c') {
        int x, y;
        cin >> x >> y;
        for (int i = 0; i < page[b].size(); ++i) {
          if (page[b][i].include(x, y)) {
            ++pointer;
            buffer.erase(buffer.begin() + pointer, buffer.end());
            buffer.push_back(page[b][i].name);
            break;
          }
        }
      } else if (c[0] == 'b')
        --pointer;
      else if (c[0] == 'f')
        ++pointer;
      else if (c[0] == 's')
        cout << b << endl;
    }
  }
}