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

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int MAXN = 100010;

int f[MAXN], mark[MAXN];
P Q[MAXN];

int getf(int k, int id) {
  return mark[k] < id ? k : f[k] = getf(f[k], id);
  /*如果k?点被??的??早于????，就返回k。否?????k的父?点，
  且因???路径上有一些?点未被??或者???期，可以路径??,直接?被
  ???点到最近???点路径上的所有?点都指向最近???点*/
}

int main() {
  int n, q;
  while (~scanf("%d %d", &n, &q), n + q) {
    for (int i = 2; i <= n; i++) {
      scanf("%d", &f[i]);
      mark[i] = inf;
    }
    int cnt = 0;
    char c;
    int x;
    for (int i = 1; i <= q; i++) {
      getchar();
      scanf("%c %d", &c, &x);
      if (c == 'Q')
        Q[cnt++] = P(i, x); // P(i,x)表示在i?刻??x的最近??点
      else
        mark[x] =
            min(mark[x], i); // mark[i]表示i?点第一次被??的??，inf表示未被??
    }
    ll ans = 0;
    for (int i = cnt - 1; i >= 0; i--)
      ans += getf(Q[i].second, Q[i].first);
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: WA

Fixed submission:


#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
const int maxn = 1e5 + 5;
typedef long long ll;
#define mid (l + r) / 2
#define lc (rt << 1)
#define rc (rt << 1 | 1)

vector<int> mp[maxn];
int f[maxn]; // f[1] = 0
int in[maxn], out[maxn];
int te = 0;
struct tr {
  struct node {
    int v, set;
  } seg[maxn << 2]; // 就是不知道怎?pushup
  void init(int l, int r, int rt) {
    if (l == r) {
      seg[rt].v = 1;
      seg[rt].set = 0;
      return;
    }
    init(l, mid, lc);
    init(mid + 1, r, rc);
    seg[rt].v = 1;
    seg[rt].set = 0; // 反正?点??。。。?不重要?？？？
  }
  int ql, qr;
  int query(int pos, int l, int r, int rt) //?点??
  {
    if (l == r) {
      return seg[rt].v;
    }
    if (pos <= mid)
      return query(pos, l, mid, lc);
    else
      return query(pos, mid + 1, r, rc);
  }
  void pushdown(int l, int r, int rt) {
    if (seg[rt].set) {
      seg[lc].set = seg[rt].set;
      seg[rc].set = seg[rt].set;
      seg[lc].v = seg[rc].v = seg[rt].set;
      seg[rt].set = 0;
    }
  }
  void update(int l, int r, int rt,
              int val) // 区?修改//当?点是叶子的?候，他不??修改rt的v，
  {
    if (ql > qr)
      return;
    if (ql <= l && r <= qr) {
      seg[rt].set = seg[rt].v = val;
      return;
    }
    pushdown(l, r, rt);
    if (ql <= mid)
      update(l, mid, lc, val);
    if (mid < qr)
      update(mid + 1, r, rc, val);
    seg[rt].v = seg[lc].v;
    // no pushup?
  }
} tree;
void init(int n) {
  for (int i = 1; i <= n; i++) {
    mp[i].clear();
  }
  memset(in, 0, sizeof(int) * (n + 1));
  memset(out, 0, sizeof(int) * (n + 1));
}
void dfs(int u) {
  in[u] = ++te;
  for (int i = 0; i < mp[u].size(); i++) {
    dfs(mp[u][i]);
  }
  out[u] = te;
}
int main() {
  int n, q;
  while (scanf("%d%d", &n, &q) != EOF) {
    if (n == 0 && q == 0)
      break;
    init(n);
    int tmp;
    for (int i = 2; i <= n; i++) {
      scanf("%d", &tmp);
      mp[tmp].push_back(i);
      f[i] = tmp;
    }
    getchar();
    dfs(1);
    tree.init(1, n, 1);
    tree.seg[1].v = 0; // make query(1) = 0

    char op;
    int v;
    ll sum = 0;
    for (int i = 0; i < q; i++) {
      scanf("%c%d%*c", &op, &v);
      if (op == 'M') {
        tree.ql = in[v] + 1;
        tree.qr = out[v];
        tree.update(1, maxn, 1, v);
      } else {
        sum += tree.query(in[v], 1, maxn, 1);
      }
    }
    printf("%lld\\n", sum);
  }
  return 0;
}