Problem p00640 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

struct point {
  int x, y;
};

struct button {
  point l, r;
  string name;
};

struct page {
  string name;
  vector<button> btn;
};

int m, now;
vector<page> a;
page nowpage;
string buf[10001];

void func(void) {
  for (int i = 0; i < a.size(); i++) {
    if (buf[now] == a[i].name) {
      nowpage = a[i];
      return;
    }
  }
}

void click(int c, int d) {

  for (int i = 0; i < nowpage.btn.size(); i++) {
    if (nowpage.btn[i].l.x <= c && c <= nowpage.btn[i].r.x &&
        nowpage.btn[i].l.y <= d && d <= nowpage.btn[i].r.y) {
      fill(buf + now + 1, buf + 10001, "");
      buf[++now] = nowpage.btn[i].name;
      return;
    }
  }
}

int main(void) {
  int n;
  int w, h, b;
  page in;
  button tmp;

  while (cin >> n, n) {

    a.clear();
    fill(buf, buf + 10001, "");

    cin >> w >> h;

    for (int i = 0; i < n; i++) {
      in.btn.clear();
      cin >> in.name >> b;
      if (i == 0)
        buf[0] = in.name;

      for (int j = 0; j < b; j++) {
        cin >> tmp.l.x >> tmp.l.y >> tmp.r.x >> tmp.r.y >> tmp.name;
        in.btn.push_back(tmp);
      }
      a.push_back(in);
    }

    now = 0;

    cin >> m;

    while (m--) {
      string s;
      cin >> s;

      func();

      if (s == "click") {
        int c, d;
        cin >> c >> d;
        click(c, d);
      } else if (s == "back" && now > 0)
        now--;
      else if (s == "forward" && buf[now + 1] != "")
        now++;
      else if (s == "show")
        cout << buf[now] << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

struct point {
  int x, y;
};

struct button {
  point ld, ru;
  string name;
};

struct page {
  string name;
  vector<button> btn;
};

int m, now;
vector<page> a;
page nowpage;
string buf[100000];

void func(void) {
  for (int i = 0; i < a.size(); i++) {
    if (buf[now] == a[i].name) {
      nowpage = a[i];
      return;
    }
  }
}

void click(int c, int d) {

  for (int i = 0; i < nowpage.btn.size(); i++) {
    if (nowpage.btn[i].ld.x <= c && nowpage.btn[i].ld.y <= d &&
        nowpage.btn[i].ru.x >= c && nowpage.btn[i].ru.y >= d) {
      buf[++now] = nowpage.btn[i].name;
      fill(buf + now + 1, buf + 100000, "");
      return;
    }
  }
}

int main(void) {
  int n;
  int w, h, b;
  point l, r;
  page in;
  button tmp;

  while (cin >> n, n) {

    a.clear();
    fill(buf, buf + 100000, "");

    cin >> w >> h;

    for (int j = 0; j < n; j++) {
      cin >> in.name >> b;

      for (int i = 0; i < b; i++) {
        cin >> tmp.ld.x >> tmp.ld.y >> tmp.ru.x >> tmp.ru.y >> tmp.name;
        in.btn.push_back(tmp);
      }
      a.push_back(in);
    }

    now = 0;
    buf[0] = a[0].name;
    nowpage = a[0];

    cin >> m;

    while (m--) {
      string s;
      cin >> s;
      if (s == "click") {
        int c, d;
        cin >> c >> d;
        click(c, d);
      } else if (s == "back" && now > 0)
        now--;
      else if (s == "forward" && buf[now + 1] != "")
        now++;
      else if (s == "show")
        cout << buf[now] << endl;

      func();
    }
  }

  return 0;
}