[CF342E]Xenia and Tree

Description

给定一棵 $n$ 个节点的树,初始时 1 号节点为红色,其余为蓝色。

要求支持如下操作:

  1. 将一个节点变为红色。
  2. 询问节点 $u$ 到最近红色节点的距离。

共 $q$ 次操作。

$$
1 \le n, q \le 10 ^5
$$

Solution

点分树可以做,参考洛咕题解。不过有比这更简单的做法。

有两种形式的暴力:

  1. 对每个询问点,都和在其之前所有的红色点求 LCA 并计算出距离,再取最小值。
  2. 对每个被修改为红色点的点,都在树上 BFS 并更新所有点到红色点的最小值。

这两个暴力各有优劣,如何把它们的优劣点综合起来?

最常用的操作就是分块。

考虑对操作序列分块,设块大小为 $S$。

如何回答询问?

首先对于在这个询问所属块之前的那些整块而言,每块都把所有的修改丢到队列里多源 BFS 一遍,每块复杂度 $O(n)$;

与这个询问在同一个块内的修改点,直接暴力求 LCA 并更新答案即可,复杂度 $O(S ^2 \log n)$。

总复杂度为 $O(n \cdot \frac m S + S ^2 \log n \cdot \frac m S)$,易知 $S$ 取 $\sqrt {\frac n {\log n}}$ 最优,此时总复杂度为 $O(m \cdot \sqrt {n \log n})$。

看起来这个复杂度很悬,实际上还会带上大约 $\frac 1 4$ 的常数,并且树剖的 $\log$ 根本跑不满,所以能轻松的过掉。

类似的分块思想的题今年 APIO 也出了,[APIO2019]桥梁,可以再拿这题练下手。

Code

一遍过了hhh

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
/**********************************************************
* Author : EndSaH
* Email : hjxhb1@gmail.com
* Created Time : 2019-09-14 09:00
* FileName : wib.cpp
* Website : https://endsah.cf
* *******************************************************/

#include <cstdio>
#include <cmath>
#include <cctype>
#include <cstring>
#include <vector>
#include <queue>

#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, flag = 1;
register char ch;
while (!isdigit(ch = getchar()))
if (ch == '-')
flag = -1;
while (x = x * 10 + (ch & 15), isdigit(ch = getchar()));
return x * flag;
}

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

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 = 1e5 + 5;
const int maxM = 1e5 + 5;

int n, m, len;
int dep[maxN], fa[maxN], top[maxN], son[maxN], size[maxN];
int mindis[maxN], dis[maxN];
int opt[maxM], node[maxM], id[maxM];
std::vector<int> G[maxN];
std::queue<int> q;

void DFS1(int u)
{
size[u] = 1;
for (int v : G[u])
{
if (fa[u] == v)
continue;
fa[v] = u, dep[v] = dep[u] + 1;
DFS1(v);
size[u] += size[v];
if (size[son[u]] < size[v])
son[u] = v;
}
}

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

int Dis(int u, int v)
{
int res = dep[u] + dep[v];
while (top[u] != top[v])
{
if (dep[top[u]] < dep[top[v]])
std::swap(u, v);
u = fa[top[u]];
}
if (dep[u] > dep[v])
std::swap(u, v);
return res - (dep[u] << 1);
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("wib.in", "r", stdin);
freopen("wib.out", "w", stdout);
#endif
n = read(), m = read();
len = sqrt(n * log(n) / log(2));
for (int i = 2; i <= n; ++i)
{
int u = read(), v = read();
G[u].push_back(v), G[v].push_back(u);
}
DFS1(1), memcpy(mindis + 1, dep + 1, n * sizeof(int));
top[1] = 1, DFS2(1);
for (int i = 1; i <= m; ++i)
opt[i] = read(), node[i] = read();
id[0] = 1;
for (int i = 1; i <= m; ++i)
id[i] = (i - 1) / len + 1;
for (int i = 1; i <= m; ++i)
{
if (id[i] != id[i - 1])
{
memset(dis + 1, 0x3F, n * sizeof(int));
for (int j = i - len; j < i; ++j)
if (opt[j] == 1)
q.push(node[j]), dis[node[j]] = 0;
while (!q.empty())
{
int u = q.front();
q.pop();
for (int v : G[u]) if (Chkmin(dis[v], dis[u] + 1))
q.push(v);
}
for (int i = 1; i <= n; ++i)
Chkmin(mindis[i], dis[i]);
}
if (opt[i] == 2)
{
int ans = mindis[node[i]];
for (int j = i - 1; j and id[j] == id[i]; --j) if (opt[j] == 1)
Chkmin(ans, Dis(node[j], node[i]));
write(ans), putchar('\n');
}
}
return 0;
}