#define NDEBUG #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include template class link_cut_tree { public: using value_structure = ValueMonoid; using value_type = typename value_structure::value_type; using operator_structure = OperatorMonoid; using operator_type = typename operator_structure::value_type; using modifier_type = Modifier; using size_type = ::std::size_t; private: class node_type { public: node_type *left, *right, *parent; typename link_cut_tree::value_type value, sum; typename link_cut_tree::operator_type lazy; bool reversed; // reverse->lazy template node_type(V&&v, operator_type&&o) : value(::std::forward(v)), sum(value), lazy(::std::move(o)), reversed(0) { } }; using pointer = node_type *; using const_pointer = const node_type *; ::std::vector nodes; size_type size_; value_structure vf; operator_structure of; modifier_type mf; pointer nil() noexcept { return nodes.data(); } void reverse(const pointer ptr) { ptr->lazy = of.reverse(ptr->lazy); ptr->reversed ^= 1; } void push(const pointer ptr) { if (ptr->reversed) { ptr->reversed = 0; ptr->value = vf.reverse(ptr->value); ::std::swap(ptr->left, ptr->right); reverse(ptr->left); reverse(ptr->right); } ptr->left->lazy = of(ptr->left->lazy, ptr->lazy); ptr->right->lazy = of(ptr->right->lazy, ptr->lazy); ptr->value = mf(ptr->value, ptr->lazy); ptr->lazy = of.identity(); } void propagate(pointer ptr) { pointer prev = nullptr; while (ptr != nil()) { ::std::swap(ptr->parent, prev); ::std::swap(ptr, prev); } while (prev) { push(prev); ::std::swap(prev->parent, ptr); ::std::swap(prev, ptr); } nil()->sum = vf.identity(); nil()->lazy = of.identity(); nil()->reversed = 0; } value_type reflect(const const_pointer ptr) { return mf(ptr->reversed ? vf.reverse(ptr->sum) : ptr->sum, ptr->lazy); } void calc(const pointer ptr) { ptr->sum = vf(vf(reflect(ptr->left), ptr->value), reflect(ptr->right)); } void rotate_l(const pointer ptr, const pointer ch) { ptr->right = ch->left; ch->left->parent = ptr; calc(ptr); ch->left = ptr; ptr->parent = ch; } void rotate_r(const pointer ptr, const pointer ch) { ptr->left = ch->right; ch->right->parent = ptr; calc(ptr); ch->right = ptr; ptr->parent = ch; } void splay(const pointer ptr) { for (pointer x, y = ptr;;) { x = ptr->parent; if (x->left == y) { y = x->parent; ptr->parent = y->parent; if (y->left == x) rotate_r(y, x), rotate_r(x, ptr); else if (y->right == x) rotate_l(y, ptr), rotate_r(x, ptr); else return ptr->parent = y, rotate_r(x, ptr); } else if (x->right == y) { y = x->parent; ptr->parent = y->parent; if (y->right == x) rotate_l(y, x), rotate_l(x, ptr); else if (y->left == x) rotate_r(y, ptr), rotate_l(x, ptr); else return ptr->parent = y, rotate_l(x, ptr); } else { return; } } } void expose(const pointer ptr) { propagate(ptr); pointer x = ptr, prev = nil(); while (x != nil()) { splay(x); x->right = prev; calc(x); prev = x; x = x->parent; } splay(ptr); calc(ptr); } void reroot(const pointer ptr) { expose(ptr); reverse(ptr); } pointer get_ptr(const size_type i) noexcept { return nodes.data() + 1 + i; } public: link_cut_tree() : link_cut_tree(0) {} explicit link_cut_tree(const size_type size, const value_structure &x = value_structure(), const operator_structure &y = operator_structure(), const modifier_type &z = modifier_type()) : size_(size), vf(x), of(y), mf(z) { nodes.reserve(size_ + 1); nodes.emplace_back(vf.identity(), of.identity()); nil()->left = nil()->right = nil()->parent = nil(); nodes.resize(size_ + 1, nodes.front()); } template link_cut_tree(InputIter first, InputIter last, const value_structure &x = value_structure(), const operator_structure &y = operator_structure(), const modifier_type &z = modifier_type()) :vf(x), of(y), mf(z) { nodes.emplace_back(vf.identity(), of.identity()); for (;first != last;++first) nodes.emplace_back(*first, of.identity()); const pointer n = nil(); for (auto &e : nodes) e.left = e.right = e.parent = n; size_ = nodes.size() - 1; } bool empty() const noexcept { return !size_; } size_type size() const noexcept { return size_; } bool connected(const size_type v, const size_type u) { assert(v < size()); assert(u < size()); expose(get_ptr(v)); expose(get_ptr(u)); return nodes[v + 1].parent != nil() || v == u; } value_type fold(const size_type v, const size_type u) { assert(v < size()); assert(u < size()); assert(connected(v, u)); reroot(get_ptr(v)); expose(get_ptr(u)); return nodes[u + 1].sum; } void link(const size_type parent, const size_type child) { assert(parent < size()); assert(child < size()); assert(!connected(parent, child)); reroot(get_ptr(child)); nodes[child + 1].parent = get_ptr(parent); } void cut(const size_type v) { assert(v < size()); expose(get_ptr(v)); nodes[v + 1].left->parent = nil(); nodes[v + 1].left = nil(); nodes[v + 1].sum = nodes[v + 1].value; } void update(const size_type v, const size_type u, const operator_type &data) { assert(v < size()); assert(u < size()); assert(connected(v, u)); reroot(get_ptr(v)); expose(get_ptr(u)); nodes[u + 1].lazy = data; } template void update(const size_type v, const F &f) { assert(v < size()); expose(get_ptr(v)); nodes[v + 1].value = f(nodes[v + 1].value); calc(get_ptr(v)); } }; template link_cut_tree make_lctree(const ::std::size_t size, const V &v, const O &o, const M &m) { return link_cut_tree(size, v, o, m); } #include template <::std::uint_fast32_t MODULO> class modint { using uint32 = ::std::uint_fast32_t; using uint64 = ::std::uint_fast64_t; public: using value_type = uint32; uint32 a; modint() : a(0) {} modint(const uint32 x) : a(x) {} modint operator+(const modint &o) const { return a + o.a < MODULO ? modint(a + o.a) : modint(a + o.a - MODULO); } modint operator-(const modint &o) const { return modint(a < o.a ? a + MODULO - o.a : a - o.a); } modint operator*(const modint &o) const { return modint(static_cast(a) * o.a % MODULO); } modint operator/(const modint &o) const { return modint(static_cast(a) * ~o % MODULO); } modint &operator+=(const modint &o) { return *this = *this + o; } modint &operator-=(const modint &o) { return *this = *this - o; } modint &operator*=(const modint &o) { return *this = *this * o; } modint &operator/=(const modint &o) { return *this = *this / o; } modint operator~() const { return pow(MODULO - 2); } modint operator-() const { return a ? modint(MODULO - a) : modint(); } modint operator++() { return a == MODULO - 1 ? a = 0 : ++a, *this; } modint operator--() { return a ? --a : a = MODULO - 1, *this; } bool operator==(const modint &o) const { return a == o.a; } bool operator!=(const modint &o) const { return a != o.a; } bool operator<(const modint &o) const { return a < o.a; } bool operator<=(const modint &o) const { return a <= o.a; } bool operator>(const modint &o) const { return a > o.a; } bool operator>=(const modint &o) const { return a >= o.a; } explicit operator bool() const { return a; } explicit operator uint32() const { return a; } modint pow(uint32 x) const { uint64 t = a, u = 1; while (x) { if (x & 1) u = u * t % MODULO; t = (t * t) % MODULO; x >>= 1; } return modint(u); } }; #include #include #include #include template class sum_monoid { public: using size_type = S; using value_type = ::std::pair; value_type operator()(const value_type &x, const value_type &y) const { return value_type(x.first + y.first, x.second + y.second); } value_type identity() const { return value_type(T(0), S(0)); } value_type reverse(const value_type &x) const { return x; } }; template class plus_monoid { public: using value_type = T; value_type operator()(const value_type &x, const value_type &y) const { return x + y; } value_type identity() const { return value_type(0); } value_type reverse(const value_type &x) const { return x; } }; template class sum_plus { public: ::std::pair operator()(const ::std::pair &x, const T &y) const { return ::std::pair(x.first + y * x.second, x.second); } }; #include #include int main() { using uint = unsigned int; using mint = modint<1000000007>; using pm = std::pair; uint n; scanf("%u", &n); std::vector s(n); for (auto &e : s) scanf("%u", &e.first.a); for (auto &e : s) scanf("%u", &e.second.a); link_cut_tree, plus_monoid, sum_plus> T(s.begin(), s.end()); while (--n) { uint a, b; scanf("%u%u", &a, &b); --a;--b; T.link(a, b); } uint q; scanf("%u", &q); while (q--) { uint t, x, y; mint z; scanf("%u%u%u", &t, &x, &y); --x;--y; if (t) { printf("%u\n", T.fold(x, y).first.a); } else { scanf("%u", &z.a); T.update(x, y, mint(z)); } } return 0; }