版权声明:湘 ICP 证 66666666 号 | Copyright © 2002-2019, ♂Hany01♂, All Rights Reserved 难道会有人转蒟蒻的博文?? https://blog.csdn.net/hhaannyyii/article/details/82431285
Description
master 对树上的求和非常感兴趣。他生成了一棵有根树,并且希望多次询问这棵树上一段路径上所有节点深度的k 次方和,而且每次的k 可能是不同的。此处节点深度的定义是这个节点到根的路径上的边数。他把这个问题交给了pupil,但pupil 并不会这么复杂的操作,你能帮他解决吗?
Solution
树上差分傻逼题。
对于不同的 分别处理,直接累一个到根的前缀和,然后 即可。
Code
/************************************************
* Au: Hany01
* Date: Sep 5th, 2018
* Prob: BZOJ5293 BJOI2018 求和
* Email: [email protected] & [email protected]
* Inst: Yali High School
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
static int _, __; static char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 3e5 + 5, MOD = 998244353;
int n, beg[maxn], nex[maxn << 1], v[maxn << 1], e = 1, val[51][maxn], fa[19][maxn], dep[maxn];
inline void add(int uu, int vv) { v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e; }
inline int ad(int x, int y) { return (x += y) >= MOD ? x - MOD : x; }
void DFS(int u, int pa, int depth) {
int tmp = depth; fa[0][u] = pa, dep[u] = depth + 1;
For(i, 1, 50) val[i][u] = ad(val[i][pa], tmp), tmp = (LL)tmp * depth % MOD;
For(i, 1, 18) fa[i][u] = fa[i - 1][fa[i - 1][u]];
for (register int i = beg[u]; i; i = nex[i])
if (v[i] != pa) DFS(v[i], u, depth + 1);
}
inline int LCA(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
Fordown(i, 18, 0) if (dep[fa[i][u]] >= dep[v]) u = fa[i][u];
if (u == v) return u;
Fordown(i, 18, 0) if (fa[i][u] ^ fa[i][v]) u = fa[i][u], v = fa[i][v];
return fa[0][u];
}
inline int query(int u, int v, int k) {
static int lca; lca = LCA(u, v);
return ad(ad(ad(val[k][u], val[k][v]), MOD - val[k][lca]), MOD - val[k][fa[0][lca]]);
}
int main()
{
#ifdef hany01
freopen("bzoj5293.in", "r", stdin);
freopen("bzoj5293.out", "w", stdout);
#endif
static int u, v, k;
n = read();
For(i, 2, n) u = read(), v = read(), add(u, v), add(v, u);
DFS(1, 0, 0);
for (static int m = read(); m --; )
u = read(), v = read(), k = read(), printf("%d\n", query(u, v, k));
return 0;
}