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

#include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

using namespace std;

typedef long long ll;

int val[4000];
vector<int> tree[4000];

int rec(int p, int hei) {
  if (val[p] != -1)
    return val[p];

  int ret = 0;
  vector<int> list;
  for (int i = 0; i < tree[p].size(); i++) {
    list.push_back(rec(tree[p][i], hei + 1));
  }

  sort(list.begin(), list.end());
  for (int i = 0; i < list.size() / 2 + 1; i++) {
    ret += list[i];
  }

  return val[p] = ret;
}

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    string str;
    cin >> str;

    vector<int> temp;
    for (int j = 0; j < 4000; j++) {
      tree[j] = temp;
    }

    memset(val, -1, sizeof(val)); // 入ってないところは-1
    stack<int> st;
    int cnt = 0;
    int p = 0;
    st.push(cnt);
    while (!st.empty()) {
      p++;
      if (str[p] == '[') {
        cnt++;
        st.push(cnt);
      } else if (str[p] == ']') {
        int topv = st.top();
        st.pop();
        if (!st.empty()) {
          tree[st.top()].push_back(topv);
        }
      } else {
        int v = 0;
        for (; p < str.size(); p++) {
          if (str[p] == ']') {
            p--;
            break;
          }
          v = v * 10 + str[p] - '0';
        }
        val[cnt] = v / 2 + 1;
      }
    }

    cout << rec(0, 0) << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sys/time.h>
#include <vector>

using namespace std;

typedef long long ll;

int val[3000];
vector<int> tree[3000];

int rec(int p, int hei) {
  if (val[p] != -1)
    return val[p];

  int ret = 0;
  vector<int> list;
  for (int i = 0; i < tree[p].size(); i++) {
    list.push_back(rec(tree[p][i], hei + 1));
  }

  sort(list.begin(), list.end());
  for (int i = 0; i < list.size() / 2 + 1; i++) {
    ret += list[i];
  }

  return val[p] = ret;
}

int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    string str;
    cin >> str;

    vector<int> temp;
    for (int j = 0; j < 3000; j++) {
      tree[j] = temp;
    }

    memset(val, -1, sizeof(val)); // 入ってないところは-1
    stack<int> st;
    int cnt = 0;
    int p = 0;
    st.push(cnt);
    while (!st.empty()) {
      p++;
      if (str[p] == '[') {
        cnt++;
        st.push(cnt);
      } else if (str[p] == ']') {
        int topv = st.top();
        st.pop();
        if (!st.empty()) {
          tree[st.top()].push_back(topv);
          // tree[topv].push_back(st.top());
        }
      } else {
        int v = 0;
        for (; p < str.size(); p++) {
          if (str[p] == ']') {
            p--;
            break;
          }
          v = v * 10 + str[p] - '0';
        }
        val[cnt] = v / 2 + 1;
      }
    }

    cout << rec(0, 0) << endl;

    // for (int i = 0; i <= cnt; i++) {
    //   std::cout << i << ":";
    //   for (int j = 0; j < tree[i].size(); j++) {
    // 	std::cout << tree[i][j] << " ";
    //   }
    //   std::cout << " = " << val[i] << std::endl;
    // }
  }
}