Dyd's Blog

He who has a strong enough why can bear almost any how.

luoguP4546 [THUWC2017]在美妙的数学王国中畅游

LCT + 泰勒展开

在美妙的数学王国中畅游在美妙的数学王国中淹死

思路

好消息是它给了泰勒展开式,考虑 LCT 维护一个多项式类即可(感觉自己越来越短了)

代码

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
#include <bits/stdc++.h>
using DB = double;
const int L = 15, N = 1e5 + 100;
struct Fuc
{
DB c[L + 5];
DB& operator [] (const int &id) { return c[id]; }
DB calc(DB x)
{
DB res = 0, t = 1;
for (int i = 0; i <= L; ++i) res += t * c[i], t *= x;
return res;
}
void init(int typ, DB a, DB b)
{
if (typ == 1)
{
DB _sin = std::sin(b), _cos = std::cos(b), fac = 1;
c[0] = _sin / fac;
for (int i = 1; i <= L; ++i)
{
fac *= i, _sin *= a, _cos *= a;
if ((i & 3) == 1) c[i] = _cos / fac;
else if ((i & 3) == 2) c[i] = -_sin / fac;
else if ((i & 3) == 3) c[i] = -_cos / fac;
else c[i] = _sin / fac;
}
}
else if (typ == 2)
{
DB e = std::exp(b), fac = 1;
c[0] = e / fac;
for (int i = 1; i <= L; ++i)
{
fac *= i, e *= a;
c[i] = e / fac;
}
}
else
{
c[0] = b, c[1] = a;
for (int i = 2; i <= L; ++i) c[i] = 0;
}
}
};
Fuc operator + (Fuc x, Fuc y)
{
Fuc res;
for (int i = 0; i <= L; ++i) res[i] = x[i] + y[i];
return res;
}
struct LCT
{
struct Node
{
Node *fa, *ch[2];
bool rev;
Fuc f, sf;
} tr[N];
const Node _null = {nullptr, {nullptr, nullptr}, false, {0}, {0}};
Node *tot = tr, *null = (Node*)&_null;
Node* _new(int typ, DB a, DB b)
{
*tot = {null, {null, null}, false, {0}, {0}};
tot->f.init(typ, a, b), tot->sf = tot->f;
return tot++;
}
bool nrt(Node *x){ return x->fa->ch[0] == x || x->fa->ch[1] == x; }
void adt(Node *x){ std::swap(x->ch[0], x->ch[1]), x->rev ^= 1; }
void up(Node *x){ x->sf = x->ch[0]->sf + x->ch[1]->sf + x->f; }
void dw(Node *x){ if (x->rev) adt(x->ch[0]), adt(x->ch[1]), x->rev = false; }
void rot(Node *x)
{
Node *y = x->fa, *z = y->fa;
int k = y->ch[1] == x;
if (nrt(y)) z->ch[z->ch[1] == y] = x;
x->fa = z;
y->ch[k] = x->ch[k ^ 1], x->ch[k ^ 1]->fa = y;
x->ch[k ^ 1] = y, y->fa = x;
up(y);
}
void splay(Node *x)
{
static Node *stk[N];
int top = 0;
Node *y = x, *z;
stk[++top] = y;
while (nrt(y)) stk[++top] = y = y->fa;
while (top) dw(stk[top--]);
while (nrt(x))
{
y = x->fa, z = y->fa;
if (nrt(y)) (y->ch[1] == x) ^ (z->ch[1] == y) ? rot(x) : rot(y);
rot(x);
}
up(x);
}
void acs(Node *x)
{
for (Node *y = null; x != null; y = x, x = x->fa)
{
splay(x);
x->ch[1] = y;
up(x);
}
}
void mk_rt(Node *x){ acs(x), splay(x), adt(x); }
Node* f_rt(Node *x)
{
acs(x), splay(x);
while (x->ch[0] != null) dw(x), x = x->ch[0];
return x;
}
void lk(int idx, int idy)
{
Node *x = tr + idx - 1, *y = tr + idy - 1;
mk_rt(x), x->fa = y;
}
void cut(int idx, int idy)
{
Node *x = tr + idx - 1, *y = tr + idy - 1;
mk_rt(x), acs(y), splay(y);
x->fa = y->ch[0] = null, up(y);
}
void cg(int idx, int typ, DB a, DB b)
{
Node *x = tr + idx - 1;
splay(x);
x->f.init(typ, a, b), up(x);
}
DB ask(int idx, int idy, DB iq)
{
Node *x = tr + idx - 1, *y = tr + idy - 1;
mk_rt(x);
if (f_rt(y) != x) return NAN;
return y->sf.calc(iq);
}
} lct;
int main()
{
char type[15];
int n, m, op, u, v;
DB a, b;
scanf("%d %d %s", &n, &m, type);
for (int i = 1; i <= n; ++i)
{
scanf("%d %lf %lf", &op, &a, &b);
lct._new(op, a, b);
}
while (m--)
{
scanf("%s %d %d", type, &u, &v);
if (type[0] == 'a') lct.lk(++u, ++v);
else if (type[0] == 'd') lct.cut(++u, ++v);
else if (type[0] == 'm') scanf("%lf %lf", &a, &b), lct.cg(++u, v, a, b);
else
{
scanf("%lf", &a);
b = lct.ask(++u, ++v, a);
if (std::isnan(b)) puts("unreachable");
else printf("%.9lf\n", b);
}
}
return 0;
}