Problem p01288 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;
const int maxn = 1e5 + 100;
const int INF = 0x3f3f3f3f;
#define pr(x)   // cout << #x << " = " << x << " ";
#define prln(x) // cout << #x << " = " << x <<endl;
#define ll long long
int head[maxn], nxt[maxn], to[maxn], dfsn, cnt, id[maxn], l[maxn], r[maxn],
    p[maxn], _n, sum[maxn << 2], dep[maxn];
void addedge(int u, int v) {
  nxt[cnt] = head[u];
  head[u] = cnt;
  to[cnt++] = v;
}
void init(int n) {
  cnt = dfsn = 0;
  _n = 1;
  while (_n < n)
    _n = _n * 2;
  int _nn = _n * 2;
  for (int i = 0; i <= _nn; ++i)
    sum[i] = -1;
  for (int i = 0; i <= n; ++i) {
    head[i] = -1;
    dep[i] = 0;
  }
}

void dfstree(int fa, int u) {
  id[u] = ++dfsn;
  dep[u] = dep[fa] + 1;
  for (int i = head[u]; ~i; i = nxt[i]) {
    dfstree(u, to[i]);
  }
  l[u] = id[u];
  r[u] = dfsn;
  pr(fa);
  pr(u);
  pr(dep[fa]);
  prln(dep[u]);
}
inline void getans(int &ans, const int &v2) {
  if (ans == -1 || dep[ans] < dep[v2])
    ans = v2;
}
void pushdown(int rt) {
  if (sum[rt] != -1) {
    getans(sum[rt << 1], sum[rt]);
    getans(sum[rt << 1 | 1], sum[rt]);
  }
}
void update(int rt, int l, int r, int ql, int qr, int v) {
  if (ql <= l && r <= qr) {
    getans(sum[rt], v);
    pr(sum[rt]);
    pr(dep[sum[rt]]);
    pr(v);
    prln(dep[v]);
    return;
  }
  pushdown(rt);
  int m = l + r >> 1;
  if (m >= ql)
    update(rt << 1, l, m, ql, qr, v);
  if (m < qr)
    update(rt << 1 | 1, m + 1, r, ql, qr, v);
}
int query(int rt) {
  rt += _n - 1;
  int ans, depth = -1;
  while (rt >= 1) {
    if (sum[rt] != -1) {
      if (dep[sum[rt]] > depth) {
        depth = dep[sum[rt]];
        ans = sum[rt];
      }
    }
    rt = rt >> 1;
  }
  return ans;
}
int main() {
#ifdef LOCAL
  freopen("C:\\\\Users\\\\Administrator\\\\Desktop\\\\in.txt", "r", stdin);
  // freopen("C:\\\\Users\\\\Administrator\\\\Desktop\\\\out.txt","w",stdout);
#endif
  int n, m, u, x, test = 0;
  char op[10];
  while (cin >> n >> m && (n || m)) {
    ll ans = 0;
    init(n);
    for (int i = 2; i <= n; ++i) {
      scanf("%d", &u);
      addedge(u, i);
    }
    // prln(test++);
    dep[0] = 0;
    dfstree(0, 1);
    pr(id[1]);
    prln(dfsn);
    update(1, 1, _n, l[1], r[1], 1);
    prln(query(id[1]));
    // prln(test++);
    for (int i = 1; i <= n; ++i) {
      pr(i);
      pr(dep[i]);
      prln(id[i]);
    }
    for (int i = 0; i < m; ++i) {
      scanf("%s%d", op, &x);
      if (op[0] == 'M') {
        pr(x);
        pr(id[x]);
        prln(r[x]);
        // sum[id[x]+_n-1] = x;
        update(1, 1, _n, id[x], r[x], x);
        prln(test++);
      } else {
        ans += query(id[x]);
        pr(x);
        pr(id[x]);
        prln(query(id[x]));
        prln(ans);
        // prln(test++);
      }
    }
    prln(test++);
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define INF 1000000000
#define M 100005
#define LL long long
using namespace std;
struct node {
  int x, y;
} A[M], Q[M];
int fa[M];
bool mark[M];
int edge[M];
int get(int x) {
  if (fa[x] == x)
    return x;
  else
    return fa[x] = get(fa[x]);
}
int get_ans(int x) {
  x = get(x);
  while (!mark[x]) {
    if (x == 0)
      return -1;
    fa[x] = edge[x];
    x = edge[x];
    x = get(x);
  }
  return x;
}
int readin() {
  int res = 0;
  char a;
  while (a = getchar(), !isdigit(a))
    ;
  do {
    res = (res << 3) + (res << 1) + (a ^ 48);
  } while (a = getchar(), isdigit(a));
  return res;
}
int main() {
  int n, q, j, k;
  while (scanf("%d%d", &n, &q) != EOF) {
    if (n == 0 && q == 0)
      break;
    memset(mark, 0, sizeof(mark));
    mark[1] = 1;
    edge[1] = 1;
    for (j = 1; j <= n; j++)
      fa[j] = j;
    for (j = 2; j <= n; j++) {
      int a;
      a = readin();
      edge[j] = a;
    }
    char s[2];
    for (j = 1; j <= q; j++) {
      scanf("%s", s);
      Q[j].y = readin();
      if (s[0] == 'Q')
        Q[j].x = 0;
      else {
        mark[Q[j].y] = 1;
        Q[j].x = 1;
      }
    }
    long long ans = 0;
    for (j = q; j >= 1; j--) {
      if (Q[j].x == 0) {
        int t = get_ans(Q[j].y);
        ans += t;
      } else
        mark[Q[j].y] = 0;
    }
    cout << ans << endl;
  }
  return 0;
}