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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| #include <cstdio> #include <vector>
struct Node { std::vector<struct Edge> e; struct Chain *chain; int size, dfn, depth; Node *fa, *ch; };
struct Edge { Node *s, *t;
Edge(Node *s, Node *t) : s(s), t(t) {} };
struct Chain { Node *top;
Chain(Node *top) : top(top) {} };
inline void addEdge(Node *u, Node *v) { u->e.push_back(Edge(u, v)); v->e.push_back(Edge(v, u)); }
void dfs1(Node *v, Node *fa = nullptr) { v->size = 1;
for (Edge &e : v->e) { if (e.t == fa) continue; e.t->fa = v; e.t->depth = v->depth + 1; dfs1(e.t, v); v->size += e.t->size; if (!v->ch || v->ch->size < e.t->size) v->ch = e.t; } }
void dfs2(Node *v) { static int ts = 0; v->dfn = ++ts;
if (!v->fa || v != v->fa->ch) v->chain = new Chain(v); else v->chain = v->fa->chain;
if (v->ch) dfs2(v->ch); for (Edge &e : v->e) { if (e.t->fa == v && e.t != v->ch) { dfs2(e.t); } } }
inline void split(Node *v) { v->depth = 1; dfs1(v); dfs2(v); }
struct SegT { int l, r; SegT *lc, *rc; int val, tag;
SegT(int l, int r, SegT *lc, SegT *rc) : l(l), r(r), lc(lc), rc(rc), val(0), tag(-1) {}
void cover(const int delta) { if (delta == -1) return; val = delta == 1 ? r - l + 1 : 0; tag = delta == 1 ? 1 : 0; }
void pushDown() { if (tag != -1) { lc->cover(tag); rc->cover(tag); tag = -1; } }
void update(const int l, const int r, const int delta) { if (l > this->r || r < this->l) return; else if (l <= this->l && r >= this->r) cover(delta); else { pushDown(); lc->update(l, r, delta); rc->update(l, r, delta); val = lc->val + rc->val; } }
static SegT *build(const int l, const int r) { if (l > r) return nullptr; else if (l == r) return new SegT(l, r, nullptr, nullptr); else { const int mid = l + (r - l) / 2; return new SegT(l, r, build(l, mid), build(mid + 1, r)); } } } *segment;
inline void update(Node *u, Node *v, int w) { while (u->chain != v->chain) { if (u->chain->top->depth < v->chain->top->depth) std::swap(u, v); segment->update(u->chain->top->dfn, u->dfn, w); u = u->chain->top->fa; }
if (u->depth > v->depth) std::swap(u, v); segment->update(u->dfn, v->dfn, w); }
int main() { int n; scanf("%d", &n);
std::vector<Node> nodes(n); for (int i = 1; i < n; i++) { int p; scanf("%d", &p); addEdge(&nodes[p], &nodes[i]); }
split(&nodes[0]); segment = SegT::build(1, n);
int q; scanf("%d", &q); while (q--) { char op[sizeof("uninstall")]; int x; scanf("%s %d", op, &x);
int bef = segment->val;
if (op[0] == 'i') { update(&nodes[0], &nodes[x], 1); printf("%d\n", segment->val - bef); } else if (op[0] == 'u') { segment->update(nodes[x].dfn, nodes[x].dfn + nodes[x].size - 1, 0); printf("%d\n", bef - segment->val); } }
return 0; }
|