「BZOJ 5020」在美妙的数学王国中畅游-Link-Cut-Tree

维护一个森林,每个点有一个函数 $f$,要求支持连接两个点,断开两个点,修改某个点的函数,询问路径上函数 $f(x)$ 的和。

链接

BZOJ 5020

题解

考虑麦克劳林展开

$$f(x)=\sum_{n = 0} ^ {\infty}f ^ {(n)} (0) \frac{x ^ n}{n!}$$

于是我们只需要对每个点维护前 $k$ 项的系数就可以了,$k$ 并不会很大,大概 $11$ 就可以保证精度。

对于 $f(x) = \sin(ax + b)$ 时

$$\begin{aligned}f'(0) &= a \cdot \cos(b) \\ f ^ {(2)}(0) &= - a ^ 2 \cdot \sin(b) \\ f ^ {(3)}(0) &= - a ^ 3 \cdot \cos(b) \\ f ^ {(4)}(0) &= a ^ 4 \cdot \sin(b)\end{aligned}$$

至此已经出现循环,后面的直接递推就好了。

对于 $f(x) = e ^ {ax + b}$ 时
$$f ^ {(i)}(0) = a ^ i \cdot e ^ b$$

对于 $f(x) = ax + b$ 时

$$\begin{aligned}f'(0) &= b \\ f''(0) &= a\end{aligned}$$

其他均为 $0$

然后用 LCT 维护每个系数的和即可。

时间复杂度 $O(kn \log n)$

代码

小常数 LCT + 循环展开效果很好啊,BZOJ rk1,比 rk2 快了 6s,23333….

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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include <bits/stdc++.h>

namespace {

inline char read() {
static const int IN_LEN = 1 << 18 | 1;
static char buf[IN_LEN], *s, *t;
return (s == t) && (t = (s = buf) + fread(buf, 1, IN_LEN, stdin)),
s == t ? -1 : *s++;
}

template <typename T>
inline void read(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return;
iosig |= c == '-';
}
for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0');
iosig && (x = -x);
}

inline int read(char *buf) {
register int s = 0;
register char c;
while (c = read(), isspace(c) && c != -1)
;
if (c == -1) {
*buf = 0;
return -1;
}
do
buf[s++] = c;
while (c = read(), !isspace(c) && c != -1);
buf[s] = 0;
return s;
}

inline void read(char &c) {
while (c = read(), isspace(c) && c != -1)
;
}

inline void read(double &t) {
static char c;
static bool iosig;
register int x = 0;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return;
c == '-' ? iosig = true : 0;
}
for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0');
if (c == '.') {
register long long y = 0, cnt = 1;
for (c = read(); isdigit(c); c = read())
y = y * 10 + (c ^ '0'), cnt *= 10;
t = x + (double)y / cnt;
} else {
t = x;
}
iosig ? t = -t : 0;
}

const int OUT_LEN = 1 << 18 | 1;

char obuf[OUT_LEN], *oh = obuf;

inline void print(char c) {
(oh == obuf + OUT_LEN) && (fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf);
*oh++ = c;
}

template <typename T>
inline void print(T x) {
static int buf[21], cnt;
if (x != 0) {
(x < 0) && (print('-'), x = -x);
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48;
while (cnt) print((char)buf[cnt--]);
} else {
print('0');
}
}

inline void print(const char *s) {
for (; *s; s++) print(*s);
}

const double EPS = 1e-9;

inline int sign(const double x) { return (x > EPS) - (x < -EPS); }

inline void print(double x) {
register int sig = sign(x);
if (sig == 0) {
print('0');
return;
}
(sig == -1) && (print('-'), x = -x);
print((int)x);
x = x - (int)x;
print('.');
static char buf[21], cnt;
x *= 1000000000;
register int t = round(x);
for (cnt = 0; t; t /= 10) buf[++cnt] = t % 10 | 48;
for (register int i = 9; i > cnt; i--) print('0');
while (cnt) print((char)buf[cnt--]);
}

struct InputOutputStream {
~InputOutputStream() { fwrite(obuf, 1, oh - obuf, stdout); }

template <typename T>
inline InputOutputStream &operator>>(T &x) {
read(x);
return *this;
}

template <typename T>
inline InputOutputStream &operator<<(const T &x) {
print(x);
return *this;
}
} io;

const int MAXN = 100000 + 9;
const int MAX_EXP = 11;

struct Node *null;

