[CF1172E]Nauuo and ODT

Description

给定一棵 $n$ 个点的树,每个节点有颜色。求
$$
\sum _{i = 1} ^n \sum _{j = 1} ^n f(i, j)
$$
其中 $f(i, j)$ 表 $i$ 到 $j$ 路径上的颜色种数。

有 $m$ 次修改,对于初始状态和每次修改都需回答上式结果。

修改之间互有影响。
$$
1 \le n, m \le 2 \times 10 ^5 \\
colcnt \le n
$$

2019-08-14

CF1172E Nauuo and ODT

Solution

首先看 $m = 0$,即没有修改怎么做。

可以暴力 $n ^2$ 找到所有路径并统计,于是有一个自然的思路:固定一个端点然后统计答案。

但实际上这个做法并没有什么前途,所以只能换一种考虑方式:计算每种颜色对答案的贡献。

显然贡献为至少经过一个这个颜色的点的路径条数。

这等价于所有路径($n ^2$)减去不经过这个颜色的点的路径条数($\sum size ^2$),其中 $size$ 为去掉这个颜色的点后,树上的联通块的个数。

在树上把这种颜色的点视作白色,不是这种颜色的点视作黑色,问题变为求所有黑色联通块 $size ^2$ 之和。

看起来很有前途!

然后你会发现这个做法貌似 $m = 0$ 都做不了。

但实际上,对于这个思路来说,初始状态是操作了 $n$ 次的状态,所以 $m = 0$ 这个东西不重要。

于是可以继续深入思考,首先修改颜色对某种颜色要么不影响,要么是一次 黑->白 或者 白->黑。因为要统计黑色联通块 $size ^2$,带修+树上问题,LCT 没跑了。

LCT 只能断边连边,所以可以将所有黑点向原树父亲连边而白点不连(定 1 为根,1 的父亲视作一个虚点,比如 $n + 1$),翻转颜色就是断/连边过程了。刚好这样做也利于答案统计,对于白点,它的所有子节点都是黑点,统计所有白点虚子树大小平方和就行。这个用 LCT 可以维护。

官方题解里有详解图

但是不能对每个颜色都开 LCT 算,复杂度会炸。

可以发现总共就 $n + m$ 次操作,于是把 $m$ 次修改离线下来,一开始初始化一棵全是黑点的 LCT(即所有点向原树父亲连边,1 向 $n + 1$ 连边),对每个颜色都进行那些影响了这个颜色的操作,最后再倒着改回来就行了。这样修改总次数就是 $\cal O(n + m)$ 的,复杂度才是对的。

对于每次操作算出来的答案会一直贡献到下一次操作为止,于是记录下每个操作在长度为 $n + m$ 的操作序列上的位置,差分统计就可以了。

$\cal O((n + m) \log n)$

因为 LCT 的 Rotate 错了调了一年… 板子不敲熟真的太致命了…

1
2
3
if (~curfa->Ident())
// curfa->fa->son[this->Ident()] = this;
curfa->fa->son[curfa->Ident()] = this;

Code

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
/**********************************************************
* Author : EndSaH
* Email : hjxhb1@gmail.com
* Created Time : 2019-08-15 19:03
* FileName : color.cpp
* Website : https://endsah.cf
* *******************************************************/

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

typedef long long LL;
typedef std::pair<int, int> pii;
typedef struct Node* ptr;

