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

/*
 * D.cpp
 *
 *  Created on: 2012-8-25
 *      Author: Administrator
 */
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
using namespace std;
int n, m;
int h, w;
struct page {
  string name;
  int bn;
  int pt[105];
  int x1[105];
  int x2[105];
  int y1[105];
  int y2[105];
} site[105];
int buffer[10000];
string its[105];
int bfend;
int pointer;

int main() {
  while (cin >> n && n) {
    //		memset(site, 0, sizeof(site));
    map<string, int> ma;
    cin >> h >> w;
    int cp = 1;
    string name;
    int b;
    while (n--) {
      cin >> name >> b;
      if (ma[name] == 0) {
        its[cp] = name;
        ma[name] = cp++;
      }
      int currpage = ma[name];
      //			cout << "currpage " << currpage << endl;
      site[currpage].bn = b;
      //			cout << "b " << b << endl;
      int x1, y1, x2, y2;
      string pp;
      for (int i = 0; i != b; i++) {
        cin >> x1 >> y1 >> x2 >> y2 >> pp;
        //				cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<"
        //"<<pp<<endl;
        if (ma[pp] == 0) {
          its[cp] = pp;
          ma[pp] = cp++;
        }
        int ipp = ma[pp];
        site[currpage].x1[i] = x1;
        site[currpage].x2[i] = x2;
        site[currpage].y1[i] = y1;
        site[currpage].y2[i] = y2;
        site[currpage].pt[i] = ipp;
      }
    }
    pointer = 0;
    bfend = 0;
    buffer[pointer] = 1;
    cin >> m;
    string command;
    while (m--) {
      //			cout<<pointer<<endl;
      cin >> command;
      if (command == "show") {
        cout << its[buffer[pointer]] << endl;
      }
      if (command == "forward") {
        if (pointer < bfend)
          pointer++;
      }
      if (command == "back") {
        if (pointer > 0)
          pointer--;
      }
      if (command == "click") {
        int x, y;
        cin >> x >> y;
        int page = buffer[pointer];
        int nextpage;
        bool flag = false;
        for (int i = 0; i != site[page].bn; i++) {
          int x1 = site[page].x1[i];
          int x2 = site[page].x2[i];
          int y1 = site[page].y1[i];
          int y2 = site[page].y2[i];
          if (x >= x1 && x <= x2 && y >= y1 && y <= y2) {
            nextpage = site[page].pt[i];
            flag = true;
            break;
          }
        }
        if (flag) {
          bfend = pointer + 1;
          pointer++;
          buffer[pointer] = nextpage;
        }
      }
    }
  }
}

Error: 0

Fixed submission:

/*************************************************************************
Author: ziki
Created Time: 2012-8-9 12:39:37
File Name: d.cpp
Description:
************************************************************************/
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long int64;
typedef unsigned long long uint64;
#define two(X) (1 << (X))
#define twoL(X) (((int64)(1)) << (X))
#define contain(S, X) (((S)&two(X)) != 0)
#define containL(S, X) (((S)&twoL(X)) != 0)
const double pi = acos(-1.0);
const double eps = 1e-11;
const int inf = 0x7FFFFFFF;
template <class T> inline void checkmin(T &a, T b) {
  if (b < a)
    a = b;
}
template <class T> inline void checkmax(T &a, T b) {
  if (b > a)
    a = b;
}
template <class T> inline T sqr(T x) { return x * x; }
typedef pair<int, int> ipair;
#define SIZE(A) ((int)A.size())
#define LENGTH(A) ((int)A.length())
#define MP(A, B) make_pair(A, B)
#define PB(X) push_back(X)
#define zero(a) memset(a, 0, sizeof(a))
#define out(x) (cout << #x << ": " << x << endl)
template <class T> void show(T a, int n) {
  for (int i = 0; i < n; ++i)
    cout << a[i] << ' ';
  cout << endl;
}
template <class T> void show(T a, int r, int l) {
  for (int i = 0; i < r; ++i)
    show(a[i], l);
  cout << endl;
}

struct obj {
  int x1, x2, y1, y2;
  int idx;
  obj(int x1, int y1, int x2, int y2, int s)
      : x1(x1), y1(y1), x2(x2), y2(y2), idx(s) {}
  int inter(int x, int y) { return x1 <= x && x <= x2 && y >= y1 && y <= y2; }
};
struct S {
  string nm;
  vector<obj> e;
  void init(string s) {
    nm = s;
    e.clear();
  }
} s[1000];

int stk[1000], now, top;
int main() {
  int n, m, w, h;
  while (cin >> n && n) {
    cin >> w >> h;
    top = 0;
    map<string, int> mp;
    int idx = 0;
    for (int i = 0; i < n; i++) {
      int eec;
      int x1, x2, y1, y2, id, id2;
      string nm;
      cin >> nm >> eec;
      if (mp.count(nm))
        id = mp[nm];
      else
        id = mp[nm] = idx++;
      s[id].init(nm);
      for (int j = 0; j < eec; j++) {
        cin >> x1 >> y1 >> x2 >> y2 >> nm;
        if (x1 < 0)
          x1 = 0;
        if (x1 > w)
          x1 = w;
        if (x2 < 0)
          x2 = 0;
        if (x2 > w)
          x2 = w;
        if (y1 < 0)
          y1 = 0;
        if (y1 > h)
          y1 = h;
        if (y2 < 0)
          y2 = 0;
        if (y2 > h)
          y2 = h;
        if (x1 > x2)
          swap(x1, x2);
        if (y1 > y2)
          swap(y1, y2);
        if (mp.count(nm))
          id2 = mp[nm];
        else
          id2 = mp[nm] = idx++;
        s[id].e.PB(obj(x1, y1, x2, y2, id2));
      }
    }
    cin >> m;
    now = top = 0;
    stk[0] = 0;
    int tx, ty, it = stk[now], to = -1;
    for (int i = 0; i < m; i++) {
      it = stk[now], to = -1;
      string cmd;
      cin >> cmd;
      if (cmd == "click") {

        cin >> tx >> ty;
        if (tx >= 0 && tx <= w && ty >= 0 && ty <= h)
          for (int j = 0; j < s[it].e.size(); j++) {
            if (s[it].e[j].inter(tx, ty)) {
              to = s[it].e[j].idx;
              break;
            }
          }
        if (to != -1) {
          stk[++now] = to;
          top = now;
        }
      } else if (cmd == "show") {
        cout << s[it].nm << endl;
      } else if (cmd == "back") {
        if (now > 0)
          now--;
      } else if (cmd == "forward") {
        if (now < top)
          now++;
      }
    }
  }
  return 0;
}