JZOJ 6176.「GDOI2019」滑稽二乘法

因为身体不舒服所以来写 GDOI 的防 AK 题了(?

首先把 \(1\) 抽出来当根。
\(\def\fa{ {\rm fa} } \fa_k(u)\) 表示 \(u\)\(k\) 级祖先。
\(\def\dep{ {\rm dep} } \dep(u)\) 表示 \(u\) 到根的距离。
\(f_i(u)\) 表示 \(u\) 子树内的黑点与 \(u\) 的距离的 \(i\) 次方和(\(i \in \{0,1,2\}\))。

对于一次询问,设 \(w = {\rm LCA}(u,v)\)
分类讨论所有黑点与 \(\def\Path{ {\rm Path} } \Path(u,v)\) 最近的点为哪个点。

若其在 \(w\) 子树外,则意味着在 \(w\) 处取到距离的最小值。
考虑 \(\fa_k(w)\) 的贡献,则其为 \[ \begin{aligned} f_2(\fa_k(w)) &- (f_2(\fa_{k-1}(w))+2f_1(\fa_{k-1}(w))+f_0(\fa_{k-1}(w))) \\ &+ 2k(f_1(\fa_k(w))-f_1(\fa_{k-1}(w))-f_0(\fa_{k-1}(w))) \\ &+ k^2(f_0(\fa_k(w))-f_0(\fa_{k-1}(w))) \end{aligned} \]

讲人话就是在 \(\fa_k(w)\) 子树内但不在 \(\fa_{k-1}(w)\) 子树内的黑点的贡献。

对于 \(k=1\dots \dep(w)\) 对上式求和可得 \[ \begin{aligned} f_2(1) &+2\dep(w)f_1(1)+\dep^2(w)f_0(1) \\ &-f_2(w)-4\sum\limits_{k=0}^{\dep(w)-1}(f_1(\fa_k(w))+(k+1)f_0(\fa_k(w))) \end{aligned} \]

若其为 \(\Path(u,w)\) 上的点,设 \(s = \dep_u - \dep_w\),则考虑 \(\fa_k(u)\) 的贡献: \[ f_2(\fa_k(u))-(f_2(\fa_{k-1}(u))+2f_1(\fa_{k-1}(u))+f_0(\fa_{k-1}(u))) \]

对于 \(k=0\dots \dep(u)-\dep(w)-1\) 对上式求和得 \[ -\sum\limits_{k=0}^{\dep(u)-\dep(w)-1} (2f_1(\fa_k(u))+f_0(\fa_k(u))) \]

同理可得 \(\Path(v,w)\) 上的贡献,再加上在 \(w\) 子树内但不在 \(u,v\) 子树内的贡献,综合可得 \[ \begin{aligned} f_2(1) &+2\dep(w)f_1(1)+\dep^2(w)f_0(1) \\ &-4\sum\limits_{k=0}^d (f_1(\fa_k(w))+(k+1)f_0(\fa_k(w))) \\ &-\sum\limits_{k=0}^s (2f_1(\fa_k(u))+f_0(\fa_k(u))) \\ &-\sum\limits_{k=0}^t (2f_1(\fa_k(v))+f_0(\fa_k(v))) \end{aligned} \]

其中 \(d = \dep(w)-1,s = \dep(u)-\dep(w)-1,t = \dep(v)-\dep(w)-1\)

到这一步其实已经可以维护了,但是由于我比较懒,所以考虑搞出一个更好维护的形式。
\(g_i(u)\) 表示 \(u\) 子树内黑点 \(\dep\) 值的 \(i\) 次方和。
则显然 \[ f_0(u)=g_0(u),f_1(u)=g_1(u)-\dep(u)g_0(u) \]

代入原式再一顿乱化可得 \[ \begin{aligned} f_2(1) &+2\dep(w)f_1(1)+\dep^2(w)f_0(1) \\ &-4\sum\limits_{k=0}^d g_1(\fa_k(w)) \\ &-4(\dep(w)+1)\sum\limits_{k=0}^d g_0(\fa_k(w)) \\ &+8\sum\limits_{k=0}^d \dep(\fa_k(w)) g_0(\fa_k(w)) \\ &-2\sum\limits_{k=0}^s g_1(\fa_k(u)) \\ &+2\sum\limits_{k=0}^s \dep(\fa_k(u))g_0(\fa_k(u)) \\ &-\sum\limits_{k=0}^s g_0(\fa_k(u)) \\ &-2\sum\limits_{k=0}^t g_1(\fa_k(v)) \\ &+2\sum\limits_{k=0}^t \dep(\fa_k(v))g_0(\fa_k(v)) \\ &-\sum\limits_{k=0}^t g_0(\fa_k(v)) \end{aligned} \]

那么需要支持维护 \(g_0(u),\dep(u)g_0(u),g_1(u)\),比较简单。
JZOJ 卡栈传统艺能。

代码:

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
#pragma GCC optimize("Ofast")
#pragma GCC target("sse3","sse2","sse")
#pragma GCC diagnostic error "-std=c++14"
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#pragma GCC optimize("fast-math","unroll-loops","no-stack-protector","inline")
#include <cstdio>
#include <utility>
#include <algorithm>
using namespace std;
const int N = 1e5;
const int mod = 998244353;
int n,m;
int to[(N << 1) + 5],pre[(N << 1) + 5],first[N + 5];
inline void add(int u,int v)
{
static int tot = 0;
to[++tot] = v,pre[tot] = first[u],first[u] = tot;
}
int col[N + 5];
int sum[3];
int fa[N + 5],dep[N + 5],sz[N + 5],son[N + 5],top[N + 5],id[N + 5],rk[N + 5];
int cur[N + 5];
void dfs1()
{
for(register int p = 1,flag;;)
{
flag = 0;
if(!sz[p])
sz[p] = 1,cur[p] = first[p];
for(register int i = cur[p];i;i = pre[i])
{
cur[p] = pre[i];
if(to[i] ^ fa[p])
{
fa[to[i]] = p,dep[to[i]] = dep[p] + 1;
p = to[i],flag = 1;
break;
}
}
if(!flag)
{
if(!fa[p])
break;
else
sz[fa[p]] += sz[p],
(!son[fa[p]] || sz[son[fa[p]]] < sz[p]) && (son[fa[p]] = p),
p = fa[p];
}
}
}
void dfs2()
{
int tot = 0;
top[1] = 1;
for(register int p = 1,flag;;)
{
flag = 0;
if(!id[p])
{
rk[id[p] = ++tot] = p,cur[p] = first[p];
if(son[p])
{
top[son[p]] = top[p],p = son[p];
continue;
}
}
for(register int i = cur[p];i;i = pre[i])
{
cur[p] = pre[i];
if(to[i] ^ fa[p] && to[i] ^ son[p])
{
top[to[i]] = to[i];
p = to[i],flag = 1;
break;
}
}
if(!flag)
{
if(!fa[p])
break;
else
p = fa[p];
}
}
}
namespace HLD
{
#define ls (p << 1)
#define rs (ls | 1)
struct node
{
int d;
int g0,dg0,g1;
int tag0,tag1;
} seg[(N << 2) + 5];
void build(int p,int tl,int tr)
{
if(tl == tr)
{
seg[p].d = dep[rk[tl]];
return ;
}
int mid = tl + tr >> 1;
build(ls,tl,mid),build(rs,mid + 1,tr);
seg[p].d = (seg[ls].d + seg[rs].d) % mod;
}
void update(int l,int r,int k0,int k1,int p,int tl,int tr)
{
if(l <= tl && tr <= r)
{
seg[p].g0 = (seg[p].g0 + (long long)(tr - tl + 1) * k0) % mod,
seg[p].dg0 = (seg[p].dg0 + (long long)seg[p].d * k0) % mod,
seg[p].g1 = (seg[p].g1 + (long long)(tr - tl + 1) * k1) % mod,
seg[p].tag0 = (seg[p].tag0 + k0) % mod,
seg[p].tag1 = (seg[p].tag1 + k1) % mod;
return ;
}
int mid = tl + tr >> 1;
l <= mid && (update(l,r,k0,k1,ls,tl,mid),1);
r > mid && (update(l,r,k0,k1,rs,mid + 1,tr),1);
seg[p].g0 = (seg[ls].g0 + seg[rs].g0) % mod,
seg[p].g0 = (seg[p].g0 + (long long)(tr - tl + 1) * seg[p].tag0) % mod,
seg[p].dg0 = (seg[ls].dg0 + seg[rs].dg0) % mod,
seg[p].dg0 = (seg[p].dg0 + (long long)seg[p].d * seg[p].tag0) % mod,
seg[p].g1 = (seg[ls].g1 + seg[rs].g1) % mod,
seg[p].g1 = (seg[p].g1 + (long long)(tr - tl + 1) * seg[p].tag1) % mod;
}
typedef pair< pair<int,int>,pair<int,int> > value;
inline value operator+(const value &a,const value &b)
{
return value(make_pair((a.first.first + b.first.first) % mod,(a.first.second + b.first.second) % mod),make_pair((a.second.first + b.second.first) % mod,(a.second.second + b.second.second) % mod));
}
value query(int l,int r,int p,int tl,int tr)
{
if(l > r)
return value(make_pair(0,0),make_pair(0,0));
if(l <= tl && tr <= r)
return value(make_pair(seg[p].g0,seg[p].dg0),make_pair(seg[p].g1,seg[p].d));
int mid = tl + tr >> 1;
value ret(make_pair(0,0),make_pair(0,0));
l <= mid && (ret = ret + query(l,r,ls,tl,mid),1);
r > mid && (ret = ret + query(l,r,rs,mid + 1,tr),1);
ret = ret + value(make_pair((long long)(min(r,tr) - max(l,tl) + 1) * seg[p].tag0 % mod,(long long)ret.second.second * seg[p].tag0 % mod),make_pair((long long)(min(r,tr) - max(l,tl) + 1) * seg[p].tag1 % mod,0));
return ret;
}
int getlca(int x,int y)
{
while(top[x] ^ top[y])
dep[top[x]] > dep[top[y]] ? (x = fa[top[x]]) : (y = fa[top[y]]);
return dep[x] < dep[y] ? x : y;
}
void update(int p,int k)
{
sum[0] = (sum[0] + k) % mod,
sum[1] = (sum[1] + (long long)k * dep[p]) % mod,
sum[2] = (sum[2] + (long long)k * dep[p] % mod * dep[p]) % mod;
for(register int x = p;top[x];x = fa[top[x]])
update(id[top[x]],id[x],k,(long long)k * dep[p] % mod,1,1,n);
}
value query(int p,int u)
{
value ret(make_pair(0,0),make_pair(0,0));
if(dep[p] <= dep[u])
return ret;
for(;top[p] ^ top[u];p = fa[top[p]])
ret = ret + query(id[top[p]],id[p],1,1,n);
ret = ret + query(id[u] + 1,id[p],1,1,n);
return ret;
}
}
int ans;
int main()
{
freopen("regress.in","r",stdin),freopen("regress.out","w",stdout);
scanf("%d",&n);
int u,v;
for(register int i = 1;i < n;++i)
scanf("%d%d",&u,&v),add(u,v),add(v,u);
dfs1(),dfs2(),HLD::build(1,1,n);
int op,w;
HLD::value t;
for(scanf("%d",&m);m;--m)
{
scanf("%d%d",&op,&u);
if(!op)
HLD::update(u,col[u] ? mod - 1 : 1),col[u] ^= 1;
else
{
scanf("%d",&v),w = HLD::getlca(u,v);
ans = (sum[2] + 2LL * dep[w] * sum[1] + (long long)dep[w] * dep[w] % mod * sum[0]) % mod,
ans = (mod - ans) % mod;
t = HLD::query(w,1),
ans = (ans + 4LL * (dep[w] + 1) % mod * t.first.first) % mod,
ans = (ans - 8LL * t.first.second + 8LL * mod) % mod,
ans = (ans + 4LL * t.second.first) % mod;
t = HLD::query(u,w),
ans = (ans + t.first.first) % mod,
ans = (ans - 2LL * t.first.second + 2LL * mod) % mod,
ans = (ans + 2LL * t.second.first) % mod;
t = HLD::query(v,w),
ans = (ans + t.first.first) % mod,
ans = (ans - 2LL * t.first.second + 2LL * mod) % mod,
ans = (ans + 2LL * t.second.first) % mod;
ans = (mod - ans) % mod;
printf("%d\n",ans);
}
}
}