#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, flag = 1;
register char ch;
while (!isdigit(ch = getchar()))
if (ch == '-')
flag = -1;
while (x = (x << 3) + (x << 1) + (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 = 2e5 + 2;

int n, m;
LL ans;
int col[maxN], fa[maxN];
LL delta[maxN << 1];
ptr root;
std::vector<int> G[maxN];
std::vector<pii> modi[maxN];

struct Node
{
int size, virt;
LL vir_2;
ptr son[2], fa;

Node() : size(1), virt(0), vir_2(0)
{ son[0] = son[1] = fa = nullptr; }

int Ident()
{ return !this->fa ? -1 : this->fa->son[0] == this ? 0 : this->fa->son[1] == this ? 1 : -1; }

static int Size(ptr x)
{ return x ? x->size : 0; }

void Pushup()
{ this->size = Size(this->son[0]) + Size(this->son[1]) + this->virt + 1; }

void Rotate() // attention! this->Pushup() disappear!
{
ptr curfa = this->fa;
int dir = this->Ident();
if (~curfa->Ident())
curfa->fa->son[curfa->Ident()] = this;
if (this->son[dir ^ 1])
this->son[dir ^ 1]->fa = curfa;
curfa->son[dir] = this->son[dir ^ 1], this->son[dir ^ 1] = curfa;
this->fa = curfa->fa, curfa->fa = this;
curfa->Pushup();
}

void Splay()
{
ptr curfa;
while (~this->Ident())
{
curfa = this->fa;
if (~curfa->Ident())
curfa->Ident() ^ this->Ident() ? this->Rotate() : curfa->Rotate();
this->Rotate();
}
this->Pushup();
}
};

inline void Access(ptr cur)
{
for (ptr son = nullptr; cur; son = cur, cur = cur->fa)
{
cur->Splay();
(cur->virt += Node::Size(cur->son[1])) -= Node::Size(son);
cur->vir_2 += (LL)Node::Size(cur->son[1]) * Node::Size(cur->son[1]);
cur->vir_2 -= (LL)Node::Size(son) * Node::Size(son);
cur->son[1] = son;
}
}

inline ptr Findroot(ptr cur)
{
Access(cur), cur->Splay();
while (cur->son[0])
cur = cur->son[0];
cur->Splay();
return cur;
}

inline void Link(int u)
{
ptr cur = root + u, curfa = root + fa[u];
Access(cur), ans += cur->vir_2;
ptr currt = Findroot(curfa);
Access(currt), ans += currt->vir_2;
Access(curfa), curfa->Splay();
cur->fa = curfa;
curfa->virt += cur->size;
curfa->vir_2 += (LL)cur->size * cur->size;
curfa->size += cur->size;
Access(currt), ans -= currt->vir_2;
}

inline void Cut(int u)
{
ptr cur = root + u, curfa = root + fa[u], currt = Findroot(cur);
Access(currt), ans += currt->vir_2;
Access(curfa), curfa->Splay();
curfa->virt -= cur->size;
curfa->vir_2 -= (LL)cur->size * cur->size;
curfa->size -= cur->size;
cur->fa = nullptr;
Access(cur), ans -= cur->vir_2;
Access(currt), ans -= currt->vir_2;
}

void DFS(int u)
{
for (int v : G[u])
if (v != fa[u])
fa[v] = u, DFS(v);
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("color.in", "r", stdin);
freopen("color.out", "w", stdout);
#endif
n = read(), m = read();
root = new Node[n + 2];
for (int i = 1; i <= n; ++i)
modi[col[i] = read()].push_back(pii(i, i));
for (int i = 2; i <= n; ++i)
{
int u = read(), v = read();
G[u].push_back(v), G[v].push_back(u);
}
fa[1] = n + 1, DFS(1);
for (int i = 1; i <= n; ++i)
Link(i);
for (int i = 1; i <= m; ++i)
{
int u = read(), x = read();
modi[col[u]].push_back(pii(u, -i - n));
modi[x].push_back(pii(u, i + n));
col[u] = x;
}
ans = 0;
for (int i = 1; i <= n; ++i) // - W->B + B->W
{
for (int j = 0; j < modi[i].size(); ++j)
{
int u = modi[i][j].fir, t = modi[i][j].sec;
if (t < 0)
Link(u);
else
Cut(u);
if (j != modi[i].size() - 1)
delta[std::abs(t)] += ans, delta[std::abs(modi[i][j + 1].sec)] -= ans;
else
delta[std::abs(t)] += ans;
}
for (int j = modi[i].size() - 1; ~j; --j)
{
int u = modi[i][j].fir, t = modi[i][j].sec;
if (t < 0)
Cut(u);
else
Link(u);
}
}
ans = 0;
for (int i = 1; i <= n; ++i)
ans += delta[i];
write(ans), putchar('\n');
for (int i = 1; i <= m; ++i)
write(ans += delta[i + n]), putchar('\n');
return 0;
}