Submit Info #12872

Problem Lang User Status Time Memory
Dynamic Tree Vertex Add Path Sum cpp ei1333 AC 587 ms 15.98 MiB

ケース詳細
Name Status Time Memory
example_00 AC 3 ms 0.67 MiB
max_random_00 AC 554 ms 15.96 MiB
max_random_01 AC 587 ms 15.98 MiB
max_random_02 AC 587 ms 15.95 MiB
random_00 AC 332 ms 10.46 MiB
random_01 AC 399 ms 12.16 MiB
random_02 AC 191 ms 4.95 MiB
random_03 AC 235 ms 12.93 MiB
random_04 AC 114 ms 2.17 MiB
small_00 AC 3 ms 0.69 MiB
small_01 AC 0 ms 0.72 MiB
small_02 AC 0 ms 0.68 MiB

#include <bits/stdc++.h> using namespace std; using int64 = long long; const int mod = 998244353; const int64 infll = (1LL << 60) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 > &p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in : v) is >> in; return is; } template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template< typename T = int64 > vector< T > make_v(size_t a) { return vector< T >(a); } template< typename T, typename... Ts > auto make_v(size_t a, Ts... ts) { return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...)); } template< typename T, typename V > typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) { t = v; } template< typename T, typename V > typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) { for(auto &e : t) fill_v(e, v); } template< typename F > struct FixPoint : F { FixPoint(F &&f) : F(forward< F >(f)) {} template< typename... Args > decltype(auto) operator()(Args &&... args) const { return F::operator()(*this, forward< Args >(args)...); } }; template< typename F > inline decltype(auto) MFP(F &&f) { return FixPoint< F >{forward< F >(f)}; } /** * @brief Lazy-Splay-Tree(遅延伝搬Splay木) */ template< typename Monoid = int, typename OperatorMonoid = Monoid > struct LazySplayTree { public: using F = function< Monoid(Monoid, Monoid) >; using G = function< Monoid(Monoid, OperatorMonoid) >; using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >; using S = function< Monoid(Monoid) >; struct Node { Node *l, *r, *p; Monoid key, sum; OperatorMonoid lazy; bool rev; size_t sz; bool is_root() const { return !p || (p->l != this && p->r != this); } Node(const Monoid &key, const OperatorMonoid &om) : key(key), sum(key), lazy(om), sz(1), rev(false), l(nullptr), r(nullptr), p(nullptr) {} }; LazySplayTree(const F &f, const Monoid &M1) : LazySplayTree(f, [](const Monoid &a) { return a; }, M1) {} LazySplayTree(const F &f, const S &s, const Monoid &M1) : LazySplayTree(f, G(), H(), s, M1, OperatorMonoid()) {} LazySplayTree(const F &f, const G &g, const H &h, const S &s, const Monoid &M1, const OperatorMonoid &OM0) : f(f), g(g), h(h), s(s), M1(M1), OM0(OM0) {} inline size_t count(const Node *t) { return t ? t->sz : 0; } inline const Monoid &sum(const Node *t) { return t ? t->sum : M1; } Node *alloc(const Monoid &v = Monoid()) { return new Node(v, OM0); } void splay(Node *t) { push(t); while(!t->is_root()) { auto *q = t->p; if(q->is_root()) { push(q), push(t); if(q->l == t) rotr(t); else rotl(t); } else { auto *r = q->p; push(r), push(q), push(t); if(r->l == q) { if(q->l == t) rotr(q), rotr(t); else rotl(t), rotr(t); } else { if(q->r == t) rotl(q), rotl(t); else rotr(t), rotl(t); } } } } Node *push_front(Node *t, const Monoid &v = Monoid()) { if(!t) { t = alloc(v); return t; } else { Node *cur = get_left(t), *z = alloc(v); splay(cur); z->p = cur; cur->l = z; splay(z); return z; } } Node *push_back(Node *t, const Monoid &v = Monoid()) { if(!t) { t = alloc(v); return t; } else { Node *cur = get_right(t), *z = alloc(v); splay(cur); z->p = cur; cur->r = z; splay(z); return z; } } Node *erase(Node *t) { splay(t); Node *x = t->l, *y = t->r; delete t; if(!x) { t = y; if(t) t->p = nullptr; } else if(!y) { t = x; t->p = nullptr; } else { x->p = nullptr; t = get_right(x); splay(t); t->r = y; y->p = t; } return t; } Node *get_left(Node *t) const { while(t->l) t = t->l; return t; } Node *get_right(Node *t) const { while(t->r) t = t->r; return t; } void set_propagate(Node *&t, int a, int b, const OperatorMonoid &pp) { auto x = split(t, a); auto y = split(x.second, b - a); set_propagate(y.first, pp); t = merge(x.first, y.first, y.second); } virtual void set_propagate(Node *&t, const OperatorMonoid &pp) { propagate(t, pp); push(t); } pair< Node *, Node * > split(Node *t, int k) { if(!t) return {nullptr, nullptr}; push(t); if(k <= count(t->l)) { auto x = split(t->l, k); t->l = x.second; t->p = nullptr; if(x.second) x.second->p = t; return {x.first, update(t)}; } else { auto x = split(t->r, k - count(t->l) - 1); t->r = x.first; t->p = nullptr; if(x.first) x.first->p = t; return {update(t), x.second}; } } template< typename ... Args > Node *merge(Node *l, Args ...rest) { Node *r = merge(rest...); if(!l && !r) return nullptr; if(!l) return splay(r), r; if(!r) return splay(l), l; splay(l), splay(r); l = get_right(l); splay(l); l->r = r; r->p = l; update(l); return l; } void insert(Node *&t, int k, const Monoid &v) { auto x = split(t, k); t = merge(merge(x.first, alloc(v)), x.second); } Monoid erase(Node *&t, int k) { auto x = split(t, k); auto y = split(x.second, 1); auto v = y.first->c; delete y.first; t = merge(x.first, y.second); return v; } Monoid query(Node *&t, int a, int b) { auto x = split(t, a); auto y = split(x.second, b - a); auto ret = sum(y.first); t = merge(x.first, y.first, y.second); return ret; } Node *build(const vector< Monoid > &v) { return build(0, (int) v.size(), v); } void toggle(Node *t) { swap(t->l, t->r); t->sum = s(t->sum); t->rev ^= true; } Node *update(Node *t) { t->sz = 1; t->sum = t->key; if(t->l) t->sz += t->l->sz, t->sum = f(t->l->sum, t->sum); if(t->r) t->sz += t->r->sz, t->sum = f(t->sum, t->r->sum); return t; } tuple< Node *, Node *, Node * > split3(Node *t, int a, int b) { auto x = split(t, a); auto y = split(x.second, b - a); return make_tuple(x.first, y.first, y.second); } void push(Node *t) { if(t->lazy != OM0) { if(t->l) propagate(t->l, t->lazy); if(t->r) propagate(t->r, t->lazy); t->lazy = OM0; } if(t->rev) { if(t->l) toggle(t->l); if(t->r) toggle(t->r); t->rev = false; } } private: const Monoid M1; const OperatorMonoid OM0; const F f; const G g; const H h; const S s; Node *build(int l, int r, const vector< Monoid > &v) { if(l + 1 >= r) return alloc(v[l]); return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v)); } void propagate(Node *t, const OperatorMonoid &x) { t->lazy = h(t->lazy, x); t->key = g(t->key, x); t->sum = g(t->sum, x); } void rotr(Node *t) { auto *x = t->p, *y = x->p; if((x->l = t->r)) t->r->p = x; t->r = x, x->p = t; update(x), update(t); if((t->p = y)) { if(y->l == x) y->l = t; if(y->r == x) y->r = t; update(y); } } void rotl(Node *t) { auto *x = t->p, *y = x->p; if((x->r = t->l)) t->l->p = x; t->l = x, x->p = t; update(x), update(t); if((t->p = y)) { if(y->l == x) y->l = t; if(y->r == x) y->r = t; update(y); } } Node *merge(Node *l) { return l; } }; /** * @brief Link-Cut-Tree */ template< typename Monoid = int, typename OperatorMonoid = Monoid > struct LinkCutTree : LazySplayTree< Monoid, OperatorMonoid > { using LST = LazySplayTree< Monoid, OperatorMonoid >; using LST::LazySplayTree; using Node = typename LST::Node; Node *expose(Node *t) { Node *rp = nullptr; for(Node *cur = t; cur; cur = cur->p) { this->splay(cur); cur->r = rp; this->update(cur); rp = cur; } this->splay(t); return rp; } void link(Node *child, Node *parent) { expose(child); expose(parent); child->p = parent; parent->r = child; this->update(parent); } void cut(Node *child) { expose(child); auto *parent = child->l; child->l = nullptr; parent->p = nullptr; this->update(child); } void evert(Node *t) { expose(t); this->toggle(t); this->push(t); } Node *lca(Node *u, Node *v) { if(get_root(u) != get_root(v)) return nullptr; expose(u); return expose(v); } void set_propagate(Node *t, const OperatorMonoid &x) { expose(t); LST::set_propagate(t, x); } Node *get_kth(Node *x, int k) { expose(x); while(x) { this->push(x); if(x->r && x->r->sz > k) { x = x->r; } else { if(x->r) k -= x->r->sz; if(k == 0) return x; k -= 1; x = x->l; } } return nullptr; } Node *get_root(Node *x) { expose(x); while(x->l) { this->push(x); x = x->l; } return x; } const Monoid &query(Node *t) { expose(t); return t->sum; } }; int main() { int N, Q; cin >> N >> Q; using LCT = LinkCutTree< int64, int >; auto add = [](int64 a, int64 b) { return a + b; }; auto s = [](int64 a) { return a; }; LCT lct(add, add, add, s, 0, 0); vector< int > A(N); cin >> A; vector< LCT::Node * > vs(N); for(int i = 0; i < N; i++) { vs[i] = lct.alloc(A[i]); } for(int i = 1; i < N; i++) { int a, b; cin >> a >> b; lct.evert(vs[a]); lct.link(vs[a], vs[b]); } while(Q--) { int T; cin >> T; if(T == 0) { int U, V, W, X; cin >> U >> V >> W >> X; lct.evert(vs[U]); lct.cut(vs[V]); lct.evert(vs[W]); lct.link(vs[W], vs[X]); } else if(T == 1) { int P, X; cin >> P >> X; lct.expose(vs[P]); vs[P]->key += X; lct.update(vs[P]); } else { int U, V; cin >> U >> V; lct.evert(vs[U]); cout << lct.query(vs[V]) << "\n"; } } }