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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

struct P {
  int x1, y1, x2, y2;
  string s;
  P(int x1, int y1, int x2, int y2, string s)
      : x1(x1), y1(y1), x2(x2), y2(y2), s(s) {}
};

int main() {
  int n;
  while (cin >> n, n) {
    int w, h;
    cin >> w >> h;
    string title[n];
    vector<P> button[n];
    ;
    map<string, int> rid;
    REP(i, n) {
      cin >> title[i];
      rid[title[i]] = i;
      int m;
      cin >> m;
      REP(j, m) {
        int ax, ay, bx, by;
        cin >> ax >> ay >> bx >> by;
        string s;
        cin >> s;
        button[i].push_back(P(ax, ay, bx, by, s));
      }
    }
    int m;
    cin >> m;
    int now = 0;
    int page[100000] = {};
    int maxpage = 0;
    while (m--) {
      string s;
      cin >> s;
      if (s == "click") {
        int x, y;
        cin >> x >> y;
        FOR(it, button[page[now]]) {
          if (it->x1 <= x && x <= it->x2 && it->y1 <= y && y <= it->y2) {
            page[++now] = rid[it->s];
            maxpage = now;
            break;
          }
        }
      } else if (s == "show") {
        cout << title[page[now]] << endl;
      } else if (s == "back") {
        if (now > 0)
          now--;
      } else if (s == "forward") {
        if (now < maxpage)
          now++;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

struct P {
  int x1, y1, x2, y2;
  string s;
  P(int x1, int y1, int x2, int y2, string s)
      : x1(x1), y1(y1), x2(x2), y2(y2), s(s) {}
};

int main() {
  int n;
  while (cin >> n, n) {
    int w, h;
    cin >> w >> h;
    string title[n];
    vector<P> button[n];
    ;
    map<string, int> rid;
    REP(i, n) {
      cin >> title[i];
      rid[title[i]] = i;
      int m;
      cin >> m;
      REP(j, m) {
        int ax, ay, bx, by;
        cin >> ax >> ay >> bx >> by;
        string s;
        cin >> s;
        button[i].push_back(P(ax, ay, bx, by, s));
      }
    }
    int m;
    cin >> m;
    int now = 0;
    int page[100000] = {};
    int maxpage = 0;
    while (m--) {
      string s;
      cin >> s;
      if (s == "click") {
        int x, y;
        cin >> x >> y;
        FOR(it, button[now]) {
          if (it->x1 <= x && x <= it->x2 && it->y1 <= y && y <= it->y2) {
            page[++now] = rid[it->s];
            maxpage = now;
            break;
          }
        }
      } else if (s == "show") {
        cout << title[now] << endl;
      } else if (s == "back") {
        if (now > 0)
          now--;
      } else if (s == "forward") {
        if (now < maxpage)
          now++;
      }
    }
  }
}