the first
2024-10-20 17:55:28
发布于:北京
19阅读
0回复
0点赞
#include <bits/stdc++.h>
template<class T>
class FenwickTree {
private: // fenwickTree for interval [0, n)
int n;
std::vector<T> data;
public:
FenwickTree() : FenwickTree(0) {}
explicit FenwickTree(int n) : n(n), data(n) {}
void add(int p, T x) {
assert(0 <= p and p < n);
p += 1;
while (p <= n) {
data[p - 1] += x;
p += p & -p;
}
}
T sum(int l, int r) {// return sum of [l, r)
assert(0 <= l and l <= r and r <= n);
return sum(r) - sum(l);
}
T sum(int r) {// return sum of [0, r)
assert(0 <= r and r <= n);
T s = 0;
while (r > 0) {
s += data[r - 1];
r -= r & -r;
}
return s;
}
};
template<int m>
class Modint {
private:
unsigned int _v;
static constexpr unsigned int umod() {return m;}
public:
static constexpr int mod() {return m;}
static Modint raw(int v) {
Modint x;
x._v = v;
return x;
}
Modint() : _v(0) {}
template<class T>
Modint(T v) {
long long x = (long long)(v % (long long)(umod()));
if (x < 0) x += umod();
_v = (unsigned int)(x);
}
unsigned int val() const {return _v;}
Modint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
Modint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
Modint operator++(int) {
Modint result = *this;
++*this;
return result;
}
Modint operator--(int) {
Modint result = *this;
--*this;
return result;
}
Modint& operator+=(const Modint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
Modint& operator-=(const Modint& rhs) {
_v -= rhs._v;
if (_v >= umod()) _v += umod();
return *this;
}
Modint& operator*=(const Modint& rhs) {
unsigned long long z = _v;
z *= rhs._v;
_v = (unsigned int)(z % umod());
return *this;
}
Modint& operator/=(const Modint& rhs) {return *this = *this * rhs.inv();}
Modint operator+() const {return *this;}
Modint operator-() const {return Modint() - *this;}
Modint pow(long long n) const {
if (n < 0) return pow(-n).inv();
Modint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
Modint inv() const {
assert(_v);
return pow(umod() - 2);
}
friend Modint operator+(const Modint& lhs, const Modint& rhs) {
return Modint(lhs) += rhs;
}
friend Modint operator-(const Modint& lhs, const Modint& rhs) {
return Modint(lhs) -= rhs;
}
friend Modint operator*(const Modint& lhs, const Modint& rhs) {
return Modint(lhs) *= rhs;
}
friend Modint operator/(const Modint& lhs, const Modint& rhs) {
return Modint(lhs) /= rhs;
}
friend bool operator==(const Modint& lhs, const Modint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const Modint& lhs, const Modint& rhs) {
return lhs._v != rhs._v;
}
};
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n, q, a;
std::cin >> n >> q >> a;
std::vector<std::vector<int>> g(n + 1);
for (int i = 1; i < n; ++i) {
int u, v;
std::cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
std::vector<int> sz(n, 1), dep(n), dfn(n);
int tag = 0;
auto dfs = [&](auto &dfs, int fa, int u)->void {
dfn[u] = tag++;
for (auto &v : g[u]) {
if (v == fa) continue;
dep[v] = dep[u] + 1;
dfs(dfs, u, v);
sz[u] += sz[v];
}
};
dfs(dfs, 0, 0);
using mint = Modint<998244353>;
FenwickTree<mint> ft(n + 1);
for (int i = 0; i < q; ++i) {
int t, u;
std::cin >> t >> u; --u;
if (t == 1) {
int x; std::cin >> x;
mint s = mint(a).pow(x - dep[u]);
ft.add(dfn[u], s);
ft.add(dfn[u] + sz[u], -s);
}
else
std::cout << (ft.sum(0, dfn[u] + 1) * mint(a).pow(dep[u])).val() << '\n';
}
return 0;
}
这里空空如也
有帮助,赞一个