Problem p00617 (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, x2, y1, y2, c;
  string name;
  bool contain(int x, int y) {
    return x1 <= x && x <= x2 && y1 <= y && y <= y2;
  }
};
S pnl[1000];
int n, m;
string in;

int eval(int s, int t) {
  if (s >= t)
    return 0;
  assert(in[s] == '<');

  int id = m, bt = in.find('>', s), ns = in.find('<', bt + 1);
  m++;
  pnl[id].name = in.substr(s + 1, bt - s - 1);

  string tmp = in.substr(bt + 1, ns - bt - 1);
  fr(i, tmp) if (*i == ',') *i = ' ';
  stringstream ss(tmp);
  ss >> pnl[id].x1 >> pnl[id].y1 >> pnl[id].x2 >> pnl[id].y2;

  tmp = "</" + pnl[id].name + ">";
  int es = in.find(tmp, bt), et = es + pnl[id].name.size() + 3;
  assert(s <= es && es <= t);

  int ret = 1;
  pnl[id].c = eval(ns, es);
  if (et != t)
    ret += eval(et, t);
  return ret;
}
int main() {
  while (cin >> n, n) {
    cin >> in;
    m = 0;
    eval(0, in.size());

    rep(i, n) {
      int x, y, id = m - 1;
      cin >> x >> y;
      for (; id >= 0; id--)
        if (pnl[id].contain(x, y))
          break;

      if (id < 0)
        cout << "OUT OF MAIN PANEL 1" << endl;
      else
        cout << pnl[id].name << " " << pnl[id].c << endl;
    }
  }
  return 0;
}

Error: -6
b45484c0-7107-4e84-b5ff-172d14ea9ca2.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00617/C++/s212912258.cpp:69: int main(): Assertion `0' failed.


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, x2, y1, y2, c;
  string name;
  bool contain(int x, int y) {
    return x1 <= x && x <= x2 && y1 <= y && y <= y2;
  }
};
S pnl[1000];
int n, m;
string in;

int eval(int s, int t) {
  if (s >= t)
    return 0;
  assert(in[s] == '<');

  int id = m, bt = in.find('>', s), ns = in.find('<', bt + 1);
  m++;
  pnl[id].name = in.substr(s + 1, bt - s - 1);

  string tmp = in.substr(bt + 1, ns - bt - 1);
  fr(i, tmp) if (*i == ',') *i = ' ';
  stringstream ss(tmp);
  ss >> pnl[id].x1 >> pnl[id].y1 >> pnl[id].x2 >> pnl[id].y2;

  tmp = "</" + pnl[id].name + ">";
  int es = in.find(tmp, bt), et = es + pnl[id].name.size() + 3;
  assert(s <= es && es <= t);

  int ret = 1;
  pnl[id].c = eval(ns, es);
  if (et != t)
    ret += eval(et, t);
  return ret;
}
int main() {
  while (cin >> n, n) {
    cin >> in;
    assert(0);
    m = 0;
    eval(0, in.size());
    rep(i, n) {
      int x, y, id = m;
      cin >> x >> y;
      for (; id >= 0; id--)
        if (pnl[id].contain(x, y))
          break;

      if (id < 0)
        cout << "OUT OF MAIN PANEL 1" << endl;
      else
        cout << pnl[id].name << " " << pnl[id].c << endl;
    }
  }
  return 0;
}