Problem p01288 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define MAX (1 << 17)
#define INF (1LL << 55)
typedef long long ll;
typedef pair<ll, ll> P;

struct SegmentTree {
  int n;
  P dat[2 * MAX - 1];

  SegmentTree(int n_) {
    n = 1;
    while (n < n_) {
      n *= 2;
    }
    for (int i = 0; i < 2 * n - 1; i++) {
      dat[i] = P(0, 1);
    }
  }

  ll get_nearest_marked_node(int k) {
    k += n - 1;
    P p = dat[k];
    while (k > 0) {
      k = (k - 1) / 2;
      p = max(p, dat[k]);
    }
    return p.second;
  }

  void mark(int a, int b, int k, int l, int r, P x) {
    if (r <= a || b <= l)
      return;

    if (a <= l && r <= b) {
      dat[k] = max(dat[k], x);
    } else {
      mark(a, b, k * 2 + 1, l, (l + r) / 2, x);
      mark(a, b, k * 2 + 2, (l + r) / 2, r, x);
    }
  }
};

ll L[MAX], R[MAX], D[MAX], p;
vector<int> G[MAX];

void init(int N) {
  p = 0;
  for (int i = 0; i < N; i++) {
    G[i].clear();
  }
}

void dfs(int v, int depth) {
  L[v] = p++;
  D[v] = depth;
  for (int i = 0; i < (int)G[v].size(); i++) {
    dfs(G[v][i], depth + 1);
  }
  R[v] = p;
}

int main() {
  int N, Q, x;
  while (scanf("%d %d", &N, &Q), N) {
    init(N);
    for (int i = 1; i < N; i++) {
      scanf("%d", &x);
      cin >> x;
      x--;
      G[x].push_back(i);
    }

    dfs(0, 0);

    SegmentTree st(N);

    char m;
    ll v, res = 0;
    while (Q--) {
      cin >> m >> v;
      v--;
      if (m == 'M') {
        st.mark(L[v], R[v], 0, 0, st.n, P(D[v], v + 1));
      } else {
        res += st.get_nearest_marked_node(L[v]);
      }
    }
    printf("%lld\n", res);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define MAX (1 << 17)
#define INF (1LL << 55)
typedef long long ll;
typedef pair<ll, ll> P;

struct SegmentTree {
  int n;
  P dat[2 * MAX - 1];

  SegmentTree(int n_) {
    n = 1;
    while (n < n_) {
      n *= 2;
    }
    for (int i = 0; i < 2 * n - 1; i++) {
      dat[i] = P(0, 1);
    }
  }

  ll get_nearest_marked_node(int k) {
    k += n - 1;
    P p = dat[k];
    while (k > 0) {
      k = (k - 1) / 2;
      p = max(p, dat[k]);
    }
    return p.second;
  }

  void mark(int a, int b, int k, int l, int r, P x) {
    if (r <= a || b <= l)
      return;

    if (a <= l && r <= b) {
      dat[k] = max(dat[k], x);
    } else {
      mark(a, b, k * 2 + 1, l, (l + r) / 2, x);
      mark(a, b, k * 2 + 2, (l + r) / 2, r, x);
    }
  }
};

ll L[MAX], R[MAX], D[MAX], p;
vector<int> G[MAX];

void init(int N) {
  p = 0;
  for (int i = 0; i < N; i++) {
    G[i].clear();
  }
}

void dfs(int v, int depth) {
  L[v] = p++;
  D[v] = depth;
  for (int i = 0; i < (int)G[v].size(); i++) {
    dfs(G[v][i], depth + 1);
  }
  R[v] = p;
}

int main() {
  int N, Q, x;
  while (scanf("%d %d", &N, &Q), N) {
    init(N);
    for (int i = 1; i < N; i++) {
      scanf("%d", &x);
      x--;
      G[x].push_back(i);
    }

    dfs(0, 0);

    SegmentTree st(N);

    char m;
    ll v, res = 0;
    while (Q--) {
      cin >> m >> v;
      v--;
      if (m == 'M') {
        st.mark(L[v], R[v], 0, 0, st.n, P(D[v], v + 1));
      } else {
        res += st.get_nearest_marked_node(L[v]);
      }
    }
    printf("%lld\n", res);
  }
  return 0;
}