struct Node {
double f[MAX_EXP], sum[MAX_EXP];
Node *c[2], *fa, *top;
bool rev;

inline bool relation() { return this == fa->c[1]; }

inline void reverse() { rev ^= 1, std::swap(c[0], c[1]); }

inline void pushDown() {
rev && (c[0]->reverse(), c[1]->reverse(), rev = false);
}

inline void maintain() {
#define opt(i) sum[i] = f[i] + c[0]->sum[i] + c[1]->sum[i]
opt(0);
opt(1), opt(2), opt(3), opt(4), opt(5);
opt(6), opt(7), opt(8), opt(9), opt(10);
#undef opt
}

inline void rotate(register bool f) {
register Node *o = fa;
top = o->top;
o->pushDown();
pushDown();
(fa = o->fa)->c[o->relation()] = this;
(o->c[f] = c[!f])->fa = o;
(c[!f] = o)->fa = this;
o->maintain();
}

inline void splay() {
register bool f;
for (pushDown(); fa != null;) {
(f = relation(), fa->fa == null)
? rotate(f)
: (f == fa->relation() ? (fa->rotate(f), rotate(f))
: (rotate(f), rotate(!f)));
}
maintain();
}

inline void expose() {
splay();
if (c[1] != null) {
c[1]->top = this;
c[1]->fa = null;
c[1] = null;
maintain();
}
}

inline bool splice() {
splay();
if (top == null) return false;
top->expose();
top->c[1] = this;
fa = top;
top = null;
fa->maintain();
return true;
}

inline void access() {
for (expose(); splice();)
;
}

inline void evert() { access(), splay(), reverse(); }
} pool[MAXN];

inline void link(register Node *u, register Node *v) {
u->evert();
u->top = v;
}

inline void cut(register Node *u, register Node *v) {
u->expose();
v->expose();
if (u->top == v) u->top = null;
if (v->top == u) v->top = null;
}

inline void modify(register Node *p, const int cmd, const double a,
const double b) {
memset(p->f, 0, sizeof(p->f));
p->splay();
switch (cmd) {
case 1: {
// sin(b), cos(b) * a, -sin(b) * a ^ 2
// -cos(b) * a ^ 3, sin(b) * a ^ 4
register double expN = a;
const double SIN_B = sin(b), COS_B = cos(b);
p->f[0] = SIN_B;
p->f[1] = COS_B * expN;
expN *= a;
p->f[2] = -(SIN_B * expN) / 2;
expN *= a;
p->f[3] = -(COS_B * expN) / 6;
expN *= a;
p->f[4] = (SIN_B * expN) / 24;

#define opt(i) p->f[i] = p->f[i - 4] * expN / (i - 3) / (i - 2) / (i - 1) / i
opt(5), opt(6), opt(7), opt(8), opt(9), opt(10);
#undef opt
break;
}
case 2: {
// a ^ i \cdot e ^ b
p->f[0] = exp(b);

#define opt(i) p->f[i] = p->f[i - 1] * a / i
opt(1), opt(2), opt(3), opt(4), opt(5);
opt(6), opt(7), opt(8), opt(9), opt(10);
#undef opt
break;
}
case 3: {
p->f[0] = b, p->f[1] = a;
break;
}
}
p->maintain();
}

inline void query(register Node *u, register Node *v, register double x) {
register double expN = 1, ans = 0;
u->evert(), v->access(), v->splay();
register Node *p = v;
for (; p->c[0] != null; p = p->c[0]) p->pushDown();
if (p != u) {
io << "unreachable\n";
return;
}
for (register int i = 0; i < MAX_EXP; i++) {
ans += expN * v->sum[i];
expN *= x;
}
io << ans << '\n';
}

inline void solve() {
null = pool;
null->c[0] = null->c[1] = null;
null->fa = null;
null->top = null;
register int n, m;
static char cmd, buf[21];
io >> n >> m >> buf;

for (register int i = 1, type; i <= n; i++) {
pool[i].c[0] = pool[i].c[1] = null;
pool[i].fa = null;
pool[i].top = null;
register double a, b;
io >> type >> a >> b;
modify(pool + i, type, a, b);
}

while (m--) {
io >> cmd;
switch (cmd) {
case 'a': {
register int u, v;
io >> u >> v;
link(pool + u + 1, pool + v + 1);
break;
}
case 'd': {
register int u, v;
io >> u >> v;
cut(pool + u + 1, pool + v + 1);
break;
}
case 'm': {
register int c, f;
register double a, b;
io >> c >> f >> a >> b;
modify(pool + c + 1, f, a, b);
break;
}
case 't': {
register int u, v;
register double x;
io >> u >> v >> x;
query(pool + u + 1, pool + v + 1, x);
break;
}
}
}
}
} // namespace

int main() {
// freopen("math.in", "r", stdin);
// freopen("math.out", "w", stdout);
solve();
return 0;
}

Comments

Your browser is out-of-date!

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

×