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

#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
const int INF = (1LL << 30);
const ll LLINF = (1LL << 60);
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;
// #define int ll

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

typedef string::const_iterator State;
class ParseError {};
int tree(State &);
int number(State &);

PII t[2][100010], ans[100010];
int idx;
int heap1[100010], heap2[100010], heap[100010];
int val[2][100010], val2[100010];
int cnt;

int tree(State &begin) {
  if (*begin != '(')
    return -1;
  begin++;
  int left = tree(begin);
  begin++;
  begin++;
  int res = number(begin);
  begin++;
  begin++;
  int right = tree(begin);
  begin++;
  val[idx][cnt] = res;
  t[idx][cnt++] = {left, right};
  // cout << left << " " << cnt-1 << " " << res << " " << right << endl;
  return cnt - 1;
}

int number(State &begin) {
  int res = 0;
  while (isdigit(*begin)) {
    res *= 10;
    res += *begin - '0';
    ++begin;
  }
  return res;
}

string dfs(int num) {
  if (num == -1)
    return "";
  string ret = "(";
  ret += (dfs(ans[num].first));
  ret += (")[");
  ret += (to_string(val2[num]));
  ret += ("](");
  ret += (dfs(ans[num].second));
  ret += (")");
  return ret;
}

int func(int a, int b) {
  if (a == -1 || b == -1)
    return -1;
  int left = func(t[0][a].first, t[1][b].first);
  int right = func(t[0][a].second, t[1][b].second);
  val2[cnt] = val[0][a] + val[1][b];
  // cout << left << " val2[" << cnt << "] = " << val2[cnt] << " " << right <<
  // endl;
  ans[cnt++] = {left, right};
  return cnt - 1;
}

signed main(void) {
  string s, tt;
  cin >> s >> tt;
  memset(val, -1, sizeof(val));
  memset(heap1, -1, sizeof(heap1));
  memset(heap2, -1, sizeof(heap2));
  memset(heap, -1, sizeof(heap));
  idx = 0, cnt = 0;
  State begin = s.begin();
  int root1 = tree(begin);
  idx = 1, cnt = 0;
  begin = tt.begin();
  int root2 = tree(begin);

  memset(val2, -1, sizeof(val2));
  cnt = 0;
  int tmp = func(root1, root2);

  cout << dfs(tmp) << endl;

  return 0;
}

Error: 0

Fixed submission:

#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
const int INF = (1LL << 30);
const ll LLINF = (1LL << 60);
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;
// #define int ll

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

typedef string::const_iterator State;
class ParseError {};
int tree(State &);
int number(State &);

PII t[2][100010];
int idx;
int heap1[100010], heap2[100010], heap[100010];
int val[2][100010];
int cnt;

int tree(State &begin) {
  if (*begin != '(')
    return -1;
  begin++;
  int left = tree(begin);
  begin++;
  begin++;
  int res = number(begin);
  begin++;
  begin++;
  int right = tree(begin);
  begin++;
  val[idx][cnt] = res;
  t[idx][cnt++] = {left, right};
  // cout << left << " " << cnt-1 << " " << res << " " << right << endl;
  return cnt - 1;
}

int number(State &begin) {
  int res = 0;
  while (isdigit(*begin)) {
    res *= 10;
    res += *begin - '0';
    ++begin;
  }
  return res;
}

string dfs(int num) {
  if (heap[num] == -1)
    return "";
  string ret = "(";
  ret += (dfs(num * 2 + 1));
  ret += (")[");
  ret += (to_string(heap[num]));
  ret += ("](");
  ret += (dfs(num * 2 + 2));
  ret += (")");
  return ret;
}

signed main(void) {
  string s, tt;
  cin >> s >> tt;
  memset(val, -1, sizeof(val));
  memset(heap1, -1, sizeof(heap1));
  memset(heap2, -1, sizeof(heap2));
  memset(heap, -1, sizeof(heap));
  idx = 0, cnt = 0;
  State begin = s.begin();
  int root1 = tree(begin);
  idx = 1, cnt = 0;
  begin = tt.begin();
  int root2 = tree(begin);

  queue<PII> que;
  que.push({root1, 0});
  heap1[0] = val[0][root1];
  while (que.size()) {
    PII p = que.front();
    que.pop();
    int v = p.first, index = p.second;
    // cout << t[0][v].first << "," << v << "," << t[0][v].second << endl;
    if (v == -1)
      continue;
    heap1[index * 2 + 1] = t[0][v].first == -1 ? -1 : val[0][t[0][v].first];
    que.push({t[0][v].first, index * 2 + 1});
    heap1[index * 2 + 2] = t[0][v].second == -1 ? -1 : val[0][t[0][v].second];
    que.push({t[0][v].second, index * 2 + 2});
  }

  while (que.size())
    que.pop();
  que.push({root2, 0});
  heap2[0] = val[1][root2];
  while (que.size()) {
    PII p = que.front();
    que.pop();
    int v = p.first, index = p.second;
    if (v == -1)
      continue;
    heap2[index * 2 + 1] = t[1][v].first == -1 ? -1 : val[1][t[1][v].first];
    que.push({t[1][v].first, index * 2 + 1});
    heap2[index * 2 + 2] = t[1][v].second == -1 ? -1 : val[1][t[1][v].second];
    que.push({t[1][v].second, index * 2 + 2});
  }

  REP(i, 100010) {
    if (heap1[i] == -1 || heap2[i] == -1) {
      heap[i] = -1;
    } else {
      heap[i] = heap1[i] + heap2[i];
    }
  }

  cout << dfs(0) << endl;

  return 0;
}