競技プログラミング用 C++ ライブラリ
test/top_tree_subtree_sum.test.cpp · GitHub で見る
#define PROBLEM "https://judge.yosupo.jp/problem/dynamic_tree_vertex_add_subtree_sum"
#include "tree/top_tree.hpp"
#include "components/fast_io.hpp"
#include "test/fixtures/top_tree_dp.hpp"
int main() {
FastInput input;
FastOutput output;
int n, q;
input.read(n), input.read(q);
std::vector<std::int64_t> values(n);
for (auto& v : values) input.read(v);
TopTree<TopTreeSumDP> tree(std::move(values));
for (int i = 1; i < n; ++i) {
int u, v;
input.read(u), input.read(v);
tree.link(u, v);
}
while (q--) {
int type, u;
input.read(type), input.read(u);
if (type == 0) {
int v, w, x;
input.read(v), input.read(w), input.read(x);
tree.cut(u, v), tree.link(w, x);
} else if (type == 1) {
std::int64_t x;
input.read(x);
tree.set(u, tree.get(u) + x);
} else {
int p;
input.read(p);
output.println(tree.subtree_prod(p, u));
}
}
}
#line 1 "test/top_tree_subtree_sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/dynamic_tree_vertex_add_subtree_sum"
#line 1 "tree/top_tree.hpp"
#include <cassert>
#include <utility>
#include <vector>
template <class DP>
class TopTree {
public:
using Vertex = typename DP::Vertex;
using Path = typename DP::Path;
using Point = typename DP::Point;
private:
struct Node {
Vertex value;
Path forward, backward;
int child[2] = {}, parent = 0, light = 0, belong = 0;
bool reversed = false;
};
struct Rake {
Point value, sum;
int child[2] = {}, parent = 0;
};
std::vector<Node> nodes_;
std::vector<Rake> rake_;
int free_ = 0;
void check([[maybe_unused]] int v) const { assert(0 <= v && v < size()); }
bool path_root(int v) const {
int p = nodes_[v].parent;
return !p || (nodes_[p].child[0] != v && nodes_[p].child[1] != v);
}
void rake_pull(int v) {
auto& x = rake_[v];
x.sum = x.value;
for (int c : x.child) if (c) x.sum = DP::rake(x.sum, rake_[c].sum);
}
void rake_rotate(int v) {
int p = rake_[v].parent, g = rake_[p].parent, d = rake_[p].child[1] == v;
int c = rake_[v].child[d ^ 1];
rake_[p].child[d] = c;
if (c) rake_[c].parent = p;
rake_[v].child[d ^ 1] = p, rake_[p].parent = v, rake_[v].parent = g;
if (g) rake_[g].child[rake_[g].child[1] == p] = v;
rake_pull(p), rake_pull(v);
}
void rake_splay(int v) {
while (int p = rake_[v].parent) {
int g = rake_[p].parent;
if (g) rake_rotate((rake_[p].child[1] == v) == (rake_[g].child[1] == p) ? p : v);
rake_rotate(v);
}
}
int rake_insert(int root, const Point& value) {
assert(free_);
int v = free_;
free_ = rake_[v].parent;
rake_[v] = {value, value, {root, 0}, 0};
if (root) rake_[root].parent = v;
rake_pull(v);
return v;
}
int rake_erase(int v) {
rake_splay(v);
int l = rake_[v].child[0], r = rake_[v].child[1];
rake_[v].parent = free_, free_ = v;
if (!l) {
if (r) rake_[r].parent = 0;
return r;
}
rake_[l].parent = 0;
int root = l;
while (rake_[root].child[1]) root = rake_[root].child[1];
rake_splay(root);
rake_[root].child[1] = r;
if (r) rake_[r].parent = root;
rake_pull(root);
return root;
}
Path vertex_path(int v) const {
const auto& x = nodes_[v];
return DP::add_vertex(x.light ? rake_[x.light].sum : DP::point_identity(), x.value);
}
void pull(int v) {
auto& x = nodes_[v];
x.forward = x.backward = vertex_path(v);
if (int l = x.child[0]) {
x.forward = DP::compress(nodes_[l].forward, x.forward);
x.backward = DP::compress(x.backward, nodes_[l].backward);
}
if (int r = x.child[1]) {
x.forward = DP::compress(x.forward, nodes_[r].forward);
x.backward = DP::compress(nodes_[r].backward, x.backward);
}
}
void reverse(int v) {
auto& x = nodes_[v];
using std::swap;
std::swap(x.child[0], x.child[1]);
swap(x.forward, x.backward);
x.reversed = !x.reversed;
}
void push(int v) {
auto& x = nodes_[v];
if (!x.reversed) return;
for (int c : x.child) if (c) reverse(c);
x.reversed = false;
}
void rotate(int v) {
int p = nodes_[v].parent, g = nodes_[p].parent, d = nodes_[p].child[1] == v;
int c = nodes_[v].child[d ^ 1];
if (path_root(p)) {
nodes_[v].belong = nodes_[p].belong;
nodes_[p].belong = 0;
} else nodes_[g].child[nodes_[g].child[1] == p] = v;
nodes_[p].child[d] = c;
if (c) nodes_[c].parent = p;
nodes_[v].child[d ^ 1] = p, nodes_[p].parent = v, nodes_[v].parent = g;
pull(p), pull(v);
}
void splay(int v) {
push(v);
while (!path_root(v)) {
int p = nodes_[v].parent;
if (!path_root(p)) {
int g = nodes_[p].parent;
push(g), push(p), push(v);
rotate((nodes_[p].child[1] == v) == (nodes_[g].child[1] == p) ? p : v);
} else push(p), push(v);
rotate(v);
}
}
void expose(int v) {
int last = 0;
for (int x = v; x; x = nodes_[x].parent) {
splay(x);
int r = nodes_[x].child[1];
if (r) {
nodes_[x].light = rake_insert(nodes_[x].light, DP::add_edge(nodes_[r].forward));
nodes_[r].belong = nodes_[x].light;
}
if (last) {
nodes_[x].light = rake_erase(nodes_[last].belong);
nodes_[last].belong = 0;
}
nodes_[x].child[1] = last;
pull(x), last = x;
}
splay(v);
}
void evert(int v) { expose(v); reverse(v); }
public:
TopTree() = default;
explicit TopTree(std::vector<Vertex> values) {
int n = int(values.size());
nodes_.reserve(n + 1), rake_.reserve(n + 1);
if (n) {
Path path = DP::add_vertex(DP::point_identity(), values.front());
nodes_.push_back({values.front(), path, path});
}
for (auto& value : values) {
Path path = DP::add_vertex(DP::point_identity(), value);
nodes_.push_back({std::move(value), path, path});
}
for (int i = 0; i <= n; ++i) rake_.push_back({DP::point_identity(), DP::point_identity(), {}, i < n ? i + 1 : 0});
free_ = n ? 1 : 0;
}
int size() const { return nodes_.empty() ? 0 : int(nodes_.size()) - 1; }
bool empty() const { return size() == 0; }
const Vertex& get(int v) const { check(v); return nodes_[v + 1].value; }
void set(int v, const Vertex& value) {
check(v), ++v;
expose(v), nodes_[v].value = value, pull(v);
}
bool same(int u, int v) {
check(u), check(v), ++u, ++v;
expose(u), expose(v);
return u == v || nodes_[u].parent;
}
bool link(int u, int v) {
check(u), check(v), ++u, ++v;
evert(u), expose(v);
if (u == v || nodes_[u].parent) return false;
nodes_[u].parent = v, nodes_[v].child[1] = u;
pull(v);
return true;
}
bool cut(int u, int v) {
check(u), check(v), ++u, ++v;
evert(u), expose(v), push(u);
if (nodes_[v].child[0] != u || nodes_[u].child[1]) return false;
nodes_[v].child[0] = nodes_[u].parent = 0;
pull(v);
return true;
}
Path prod(int u, int v) {
check(u), check(v), ++u, ++v;
evert(u), expose(v);
assert(u == v || nodes_[u].parent);
return nodes_[v].forward;
}
Path all_prod(int root) { return subtree_prod(root, root); }
Path subtree_prod(int root, int v) {
check(root), check(v), ++root, ++v;
evert(root), expose(v);
assert(root == v || nodes_[root].parent);
return vertex_path(v);
}
};
#line 1 "components/fast_io.hpp"
#include <algorithm>
#include <array>
#include <bit>
#line 8 "components/fast_io.hpp"
#include <charconv>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <memory>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#if defined(__unix__) || defined(__APPLE__)
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
namespace fast_io_detail { struct BulkInput; struct BulkOutput; }
class FastInput {
friend struct fast_io_detail::BulkInput;
static constexpr std::size_t capacity = 1 << 16;
FILE *file = nullptr;
std::unique_ptr<char[]> buffer;
const char *current = "", *end = current;
std::string scratch;
void *mapping = nullptr;
std::size_t mapped_size = 0;
bool refill() {
if (!file) return false;
std::size_t n = std::fread(buffer.get(), 1, capacity, file);
current = buffer.get();
end = current + n;
return n;
}
static const char *scan(const char *p, const char *end) {
while (end - p >= 8) {
std::uint64_t x;
std::memcpy(&x, p, 8);
if ((x - 0x2121212121212121ULL) & ~x & 0x8080808080808080ULL) break;
p += 8;
}
while (p != end && static_cast<unsigned char>(*p) > ' ') ++p;
return p;
}
public:
explicit FastInput(FILE *input = stdin) {
#if defined(__unix__) || defined(__APPLE__)
struct stat status;
int fd = fileno(input);
auto offset = ftello(input);
if (fd >= 0 && offset >= 0 && !fstat(fd, &status) && S_ISREG(status.st_mode) && status.st_size > offset) {
mapped_size = status.st_size;
mapping = mmap(nullptr, mapped_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (mapping != MAP_FAILED) {
current = static_cast<const char *>(mapping) + offset;
end = static_cast<const char *>(mapping) + mapped_size;
return;
}
mapping = nullptr;
}
#endif
file = input;
buffer = std::make_unique_for_overwrite<char[]>(capacity);
}
explicit FastInput(std::string_view input) : current(input.empty() ? "" : input.data()), end(current + input.size()) {}
FastInput(const FastInput &) = delete;
FastInput &operator=(const FastInput &) = delete;
~FastInput() {
#if defined(__unix__) || defined(__APPLE__)
if (mapping) munmap(mapping, mapped_size);
#endif
}
std::string_view token() {
while (true) {
while (current != end && static_cast<unsigned char>(*current) <= ' ') ++current;
if (current != end) break;
if (!refill()) return {};
}
const char *begin = current;
current = scan(current, end);
if (current != end || !file) return {begin, std::size_t(current - begin)};
scratch.assign(begin, end);
while (refill()) {
current = scan(current, end);
scratch.append(buffer.get(), std::size_t(current - buffer.get()));
if (current != end) break;
}
return scratch;
}
template <class T>
bool read(T &value) {
if constexpr (std::is_integral_v<T> && !std::is_same_v<T, bool>) {
while (true) {
while (current != end && static_cast<unsigned char>(*current) <= ' ') ++current;
if (current != end) break;
if (!refill()) return false;
}
if constexpr (std::is_unsigned_v<T> && sizeof(T) == 4 && std::endian::native == std::endian::little) {
if (end - current >= 11) {
std::uint64_t x;
std::memcpy(&x, current, 8);
if (!(((x + 0x4646464646464646ULL) | (x - 0x3030303030303030ULL)) & 0x8080808080808080ULL)) {
x &= 0x0f0f0f0f0f0f0f0fULL;
x = (x * 10 + (x >> 8)) & 0x00ff00ff00ff00ffULL;
x = (x * 100 + (x >> 16)) & 0x0000ffff0000ffffULL;
std::uint64_t v = std::uint32_t(x * 10000 + (x >> 32));
int n = 8;
for (; n < 10 && static_cast<unsigned char>(current[n]) - unsigned('0') < 10; ++n)
v = v * 10 + (current[n] - '0');
if (static_cast<unsigned char>(current[n]) <= ' ' && v <= 0xffffffffULL) {
current += n;
value = T(v);
return true;
}
}
}
}
T parsed{};
auto result = std::from_chars(current, end, parsed);
if (result.ec == std::errc{} && (result.ptr != end ? static_cast<unsigned char>(*result.ptr) <= ' ' : !file)) {
current = result.ptr;
value = parsed;
return true;
}
}
auto s = token();
if (s.empty()) return false;
if constexpr (std::is_same_v<T, bool>) {
assert(s == "0" || s == "1");
value = s[0] == '1';
} else if constexpr (std::is_integral_v<T>) {
auto result = std::from_chars(s.data(), s.data() + s.size(), value);
assert(result.ec == std::errc{} && result.ptr == s.data() + s.size());
(void)result;
} else value = T(s);
return true;
}
};
class FastOutput {
friend struct fast_io_detail::BulkOutput;
inline static constexpr auto quads = [] {
std::array<std::array<char, 4>, 10000> table{};
for (int i = 0; i < 10000; ++i)
table[i] = {char('0' + i / 1000), char('0' + i / 100 % 10), char('0' + i / 10 % 10), char('0' + i % 10)};
return table;
}();
FILE *file;
std::unique_ptr<char[]> buffer = std::make_unique_for_overwrite<char[]>(1 << 16);
std::size_t capacity = 1 << 16, length = 0;
char *reserve(std::size_t n) {
if (n > capacity - length) {
if (!flush()) throw std::runtime_error("output failed");
if (n > capacity) {
std::size_t next = std::max(n, 2 * capacity);
buffer = std::make_unique_for_overwrite<char[]>(next);
capacity = next;
}
}
return buffer.get() + length;
}
public:
explicit FastOutput(FILE *output = stdout) : file(output) {}
FastOutput(const FastOutput &) = delete;
FastOutput &operator=(const FastOutput &) = delete;
~FastOutput() { flush(); }
bool flush() {
std::size_t offset = 0;
while (offset < length) {
auto n = std::fwrite(buffer.get() + offset, 1, length - offset, file);
if (!n) {
std::memmove(buffer.get(), buffer.get() + offset, length - offset);
length -= offset;
return false;
}
offset += n;
}
length = 0;
return std::fflush(file) == 0;
}
void write(char c) { *reserve(1) = c; ++length; }
void write(__uint128_t value) {
char digits[40];
char *end = digits + sizeof(digits), *begin = end;
do { *--begin = char('0' + value % 10); value /= 10; } while (value);
write(std::string_view(begin, end - begin));
}
void write(__int128_t value) {
__uint128_t magnitude = __uint128_t(value);
if (value < 0) write('-'), magnitude = 0 - magnitude;
write(magnitude);
}
void write(std::string_view s) {
char *out = reserve(s.size());
if (!s.empty()) std::memcpy(out, s.data(), s.size());
length += s.size();
}
template <class T> requires std::is_integral_v<T>
void write(T value) {
char *out = reserve(32);
if constexpr (std::is_same_v<T, bool>) *out = '0' + value, ++length;
else if constexpr (std::is_unsigned_v<T> && sizeof(T) == 4) {
char *p = out;
if (value >= 100000000) {
auto high = value / 100000000, low = value % 100000000;
p = std::to_chars(p, p + 2, high).ptr;
std::memcpy(p, quads[low / 10000].data(), 4);
std::memcpy(p + 4, quads[low % 10000].data(), 4);
p += 8;
} else if (value >= 10000) {
p = std::to_chars(p, p + 4, value / 10000).ptr;
std::memcpy(p, quads[value % 10000].data(), 4);
p += 4;
} else p = std::to_chars(p, p + 4, value).ptr;
length += p - out;
} else length += std::to_chars(out, out + 32, value).ptr - out;
}
template <class T> requires requires(const T &value, char *out) { value.decimal_capacity(); value.to_chars(out); }
void write(const T &value) {
char *out = reserve(value.decimal_capacity());
length += value.to_chars(out) - out;
}
template <class T>
void println(const T &value) { write(value); write('\n'); }
};
#line 1 "test/fixtures/top_tree_dp.hpp"
#line 1 "math/mint.hpp"
#line 4 "math/mint.hpp"
#include <istream>
#include <ostream>
#line 7 "math/mint.hpp"
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC push_options
#pragma GCC optimize("O3")
#endif
namespace mint_detail {
template <class I>
constexpr bool mint_integral = std::is_integral_v<I>
#if defined(__SIZEOF_INT128__)
|| std::is_same_v<I, __int128> || std::is_same_v<I, unsigned __int128>
#endif
;
struct [[gnu::warn_unused]] Mint {
using mint = Mint;
unsigned v = 0;
[[nodiscard]] static constexpr unsigned mod() { return 998244353; }
[[nodiscard, gnu::always_inline]] static unsigned reduce(unsigned long long x) {
return (x + (unsigned long long)(unsigned(x) * 998244351u) * mod()) >> 32;
}
[[gnu::always_inline]] Mint() = default;
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
Mint(I x) {
long long r = x % (long long)mod();
if (r < 0) r += mod();
v = reduce((unsigned long long)r * 932051910);
}
[[nodiscard, gnu::always_inline]] static Mint raw(unsigned x) {
Mint a;
a.v = reduce((unsigned long long)x * 932051910);
return a;
}
[[nodiscard, gnu::always_inline]] static Mint word(unsigned x) {
Mint a;
a.v = x;
return a;
}
[[nodiscard, gnu::always_inline]] unsigned val() const {
unsigned r = reduce(v);
return r < mod() ? r : r - mod();
}
friend std::istream &operator>>(std::istream &is, Mint &x) {
long long value;
if (is >> value) x = Mint(value);
return is;
}
friend std::ostream &operator<<(std::ostream &os, const Mint &x) {
return os << x.val();
}
[[gnu::always_inline]] Mint &operator+=(Mint b) {
v += b.v;
if (v >= 2 * mod()) v -= 2 * mod();
return *this;
}
[[gnu::always_inline]] Mint &operator-=(Mint b) {
v -= b.v;
if (v >> 31) v += 2 * mod();
return *this;
}
[[gnu::always_inline]] Mint &operator*=(Mint b) {
v = reduce((unsigned long long)v * b.v);
return *this;
}
Mint &operator/=(Mint b) { return *this *= b.inv(); }
Mint &operator++() { return *this += Mint(1); }
Mint &operator--() { return *this -= Mint(1); }
Mint operator++(int) {
Mint a = *this;
++*this;
return a;
}
Mint operator--(int) {
Mint a = *this;
--*this;
return a;
}
[[nodiscard, gnu::always_inline]] Mint operator+() const { return *this; }
[[nodiscard, gnu::always_inline]] Mint operator-() const { return word(v ? 2 * mod() - v : 0); }
[[nodiscard, gnu::always_inline]] Mint operator+(Mint b) const { return Mint(*this) += b; }
[[nodiscard, gnu::always_inline]] Mint operator-(Mint b) const { return Mint(*this) -= b; }
[[nodiscard, gnu::always_inline]] Mint operator*(Mint b) const { return Mint(*this) *= b; }
[[nodiscard]] Mint operator/(Mint b) const { return Mint(*this) /= b; }
[[nodiscard, gnu::always_inline]] bool operator==(Mint b) const { return (v < mod() ? v : v - mod()) == (b.v < mod() ? b.v : b.v - mod()); }
[[nodiscard, gnu::always_inline]] bool operator!=(Mint b) const { return !(*this == b); }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator+(I a, Mint b) { return Mint(a) + b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator-(I a, Mint b) { return Mint(a) - b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator*(I a, Mint b) { return Mint(a) * b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend Mint operator/(I a, Mint b) { return Mint(a) / b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend bool operator==(I a, Mint b) { return Mint(a) == b; }
template <class I, std::enable_if_t<mint_integral<I>, int> = 0>
[[nodiscard]] friend bool operator!=(I a, Mint b) { return Mint(a) != b; }
[[nodiscard]] Mint pow(long long n) const {
assert(n >= 0);
if (!n) return Mint(1);
Mint r(1), a = *this;
for (; n > 1; n >>= 1, a *= a)
if (n & 1) r *= a;
return r * a;
}
[[nodiscard, gnu::noinline]] Mint inv() const {
assert(*this != Mint(0));
Mint a2 = *this * *this, a3 = a2 * *this;
Mint a7 = a3 * a3 * *this;
Mint a31 = a7;
for (int i = 0; i < 2; ++i) a31 *= a31;
a31 *= a3;
Mint r = a7;
for (int i = 0; i < 3; ++i) r *= r;
r *= a3;
r *= r;
for (int j = 0; j < 4; ++j) {
for (int i = 0; i < 5; ++i) r *= r;
r *= a31;
}
for (int i = 0; i < 3; ++i) r *= r;
return r * a7;
}
};
} // namespace mint_detail
using mint = mint_detail::Mint;
using fps_mint = mint;
#if defined(__GNUC__) && !defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
#pragma GCC pop_options
#endif
#line 8 "test/fixtures/top_tree_dp.hpp"
struct TopTreeSumDP {
using Vertex = std::int64_t;
using Point = std::int64_t;
using Path = std::int64_t;
static Point point_identity() { return 0; }
static Point rake(Point a, Point b) { return a + b; }
static Path compress(Path a, Path b) { return a + b; }
static Point add_edge(Path a) { return a; }
static Path add_vertex(Point a, Vertex v) { return a + v; }
};
struct TopTreeCompositeDP {
using Vertex = std::array<mint, 2>;
using Path = Vertex;
struct Point {};
static Point point_identity() { return {}; }
static Point rake(Point, Point) { return {}; }
static Point add_edge(const Path&) { return {}; }
static Path add_vertex(Point, const Vertex& v) { return v; }
static Path compress(const Path& a, const Path& b) { return {a[0] * b[0], a[1] * b[0] + b[1]}; }
};
struct TopTreeDiameterDP {
using Vertex = std::int64_t;
struct Point { std::int64_t first = 0, second = 0, diameter = 0; };
struct Path { std::int64_t length, left, right, diameter; };
static Point point_identity() { return {}; }
static Point rake(Point a, Point b) {
return {std::max(a.first, b.first), std::max({a.second, b.second, std::min(a.first, b.first)}), std::max(a.diameter, b.diameter)};
}
static Point add_edge(const Path& a) { return {a.left, 0, a.diameter}; }
static Path add_vertex(Point a, Vertex v) { return {v, v + a.first, v + a.first, std::max(a.diameter, v + a.first + a.second)}; }
static Path compress(const Path& a, const Path& b) {
return {a.length + b.length, std::max(a.left, a.length + b.left), std::max(b.right, b.length + a.right), std::max({a.diameter, b.diameter, a.right + b.left})};
}
};
struct TopTreeIndependentSetDP {
using Vertex = mint;
using Point = std::array<mint, 2>;
using Path = std::array<mint, 4>;
static Point point_identity() { return {1, 1}; }
static Point rake(const Point& a, const Point& b) { return {a[0] * b[0], a[1] * b[1]}; }
static Point add_edge(const Path& a) { return {a[0] + a[2], a[0]}; }
static Path add_vertex(const Point& p, Vertex v) { return {p[0], p[0], v * p[1], 0}; }
static Path compress(const Path& a, const Path& b) {
return {a[0] * b[0] + a[1] * b[2], a[0] * b[1] + a[1] * b[3],
a[2] * b[0] + a[3] * b[2], a[2] * b[1] + a[3] * b[3]};
}
};
#line 5 "test/top_tree_subtree_sum.test.cpp"
int main() {
FastInput input;
FastOutput output;
int n, q;
input.read(n), input.read(q);
std::vector<std::int64_t> values(n);
for (auto& v : values) input.read(v);
TopTree<TopTreeSumDP> tree(std::move(values));
for (int i = 1; i < n; ++i) {
int u, v;
input.read(u), input.read(v);
tree.link(u, v);
}
while (q--) {
int type, u;
input.read(type), input.read(u);
if (type == 0) {
int v, w, x;
input.read(v), input.read(w), input.read(x);
tree.cut(u, v), tree.link(w, x);
} else if (type == 1) {
std::int64_t x;
input.read(x);
tree.set(u, tree.get(u) + x);
} else {
int p;
input.read(p);
output.println(tree.subtree_prod(p, u));
}
}
}