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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
const int inf = 1e9;

// range min
struct segmentTree {
  vector<int> node;
  int n;

  segmentTree(vector<int> v) {
    int s = v.size();
    n = 2;
    while (n < s)
      n *= 2;
    node = vector<int>(2 * n - 1, inf);
    for (int i = 0; i < s; i++) {
      node[i + n - 1] = v[i];
    }
    for (int i = n - 2; i >= 0; i--) {
      node[i] = min(node[2 * i + 1], node[2 * i + 2]);
    }
  }
  segmentTree() {}

  void Set(int val, int pos) {
    int idx = n - 1 + pos;
    node[idx] = val;
    do {
      idx = (idx - 1) / 2;
      node[idx] = min(node[2 * idx + 1], node[2 * idx + 2]);
    } while (idx != 0);
  }
  int Get(int l, int r) {
    if (l == r)
      return inf;
    return _get(l, r, 0, 0, n);
  }
  int _get(int l, int r, int k, int cl, int cr) {
    if (l <= cl && cr <= r) {
      return node[k];
    }
    if (r <= cl || cr <= l) {
      return inf;
    }
    int m = (cl + cr) / 2;
    return min(_get(l, r, 2 * k + 1, cl, m), _get(l, r, 2 * k + 2, m, cr));
  }
};

vector<vector<int>> tree, query, mark;
vector<int> dep2idx;
segmentTree segtree;
long long int solve(int node, int depth) {
  long long int ret = 0;
  for (int i = 0; i < (int)mark[node].size(); i++) {
    segtree.Set(-depth, mark[node][i]);
    dep2idx[depth] = node;
  }
  for (int i = 0; i < (int)query[node].size(); i++) {
    ret += dep2idx[-segtree.Get(0, query[node][i])];
  }
  for (int i = 0; i < (int)tree[node].size(); i++) {
    ret += solve(tree[node][i], depth + 1);
  }
  for (int i = 0; i < (int)mark[node].size(); i++) {
    segtree.Set(0, mark[node][i]);
    dep2idx[depth] = 0;
  }
  return ret;
}

int main() {
  while (1) {
    int n, q;
    cin >> n >> q;
    if (n == 0)
      break;

    vector<vector<int>> vvi(n + 1);
    tree = query = mark = vvi;
    for (int i = 2; i <= n; i++) {
      int par;
      cin >> par;
      tree[par].push_back(i);
    }
    mark[1].push_back(0);
    for (int i = 1; i <= q; i++) {
      char c;
      int node;
      cin >> c >> node;
      if (c == 'Q')
        query[node].push_back(i);
      else
        mark[node].push_back(i);
    }
    segtree = segmentTree(vector<int>(q + 1, inf));
    dep2idx = vector<int>(n, 0);
    cout << solve(1, 0) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const long long int inf = 1e18;

// range min
struct segmentTree {
  vector<long long int> node;
  int n;

  segmentTree(vector<long long int> v) {
    int s = v.size();
    n = 2;
    while (n < s)
      n *= 2;
    node = vector<long long int>(2 * n - 1, inf);
    for (int i = 0; i < s; i++) {
      node[i + n - 1] = v[i];
    }
    for (int i = n - 2; i >= 0; i--) {
      node[i] = min(node[2 * i + 1], node[2 * i + 2]);
    }
  }

  void Set(long long int val, int pos) {
    int idx = n - 1 + pos;
    node[idx] = val;
    do {
      idx = (idx - 1) / 2;
      node[idx] = min(node[2 * idx + 1], node[2 * idx + 2]);
    } while (idx != 0);
  }
  long long int Get(int l, int r) { return _get(l, r, 0, 0, n); }
  long long int _get(int l, int r, int k, int cl, int cr) {
    if (l <= cl && cr <= r) {
      return node[k];
    }
    if (r <= cl || cr <= l) {
      return inf;
    }
    int m = (cl + cr) / 2;
    return min(_get(l, r, 2 * k + 1, cl, m), _get(l, r, 2 * k + 2, m, cr));
  }
};

long long int solve(int node, int depth, vector<vector<int>> &tree,
                    vector<vector<int>> &mark, vector<vector<int>> &query,
                    segmentTree &segtree, vector<int> &tl) {
  long long int ret = 0;
  for (int i = 0; i < (int)query[node].size(); i++) {
    ret += tl[-segtree.Get(0, query[node][i])];
  }
  for (int i = 0; i < (int)mark[node].size(); i++) {
    segtree.Set(-depth, mark[node][i]);
    tl[depth] = node;
  }
  for (int i = 0; i < (int)tree[node].size(); i++) {
    ret += solve(tree[node][i], depth + 1, tree, mark, query, segtree, tl);
  }
  for (int i = 0; i < (int)mark[node].size(); i++) {
    segtree.Set(0, mark[node][i]);
    tl[depth] = 0;
  }
  return ret;
}

int main() {
  while (1) {
    int n, q;
    cin >> n >> q;
    if (n == 0)
      break;

    vector<vector<int>> tree(n + 1);
    for (int i = 2; i <= n; i++) {
      int par;
      cin >> par;
      tree[par].push_back(i);
    }
    vector<vector<int>> query(n + 1), mark(n + 1);
    mark[1].push_back(0);
    for (int i = 1; i <= q; i++) {
      char c;
      int node;
      cin >> c >> node;
      if (c == 'Q')
        query[node].push_back(i);
      else
        mark[node].push_back(i);
    }
    segmentTree segtree(vector<long long int>(n + 1, inf));
    vector<int> timeline(n, 0);
    cout << solve(1, 0, tree, mark, query, segtree, timeline) << endl;
  }
  return 0;
}