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
| #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double db; const int N = 100010; const int D = 20; struct poly { db e[D]; poly() { memset(e, 0, sizeof(e)); } db& operator[](const int& k) { return e[k]; } friend poly operator+(const poly& a, const poly& b) { poly r; for (int i = 0; i < D; ++i) r[i] = a.e[i] + b.e[i]; return r; } db operator()(const db& x) const { db r = 0, _x = 1; for (int i = 0; i < D; ++i) { r += e[i] * _x; _x *= x; } return r; } };
void setf(poly& p, int op, db a, db b) { memset(p.e, 0, sizeof(p.e)); static db s[2][4] = { { 0, 1, 0, -1}, { 1, 1, 1, 1 }, }; if (op <= 2) { db tp = 1; for (int i = 0; i < D; ++i) { db cur = tp; for (int k = i; k < D; ++k) { p[i] += s[op - 1][k % 4] * cur; cur = cur * b / (db)(k - i + 1); } tp = tp * a / (db)(i + 1); } } else p[1] = a, p[0] = b; }
namespace LCT { #define ls ch[x][0] #define rs ch[x][1] int f[N], ch[N][2], rev[N]; poly val[N], sum[N]; inline bool Get(int x) { return ch[f[x]][1] == x; } inline bool isRoot(int x) { return ch[f[x]][0] != x && ch[f[x]][1] != x; } inline void PushUp(int x) { sum[x] = sum[ls] + sum[rs] + val[x]; } inline void Setrev(int x) { if (x) swap(ls, rs), rev[x] ^= 1; } inline void PushDown(int x) { if (rev[x]) { Setrev(ls); Setrev(rs); rev[x] = 0; } } inline void Rotate(int x) { int y = f[x], z = f[y], k = Get(x); if (!isRoot(y)) ch[z][Get(y)] = x; ch[y][k] = ch[x][k ^ 1]; f[ch[x][k ^ 1]] = y; ch[x][k ^ 1] = y; f[y] = x; f[x] = z; PushUp(y); PushUp(x); } void Update(int x) { if (!isRoot(x)) Update(f[x]); PushDown(x); } inline void Splay(int x) { Update(x); for (int fa; fa = f[x], !isRoot(x); Rotate(x)) { if (!isRoot(fa)) Rotate(Get(fa) == Get(x) ? fa : x); } PushUp(x); } inline void Access(int x) { for (int p = 0; x;p = x, x = f[x]) { Splay(x); rs = p; PushUp(x); } } inline void MakeRoot(int x) { Access(x); Splay(x); Setrev(x); } inline int FindRoot(int x) { Access(x); Splay(x); while (ls) PushDown(x), x = ls; Splay(x); return x; } inline void Split(int x, int y) { MakeRoot(x); Access(y); Splay(y); } inline void Cut(int x, int y) { MakeRoot(x); Access(y); Splay(y); ch[y][0] = f[x] = 0; } inline void Link(int x, int y) { MakeRoot(x); MakeRoot(y); f[x] = y; } inline bool Check(int x, int y) { MakeRoot(x); return FindRoot(y) == x; } #undef ls #undef rs }
int n, m; char type[5];
int main() { scanf("%d%d%s", &n, &m, type); for (int i = 1; i <= n; ++i) { int op; db a, b; scanf("%d%lf%lf", &op, &a, &b); setf(LCT::val[i], op, a, b); LCT::PushUp(i); } cerr << "init ok." << endl; while (m--) { char op[20]; int u, v, f; db a, b, x; scanf("%s", op); if (op[0] == 'a') { scanf("%d%d", &u, &v); ++u, ++v; LCT::Link(u, v); } else if (op[0] == 'd') { scanf("%d%d", &u, &v); ++u, ++v; LCT::Cut(u, v); } else if (op[0] == 'm') { scanf("%d%d%lf%lf", &u, &f, &a, &b); ++u; LCT::Split(u, u); setf(LCT::val[u], f, a, b); LCT::PushUp(u); } else { scanf("%d%d%lf", &u, &v, &x); ++u, ++v; if(!LCT::Check(u, v)) { puts("unreachable"); continue; } LCT::Split(u, v); printf("%.20e\n", LCT::sum[v](x)); } }
return 0; }
|