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
| #include <iostream> #include <cstdio> #include <cassert> #include <cstring> #include <cmath> #include <functional> #include <algorithm> #include <utility> #include <vector> #include <string> #include <map> #include <set> #ifdef XLor #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0) void err() { std::cout << std::endl; } template<typename T, typename...Args> void err(T a, Args...args) { std::cout << a << ' '; err(args...); } #else #define dbg(...) #endif #define ms(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; typedef pair<int,int> PII; const int mod = 998244353; const int inf = 1 << 30; const int maxn = 150000 + 5;
inline int add(int x, int y) { x += y; return x >= mod ? x -= mod : x; } inline int sub(int x, int y) { x -= y; return x < 0 ? x += mod : x; } inline int mul(int x, int y) { return 1ll * x * y % mod; } inline int qpow(int x, ll n) { int r = 1; while (n > 0) { if (n & 1) r = 1ll * r * x % mod; n >>= 1; x = 1ll * x * x % mod; } return r; } inline int inv(int x) { return qpow(x, mod - 2); }
int n, q, son[maxn], siz[maxn], dfn[maxn], tot, ivn; int top[maxn], fa[maxn], val[maxn]; vector<int> edge[maxn];
struct BIT { int a[maxn]; void update(int i, int x) { for (; i <= n; i += i & -i) a[i] = add(a[i], x); } void update(int l, int r, int x) { if (l >= r) return ; update(l, x); update(r, mod - x); } int query(int i) { int r = 0; for (; i; i -= i & -i) r = add(r, a[i]); return r; } } h;
void dfs(int u, int f) { fa[u] = f; dfn[u] = ++tot; siz[u] = 1; for (int v: edge[u]) { if (v == f) continue; dfs(v, u); siz[u] += siz[v]; if (siz[v] > siz[son[u]]) son[u] = v; } } void dfs(int u, int f, int tp) { top[u] = tp; if (son[u]) dfs(son[u], u, tp); for (int v: edge[u]) { if (v == f || v == son[u]) continue; dfs(v, u, v); } }
void update(int u, int d) { val[u] = add(val[u], d); int tot = mul(d, siz[u]); h.update(1, dfn[u], tot); h.update(dfn[u] + siz[u], n + 1, tot); if (son[u]) { int v = son[u]; tot = mul(d, n - siz[v]); h.update(dfn[v], dfn[v] + siz[v], tot); } }
int query(int u) { int ans = h.query(dfn[u]), x = u; while (x) { int tp = top[x]; ans = add(ans, mul(val[fa[tp]], n - siz[tp])); x = fa[tp]; } return add(val[u], mul(ans, ivn)); }
int main() { scanf("%d%d", &n, &q); ivn = inv(n); for (int i = 2, u, v; i <= n; i++) { scanf("%d%d", &u, &v); edge[u].push_back(v); edge[v].push_back(u); } dfs(1, 0); dfs(1, 0, 1); int op, u, d; while (q--) { scanf("%d%d", &op, &u); if (op == 1) { scanf("%d", &d); update(u, d); } else if (op == 2) { printf("%d\n", query(u)); } } return 0; }
|