1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| int q, n = 0, p[N], fa[N][20], dep[N]; char op[N]; int dis(int x, int y) { if (dep[x] < dep[y]) swap(x, y); int X = x, Y = y; for (int i = 19; i >= 0; i--) { if (fa[X][i] != -1 && dep[fa[X][i]] >= dep[Y]) { X = fa[X][i]; } } if (X == Y) return dep[x] - dep[X]; for (int i = 19; i >= 0; i--) { if (fa[X][i] != -1 && fa[Y][i] != -1 && fa[X][i] != fa[Y][i]) { X = fa[X][i]; Y = fa[Y][i]; } } return dep[x] - dep[X] + dep[y] - dep[Y] + 2; } int rt[N], d[N][2], cnt = -1; void solve() { cin >> q; rep(i, q) { cin >> op[i] >> p[i]; if (p[i] != -1) p[i]--; if (op[i] == 'B') { if (p[i] != -1) dep[n] = dep[p[i]] + 1, rt[n] = rt[p[i]]; else rt[n] = n; fa[n++][0] = p[i]; } } repp(i, 19) { rep(j, n) { if (fa[j][i - 1] == -1) fa[j][i] = -1; else fa[j][i] = fa[fa[j][i - 1]][i - 1]; } } rep(i, q) { if (op[i] == 'B') { ++cnt; if (p[i] == -1) d[cnt][0] = d[cnt][1] = cnt; else { int x = d[rt[p[i]]][0], y = d[rt[p[i]]][1]; if (dis(x, cnt) > dis(d[rt[p[i]]][0], d[rt[p[i]]][1])) { d[rt[p[i]]][0] = x; d[rt[p[i]]][1] = cnt; } if (dis(y, cnt) > dis(d[rt[p[i]]][0], d[rt[p[i]]][1])) { d[rt[p[i]]][0] = y; d[rt[p[i]]][1] = cnt; } } } else { int x = d[rt[p[i]]][0], y = d[rt[p[i]]][1]; cout << max(dis(x, p[i]), dis(y, p[i])) << endl; } } }
|