[CF1017G]The Tree

Description

$$
1 \le n, m \le 2 \times 10 ^5
$$

Solution

暴力染色不太好维护。考虑如何查询一个节点是否被染色,如果把 1 这个染色操作视作单点加 1,那么如果存在一个祖先(包含 $x$),其到 $x$ 的路径权值和大于等于路径长度,那么 $x$ 便被染色。

于是便有了一个思路:把路径长度设为负的,也就是初始把所有点权视为 -1,然后查询就直接查询根到 $x$ 的所有后缀和的最大值是否 $\ge 0$ 即可。

(前缀指根到 $x$,后缀指 $x$ 到根)

树直接想怎么维护所有后缀和的最大值可能不直观,丢到序列上就很好想:一个区间的后缀最大值为 左区间的后缀最大值 + 右区间权值和,右区间后缀最大值 这两个值中大的那个。于是线段树维护,查询直接对那 $\log$ 个区间按上面这个方法从右往左合并。

在树上也是一样的,树剖提区间。因为是从 $x$ 往根跳,所以区间默认从右往左,直接合并就行。

考虑怎样在当前这个思路下正确维护 2 操作。首先肯定得区间覆盖为 -1,还得让覆盖操作之后子树内所有节点到根的后缀和最大值为 -1,而子树内所有节点的后缀路径要么不包含 $x$,此时肯定后缀最大值是他自己到他自己的路径,值为 -1;要么包含 $x$,这个有可能不对(因为有上面节点的影响),所以直接在覆盖之后查 $x$ 的后缀和最大值,给 $x$ 单点加这个 -(查到的值 + 1) 即可,这样就能确保所有节点到根的后缀和最大值为 -1 了。

一定得注意合并顺序。

Code

指针警告:D

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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**********************************************************
* Author : EndSaH
* Email : hjxhb1@gmail.com
* Created Time : 2019-08-16 14:21
* FileName : bigbro.cpp
* Website : https://endsah.cf
* *******************************************************/

#include <cstdio>
#include <cctype>
#include <vector>

typedef std::pair<int, int> pii;

#define fir first
#define sec second
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(s) debug("The message in line %d, Function %s: %s\n", __LINE__, __FUNCTION__, s)
#define getchar() (ipos == iend and (iend = (ipos = _ibuf) + fread(_ibuf, 1, __bufsize, stdin), ipos == iend) ? EOF : *ipos++)
#define putchar(ch) (opos == oend ? fwrite(_obuf, 1, __bufsize, stdout), opos = _obuf : 0, *opos++ = (ch))
#define __bufsize (1 << 21 | 1)

char _ibuf[__bufsize], _obuf[__bufsize], _stk[50];
char *ipos = _ibuf, *iend = _ibuf, *opos = _obuf, *oend = _obuf + __bufsize, *stkpos = _stk;

struct END
{ ~END() { fwrite(_obuf, 1, opos - _obuf, stdout); } }
__;

inline int read()
{
register int x = 0;
register char ch;
while (!isdigit(ch = getchar()));
while (x = (x << 3) + (x << 1) + (ch & 15), isdigit(ch = getchar()));
return x;
}

template <typename _INT>
inline void write(_INT x)
{
while (*++stkpos = x % 10 ^ 48, x /= 10, x);
while (stkpos != _stk)
putchar(*stkpos--);
}

inline void Puts(const char* str)
{
while (*str)
putchar(*str++);
putchar('\n');
}

template <typename _Tp>
inline bool Chkmax(_Tp& x, const _Tp& y)
{ return x < y ? x = y, true : false; }

template <typename _Tp>
inline bool Chkmin(_Tp& x, const _Tp& y)
{ return x > y ? x = y, true : false; }

const int maxN = 2e5 + 2;

int n, m, dfst;
int fa[maxN], top[maxN], son[maxN], dfn[maxN], size[maxN];
std::vector<int> G[maxN];

inline pii Merge(pii l, pii r)
{ return pii(l.fir + r.fir, std::max(r.sec, l.sec + r.fir)); }

namespace SEG
{
typedef struct Node* ptr;

ptr root;

struct Node
{
int l, r;
pii info;
bool tag;
ptr lson, rson;

Node() { }

Node(int _l, int _r) : l(_l), r(_r)
{ this->tag = false; }

void Cover()
{ this->tag = true, this->info = pii(this->l - this->r - 1, -1); }

void Pushup()
{ this->info = Merge(this->lson->info, this->rson->info); }

void Pushdown()
{
if (this->tag)
this->lson->Cover(), this->rson->Cover(), this->tag = false;
}
};

inline ptr New(int l, int r)
{
static ptr mempool = new Node[n << 2];
return new(mempool++) Node(l, r);
}

void Build(int l, int r, ptr& cur = root)
{
cur = New(l, r);
if (l == r)
{
cur->info = pii(-1, -1);
return;
}
int mid = (l + r) >> 1;
Build(l, mid, cur->lson), Build(mid + 1, r, cur->rson);
cur->Pushup();
}

void Add(int pos, int addval, ptr cur = root)
{
if (cur->l == cur->r)
{
cur->info = pii(cur->info.fir + addval, cur->info.sec + addval);
return;
}
cur->Pushdown();
int mid = (cur->l + cur->r) >> 1;
if (pos <= mid)
Add(pos, addval, cur->lson);
else
Add(pos, addval, cur->rson);
cur->Pushup();
}

pii Query(int ql, int qr, ptr cur = root)
{
if (ql <= cur->l and cur->r <= qr)
return cur->info;
cur->Pushdown();
pii res(0, -1e9);
int mid = (cur->l + cur->r) >> 1;
if (mid < qr)
res = Merge(Query(ql, qr, cur->rson), res);
if (ql <= mid)
res = Merge(Query(ql, qr, cur->lson), res);
return res;
}

void Cover(int ql, int qr, ptr cur = root)
{
if (ql <= cur->l and cur->r <= qr)
{
cur->Cover();
return;
}
if (ql > cur->r or qr < cur->l)
return;
cur->Pushdown();
Cover(ql, qr, cur->lson), Cover(ql, qr, cur->rson);
cur->Pushup();
}
}

void DFS1(int u)
{
size[u] = 1;
for (int v : G[u]) if (v != fa[u])
{
DFS1(v);
size[u] += size[v];
if (size[v] > size[son[u]])
son[u] = v;
}
}

void DFS2(int u)
{
dfn[u] = ++dfst;
if (son[u])
top[son[u]] = top[u], DFS2(son[u]);
for (int v : G[u]) if (!top[v])
top[v] = v, DFS2(v);
}

inline int Query(int x)
{
pii res(0, -1e9);
while (top[x] != 1)
{
res = Merge(SEG::Query(dfn[top[x]], dfn[x]), res);
x = fa[top[x]];
}
res = Merge(SEG::Query(1, dfn[x]), res);
return res.sec;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("bigbro.in", "r", stdin);
freopen("bigbro.out", "w", stdout);
#endif
n = read(), m = read();
for (int i = 2; i <= n; ++i)
G[fa[i] = read()].push_back(i);
DFS1(1);
top[1] = 1, DFS2(1);
SEG::Build(1, n);
while (m--)
{
int opt = read(), x = read(), val;
switch (opt)
{
case 1 :
SEG::Add(dfn[x], 1);
break;
case 2 :
SEG::Cover(dfn[x], dfn[x] + size[x] - 1);
SEG::Add(dfn[x], -Query(x) - 1);
break;
case 3 :
Puts(Query(x) >= 0 ? "Yes" : "No");
break;
}
}
return 0;
}