【题解】HackerRank - Fibonacci Numbers Tree

题目

https://www.hackerrank.com/challenges/fibonacci-numbers-tree/

题意

给一棵树,要求支持两种操作:

  • U x k 给 x 所在的子树中的每一个结点 y 加上一个值 \(Fib(k+dist(x,y))\)
  • Q x y询问 x 到 y 的路径上所有结点的值的和。

题解

  • 如果子树上所有结点都加上一个定值的话,那么就能做了。只需要 树链剖分+线段树 就好了。

  • 利用 Fibonacci 数列的一个性质 \(F_{a+b}=F_{a-1} \cdot F_b+F_a \cdot F_{b+1}\)(对于负数项同样成立),将 \(F(k+dist(x,y))\) 拆分为:

    \[\begin{eqnarray}F(k-dep(x)+dep(y)) &=& F(k-dep(x)-1) \cdot F(dep(y))+F(k-dep(x)) \cdot F(dep(y)+1) \\ &=& C_1 \cdot F(dep(y))+C_2 \cdot F(dep(y)+1)\end{eqnarray}\]

  • 然后。。。然后就能做了嘛!

代码(套了一堆板子,有点长)

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
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(args...) do { cout << "\033[32;1m" << #args << " -> "; err(args); } while (0)
#else
#define dbg(...)
#endif
void err() { cout << "\033[39;0m" << endl; }
template<template<typename...> class T, typename t, typename... Args>
void err(T<t> a, Args... args) { for (auto x: a) cout << x << ' '; err(args...); }
template<typename T, typename... Args>
void err(T a, Args... args) { cout << a << ' '; err(args...); }
// -----------------------------------------------------------------------------
const int maxn = 1E5 + 100;
const LL MOD = 1E9 + 7;
vector<int> G[maxn];
int dep[maxn], idx[maxn], sz[maxn], son[maxn], top[maxn], dep_idx[maxn], fa[maxn], clk, out[maxn];
int n, Q;
LL fib[maxn] = {0, 1};

void add(LL &a, LL b) { (a += b) %= MOD; }

namespace BM {
inline void up(LL& a, LL b) { (a += b) %= MOD; }
typedef vector<LL> V;
V mul(const V& a, const V& b, const V& m, int k) {
V r; r.resize(2 * k - 1);
FOR (i, 0, k)
FOR (j, 0, k)
up(r[i + j], a[i] * b[j]);
FORD (i, k - 2, - 1) {
FOR (j, 0, k)
up(r[i + j], r[i + k] * m[j]);
r.pop_back();
}
return r;
}

V pow(LL n, const V& m) {
int k = (int)m.size() - 1; assert(m[k] == -1 || m[k] == MOD - 1);
V r(k), x(k); r[0] = x[1] = 1;
for (; n; n >>= 1, x = mul(x, x, m, k))
if (n & 1) r = mul(x, r, m, k);
return r;
}

LL go(LL n) {
static V a = {1, 1, -1};
static V x = {1, 1};
static int k = 2;
if (n <= k) return x[n - 1];
V r = pow(n - 1, a);
LL ans = 0;
FOR (i, 0, k)
up(ans, r[i] * x[i]);
return ans;
}

LL fib(LL n) {
if (n == 0) return 0;
if (n > 0) return go(n);
return (n & 1) ? go(-n) : MOD - go(-n);
}
}

namespace TREE {
struct Q {
LL v0, v1;
Q(LL d0 = 0, LL d1 = 0): v0(d0), v1(d1) {}
void operator += (Q& q) { add(v0, q.v0); add(v1, q.v1); }
};
struct P {
LL sum;
P(LL sum = 0): sum(sum) {}
void up(Q& q, LL d0, LL d1) { add(sum, d0 * q.v0 + d1 * q.v1); }
};
template<typename T>
P operator & (T&& a, T&& b) {
return P((a.sum + b.sum) % MOD);
}
P p[maxn << 2];
Q q[maxn << 2];
LL d0[maxn << 2], d1[maxn << 2];
#define ls o * 2, l, (l + r) / 2
#define rs o * 2 + 1, (l + r) / 2 + 1, r
void build(int o, int l, int r) {
if (l == r) {
d0[o] = fib[dep_idx[l]];
d1[o] = fib[dep_idx[l] + 1];
return;
}
build(ls); build(rs);
add(d0[o], d0[o * 2] + d0[o * 2 + 1]);
add(d1[o], d1[o * 2] + d1[o * 2 + 1]);
};
void maintain(int o, int l, int r) {
if (l == r) p[o] = P();
else p[o] = p[o * 2] & p[o * 2 + 1];
p[o].up(q[o], d0[o], d1[o]);
}
void pushdown(int o, int l, int r) {
q[o * 2] += q[o]; q[o * 2 + 1] += q[o];
q[o] = Q();
maintain(ls); maintain(rs);
}
P query(int ql, int qr, int o = 1, int l = 1, int r = n) {
if (ql > r || l > qr) return P();
if (ql <= l && r <= qr) return p[o];
pushdown(o, l, r);
return query(ql, qr, ls) & query(ql, qr, rs);
}
void update(int ql, int qr, Q v, int o = 1, int l = 1, int r = n) {
if (ql > r || l > qr) return;
if (ql <= l && r <= qr) q[o] += v;
else {
pushdown(o, l, r);
update(ql, qr, v, ls); update(ql, qr, v, rs);
}
maintain(o, l, r);
}
}


void predfs(int u, int d) {
dep[u] = d;
sz[u] = 1;
int& maxs = son[u] = -1;
for (int v: G[u])
if (v != fa[u]) {
fa[v] = u;
predfs(v, d + 1);
sz[u] += sz[v];
if (maxs == -1 || sz[v] > sz[maxs])
maxs = v;
}
}

void dfs(int u, int tp) {
top[u] = tp;
idx[u] = ++clk;
dep_idx[idx[u]] = dep[u];
if (son[u] != -1) dfs(son[u], tp);
for (int v: G[u])
if (v != son[u] && v != fa[u])
dfs(v, v);
out[u] = clk;
}

LL query(int u, int v) {
int uu = top[u], vv = top[v];
LL ret = 0;
while (uu != vv) {
if (dep[uu] < dep[vv]) { swap(uu, vv); swap(u, v); }
add(ret, TREE::query(idx[uu], idx[u]).sum);
u = fa[uu];
uu = top[u];
}
if (dep[u] < dep[v]) swap(u, v);
add(ret, TREE::query(idx[v], idx[u]).sum);
return ret;
}


int main() {
#ifdef zerol
freopen("in", "r", stdin);
// freopen("out", "w", stdout);
#endif
FOR (i, 2, maxn) fib[i] = (fib[i - 1] + fib[i - 2]) % MOD;
cin >> n >> Q;
FOR (u, 2, n + 1) {
int v; scanf("%d", &v);
G[v].push_back(u); G[u].push_back(v);
}
predfs(1, 1); dfs(1, 1); TREE::build(1, 1, n);
char s[10];
while (Q--) {
scanf("%s", s);
if (s[0] == 'U') {
int u; LL k; scanf("%d%lld", &u, &k);
TREE::update(idx[u], out[u], {BM::fib(k - dep[u] - 1), BM::fib(k - dep[u])});
} else if (s[0] == 'Q') {
int u, v; scanf("%d%d", &u, &v);
printf("%lld\n", query(u, v));
}
}
}