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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int pos;
struct Node;
vector<Node> global;
struct Node {
  string pat;
  vector<P> xs;
  bool leaf;
  char chr;
  long long len;

  Node(char c) {
    leaf = true;
    chr = c;
    len = 1;
    pat = chr;
  }
  Node(string s) {
    pat = s;
    leaf = false;
    len = 0;

    int num = 0;
    rep(i, s.length()) {
      char c = s[i];
      if ('0' <= c && c <= '9')
        num = 10 * num + (int)(c - '0');
      else {
        if (num > 0) {
          if (c == '(') {
            int state = 1;
            string t = "";
            i++;
            while (true) {
              if (s[i] == '(')
                state++;
              else if (s[i] == ')') {
                state--;
                if (state == 0)
                  break;
              }
              t += s[i];
              i++;
            }
            global.pb(Node(t));
            xs.pb(P(global.size() - 1, num));
            rep(_, num) {
              len += 1LL * global.back().len;
              if (len > pos)
                return;
            }
          } else {
            xs.pb(P(c - 'A', num));
            len += num;
          }
          num = 0;
        } else {
          xs.pb(P(c - 'A', 1));
          len += 1;
        }
      }
      if (len > pos)
        return;
    }
  }

  char get(int offset) {
    assert(offset < len);
    if (leaf)
      return chr;
    long long l = 0;
    for (P p : xs) {
      long long u = global[p._1].len * p._2;
      if (l + u > offset) {
        return global[p._1].get((offset - l) % global[p._1].len);
      }
      l += u;
    }
    return '0';
  }
};

char parse(string s) {
  Node top(s);
  if (top.len <= pos)
    return '0';
  return top.get(pos);
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  rep(i, 26) global.pb(Node((char)('A' + i)));

  while (cin >> s >> pos) {
    if (s == "0")
      break;
    cout << parse(s) << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int pos;
struct Node;
vector<Node> global;
struct Node {
  string pat;
  vector<P> xs;
  bool leaf;
  char chr;
  long long len;

  Node(char c) {
    leaf = true;
    chr = c;
    len = 1;
    pat = chr;
  }
  Node(string s) {
    pat = s;
    leaf = false;
    len = 0;

    int num = 0;
    rep(i, s.length()) {
      char c = s[i];
      if ('0' <= c && c <= '9')
        num = 10 * num + (int)(c - '0');
      else {
        if (num > 0) {
          if (c == '(') {
            int state = 1;
            string t = "";
            i++;
            while (true) {
              if (s[i] == '(')
                state++;
              else if (s[i] == ')') {
                state--;
                if (state == 0)
                  break;
              }
              t += s[i];
              i++;
            }
            global.pb(Node(t));
            xs.pb(P(global.size() - 1, num));
            len += 1LL * num * global.back().len;
          } else {
            xs.pb(P(c - 'A', num));
            len += num;
          }
          num = 0;
        } else {
          xs.pb(P(c - 'A', 1));
          len += 1;
        }
      }
      if (len > pos)
        return;
    }
  }

  char get(int offset) {
    assert(offset < len);
    if (leaf)
      return chr;
    long long l = 0;
    for (P p : xs) {
      long long u = global[p._1].len * p._2;
      if (l + u > offset) {
        return global[p._1].get((offset - l) % global[p._1].len);
      }
      l += u;
    }
    return '0';
  }
};

char parse(string s, int _pos) {
  pos = _pos;
  rep(i, 26) global.pb(Node((char)('A' + i)));
  Node top(s);
  if (top.len <= pos)
    return '0';
  return top.get(pos);
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  int i;
  while (cin >> s >> i) {
    if (s == "0")
      break;
    cout << parse(s, i) << "\\n";
  }
  return 0;
}