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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

struct S {
  int x1, y1, x2, y2;
  string name;
};

int main() {
  int n;
  while (cin >> n, n) {
    int h, w;
    cin >> h >> w;
    vector<string> name(n);
    map<string, vector<S>> button;

    cin.ignore();

    rep(i, n) {
      int k;
      string str;
      getline(cin, str);
      int p = str.rfind(" ");

      name[i] = str.substr(0, p);
      stringstream(str.substr(p)) >> k;

      rep(j, k) {
        S s;
        string str;
        getline(cin, str);

        int p = 0;
        rep(it, 4) p = str.find(" ", p + 1);
        s.name = str.substr(p + 1);

        stringstream(str.substr(0, p)) >> s.x1 >> s.y1 >> s.x2 >> s.y2;
        button[name[i]].pb(s);
      }
    }

    int m;
    cin >> m;
    vector<string> buffer;
    int cur = 0;
    buffer.pb(name[0]);

    rep(i, m) {
      string op;
      cin >> op;
      if (op == "show") {
        cout << buffer[cur] << endl;
      } else if (op == "click") {
        int x, y;
        cin >> x >> y;
        fr(it, button[buffer[cur]]) if (it->x1 <= x && x <= it->x2 &&
                                        it->y1 <= y && y <= it->y2) {
          buffer.erase(buffer.begin() + cur + 1, buffer.end());
          buffer.pb(it->name);
          cur++;
          break;
        }
      } else if (op == "back") {
        if (cur > 0)
          cur--;
      } else if (op == "forward") {
        if (cur < buffer.size() - 1)
          cur++;
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

struct S {
  int x1, y1, x2, y2;
  string name;
};

int main() {
  int n;
  while (cin >> n, n) {
    int h, w;
    cin >> h >> w;
    vector<string> name(n);
    map<string, vector<S>> button;

    cin.ignore();

    rep(i, n) {
      int k;
      string str;
      getline(cin, str);
      int p = str.rfind(" ");

      name[i] = str.substr(0, p);
      stringstream(str.substr(p)) >> k;

      rep(j, k) {
        S s;
        string str;
        getline(cin, str);

        int p = 0;
        rep(it, 4) p = str.find(" ", p + 1);
        s.name = str.substr(p + 1);

        stringstream(str.substr(0, p)) >> s.x1 >> s.y1 >> s.x2 >> s.y2;
        button[name[i]].pb(s);
      }
    }

    int m;
    cin >> m;
    vector<string> buffer;
    int cur = 0;
    buffer.pb(name[0]);

    rep(i, m) {
      string op;
      cin >> op;
      if (op == "show") {
        cout << buffer[cur] << endl;
      } else if (op == "click") {
        int x, y;
        cin >> x >> y;
        fr(it, button[name[cur]]) if (it->x1 <= x && x <= it->x2 &&
                                      it->y1 <= y && y <= it->y2) {
          buffer.erase(buffer.begin() + cur + 1, buffer.end());
          buffer.pb(it->name);
          cur++;
          break;
        }
      } else if (op == "back") {
        if (cur > 0)
          cur--;
      } else if (op == "forward") {
        if (cur < buffer.size() - 1)
          cur++;
      }
    }
  }

  return 0;
}