「洛谷2146」软件包管理器

zcy会写树剖啦!

本题为树链剖分的模板题

对于”install x”操作, 将$x$到根节点路径上所有点的点权全部赋值为$1$

对于”uninstall x”操作, 将$x$及$x$的子树点权全部赋值为$0$​

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// luogu-judger-enable-o2
#include <bits/stdc++.h>
using namespace std;

const int MaxN = 150010;

struct edge
{
int to, next;
};

struct node
{
int l, r;
int sum, tag;
};

struct SegmentTree
{
node t[MaxN << 1];
inline void pushup(int id) { t[id].sum = t[id << 1].sum + t[id << 1 | 1].sum; }
void build(int id, int l, int r)
{
t[id].l = l, t[id].r = r, t[id].tag = -1;
if (l == r)
return;
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
}
inline void pushdown(int id)
{
if (t[id].tag != -1)
{
t[id << 1].sum = t[id].tag * (t[id << 1].r - t[id << 1].l + 1);
t[id << 1 | 1].sum = t[id].tag * (t[id << 1 | 1].r - t[id << 1 | 1].l + 1);

t[id << 1].tag = t[id].tag;
t[id << 1 | 1].tag = t[id].tag;

t[id].tag = -1;
}
}
inline void modify(int id, int l, int r, int delta)
{
if (l > t[id].r || t[id].l > r)
return;
if (l <= t[id].l && t[id].r <= r)
{
t[id].sum = delta * (t[id].r - t[id].l + 1);
t[id].tag = delta;
return;
}
if (t[id].l == t[id].r)
return;
pushdown(id);
modify(id << 1, l, r, delta);
modify(id << 1 | 1, l, r, delta);
pushup(id);
return;
}
inline int query(int id, int l, int r)
{
if (l > t[id].r || t[id].l > r)
return 0;
if (l <= t[id].l && t[id].r <= r)
return t[id].sum;
if (t[id].l == t[id].r)
return 0;
pushdown(id);
return query(id << 1, l, r) + query(id << 1 | 1, l, r);
}
} T;

edge e[MaxN << 1];
int n, m, cnt, dfsnum, size[MaxN], hson[MaxN];
int head[MaxN], top[MaxN], dfn[MaxN], fa[MaxN], dep[MaxN];

inline void add_edge(int u, int v)
{
++cnt;
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt;
}

inline void dfs1(int u, int f)
{
size[u] = 1;
for (int i = head[u]; i; i = e[i].next)
{
int v = e[i].to;
if (v == f)
continue;
dep[v] = dep[u] + 1;
fa[v] = u;
dfs1(v, u);
size[u] += size[v];
if (size[v] > size[hson[u]])
hson[u] = v;
}
}

inline void dfs2(int u, int Top)
{
++dfsnum;
dfn[u] = dfsnum;
top[u] = Top;
if (hson[u])
dfs2(hson[u], Top);
for (int i = head[u]; i; i = e[i].next)
{
int v = e[i].to;
if (v == hson[u] || v == fa[u])
continue;
dfs2(v, v);
}
}

inline int read()
{
int x = 0;
char ch = getchar();
while (ch > '9' || ch < '0')
ch = getchar();
while (ch <= '9' && ch >= '0')
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return x;
}

inline void update_tree(int u)
{
T.modify(1, dfn[u], dfn[u] + size[u] - 1, 0);
}

inline void update_chain(int u, int v, int delta)
{
while (top[u] != top[v])
{
if (dep[top[u]] < dep[top[v]])
swap(u, v);
T.modify(1, dfn[top[u]], dfn[u], delta);
u = fa[top[u]];
}
if (dep[u] < dep[v])
swap(u, v);
T.modify(1, dfn[v], dfn[u], delta);
}

signed main()
{
n = read();
for (int i = 2; i <= n; ++i)
{
int u = read() + 1;
add_edge(i, u);
add_edge(u, i);
}
dep[1] = 1, fa[1] = 0;
dfs1(1, 0), dfs2(1, 1);
T.build(1, 1, n);
m = read();
for (int i = 1; i <= m; i++)
{
string op;
cin >> op;
int before = T.t[1].sum;
if (op == "install")
{
int u = read() + 1;
update_chain(u, 1, 1);
int after = T.t[1].sum;
printf("%d\n", after - before);
}
else
{
int u = read() + 1;
update_tree(u);
int after = T.t[1].sum;
printf("%d\n", before - after);
}
}
return 0;
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×