This documentation is automatically generated by competitive-verifier/competitive-verifier
#define PROBLEM "https://judge.yosupo.jp/problem/dynamic_tree_vertex_add_subtree_sum"
#include <bits/stdc++.h>
using namespace std;
#include "tree/link_cut.hpp"
#include "other/types.hpp"
struct node : lct_node<node> {
using lct_node<node>::lct_node;
i64 val = 0, sum = 0, light = 0;
void set(i32 x) { val = x; }
void add(i32 x) { val += x; }
void pull() { sum = $(l)->sum + $(r)->sum + val + light; }
void add_light(ptr c) { light += c->sum; }
void sub_light(ptr c) { light -= c->sum; }
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
i32 n, q;
cin >> n >> q;
LCT<node> tr(n);
for (i32 i = 0, a; i < n; i++) cin >> a, tr[i]->set(a);
for (i32 i = 0, u, v; i < n - 1; i++) cin >> u >> v, tr.link(u, v);
while (q--) {
i32 t;
cin >> t;
if (t == 0) {
i32 a, b, c, d;
cin >> a >> b >> c >> d;
tr.cut(a, b), tr.link(c, d);
} else if (t == 1) {
i32 p, x;
cin >> p >> x;
tr.add(p, x);
} else {
i32 v, p;
cin >> v >> p;
tr.cut(v, p);
cout << tr[v]->sum << "\n";
tr.link(v, p);
}
}
}
#line 1 "test/yosupo/tree/dynamic_tree_vertex_add_subtree_sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/dynamic_tree_vertex_add_subtree_sum"
#include <bits/stdc++.h>
using namespace std;
#line 2 "tree/link_cut.hpp"
template<class node> struct LCT {
using ptr = node *;
vector<node> nodes;
LCT(int n = 0) : nodes(n) {}
auto operator[](int i) { return &nodes[i]; }
int id(ptr t) { return int(t - &nodes[0]); }
void splay(int v) { splay(&nodes[v]); }
void expose(int v) { expose(&nodes[v]); }
void expose_path(int u, int v) { evert(u), expose(v); }
void evert(int v) { evert(&nodes[v]); }
void link(int v, int p) { link(&nodes[v], &nodes[p]); }
void cut(int v, int p) { cut(&nodes[v], &nodes[p]); }
int lca(int u, int v) {
ptr l = lca(&nodes[u], &nodes[v]);
return l ? id(l) : -1;
}
template<class... T> void set(int v, T &&...x) {
expose(v), nodes[v].set(x...);
}
template<class... T> void add(int v, T &&...x) {
expose(v), nodes[v].add(x...);
}
static void splay(ptr t) {
for (t->_push(); state(t); rot(t)) {
ptr p = t->p, g = p->p;
if (g) g->_push();
p->_push(), t->_push();
if (state(p)) rot(state(t) == state(p) ? p : t);
}
}
static ptr expose(ptr t) {
ptr prv = 0;
for (ptr cur = t; cur; cur = cur->p) {
splay(cur);
if (cur->r) cur->_add_light(cur->r);
if (prv) cur->_sub_light(prv);
attach(cur, 1, exchange(prv, cur));
}
splay(t);
return prv;
}
static void evert(ptr t) { expose(t), t->_flip(); }
static void link(ptr v, ptr p) {
evert(v), expose(p);
assert(!v->p && !p->r);
attach(p, 1, v);
}
static void cut(ptr v, ptr p) {
evert(p), expose(v);
assert(!v->p && v->l == p);
attach(v, 0, p->p = 0);
}
static ptr lca(ptr u, ptr v) {
if (u == v) return u;
expose(u);
ptr l = expose(v);
return u->p ? l : 0;
}
static ptr &ch(ptr t, bool d) { return d ? t->r : t->l; }
static void attach(ptr p, int d, ptr c) {
if (c) c->p = p;
ch(p, d) = c, p->pull();
}
static int state(ptr t) {
if (!t->p) return 0;
return t == t->p->l ? -1 : t == t->p->r ? 1 : 0;
}
static void rot(ptr t) {
ptr p = t->p, g = p->p;
int d = state(t) == 1, dp = state(p);
t->p = p->p;
if (g) dp ? attach(g, dp == 1, t) : g->_change_light(p, t);
attach(p, d, ch(t, !d));
attach(t, !d, p);
}
};
template<class node> struct lct_node {
using ptr = node *;
static ptr $(ptr t) {
static node nil;
return t ?: &nil;
}
ptr p = 0, l = 0, r = 0;
bool rev = 0;
node *as_derived() { return (node *)this; };
void _push() {
if (exchange(rev, 0)) $(l)->_flip(), $(r)->_flip();
if constexpr (has_push<node>) as_derived()->push();
}
void _flip() {
swap(l, r), rev ^= 1;
if constexpr (has_flip<node>) as_derived()->flip();
}
void _add_light(ptr c) {
if constexpr (has_add_light<node>) as_derived()->add_light(c);
}
void _sub_light(ptr c) {
if constexpr (has_sub_light<node>) as_derived()->sub_light(c);
}
void _change_light(ptr prv, ptr cur) {
if constexpr (has_change_light<node>)
as_derived()->change_light(prv, cur);
}
// rip compile time
#define CHECK(a) \
template<class T, class = void> struct has_##a##_t : false_type {}; \
template<class T> struct has_##a##_t<T, void_t<decltype(&T::a)>> : true_type {}; \
template<class T> static constexpr bool has_##a = has_##a##_t<T>::value;
CHECK(push) CHECK(flip)
CHECK(add_light) CHECK(sub_light) CHECK(change_light)
#undef CHECK
};
#line 2 "other/types.hpp"
using i8 = int8_t;
using u8 = uint8_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using f32 = float;
using f64 = double;
using f80 = long double;
using str = string;
template<class T> using vec = vector<T>;
template<class E = i32> using graph = vec<vec<E>>;
#line 8 "test/yosupo/tree/dynamic_tree_vertex_add_subtree_sum.test.cpp"
struct node : lct_node<node> {
using lct_node<node>::lct_node;
i64 val = 0, sum = 0, light = 0;
void set(i32 x) { val = x; }
void add(i32 x) { val += x; }
void pull() { sum = $(l)->sum + $(r)->sum + val + light; }
void add_light(ptr c) { light += c->sum; }
void sub_light(ptr c) { light -= c->sum; }
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
i32 n, q;
cin >> n >> q;
LCT<node> tr(n);
for (i32 i = 0, a; i < n; i++) cin >> a, tr[i]->set(a);
for (i32 i = 0, u, v; i < n - 1; i++) cin >> u >> v, tr.link(u, v);
while (q--) {
i32 t;
cin >> t;
if (t == 0) {
i32 a, b, c, d;
cin >> a >> b >> c >> d;
tr.cut(a, b), tr.link(c, d);
} else if (t == 1) {
i32 p, x;
cin >> p >> x;
tr.add(p, x);
} else {
i32 v, p;
cin >> v >> p;
tr.cut(v, p);
cout << tr[v]->sum << "\n";
tr.link(v, p);
}
}
}
| Env | Name | Status | Elapsed | Memory |
|---|---|---|---|---|
| g++ | almost_line_00 |
|
327 ms | 14 MB |
| g++ | almost_line_01 |
|
333 ms | 14 MB |
| g++ | example_00 |
|
6 ms | 4 MB |
| g++ | max_random_00 |
|
356 ms | 14 MB |
| g++ | max_random_01 |
|
309 ms | 14 MB |
| g++ | max_random_02 |
|
326 ms | 14 MB |
| g++ | max_random_03 |
|
334 ms | 14 MB |
| g++ | max_random_04 |
|
343 ms | 14 MB |
| g++ | random_00 |
|
215 ms | 10 MB |
| g++ | random_01 |
|
233 ms | 12 MB |
| g++ | random_02 |
|
133 ms | 6 MB |
| g++ | random_03 |
|
133 ms | 13 MB |
| g++ | random_04 |
|
88 ms | 4 MB |
| g++ | small_00 |
|
6 ms | 4 MB |
| g++ | small_01 |
|
5 ms | 4 MB |
| g++ | small_02 |
|
5 ms | 4 MB |
| g++ | small_03 |
|
5 ms | 4 MB |
| g++ | small_04 |
|
5 ms | 4 MB |
| clang++ | almost_line_00 |
|
395 ms | 14 MB |
| clang++ | almost_line_01 |
|
372 ms | 14 MB |
| clang++ | example_00 |
|
5 ms | 4 MB |
| clang++ | max_random_00 |
|
403 ms | 14 MB |
| clang++ | max_random_01 |
|
399 ms | 14 MB |
| clang++ | max_random_02 |
|
389 ms | 14 MB |
| clang++ | max_random_03 |
|
408 ms | 14 MB |
| clang++ | max_random_04 |
|
392 ms | 14 MB |
| clang++ | random_00 |
|
243 ms | 10 MB |
| clang++ | random_01 |
|
250 ms | 12 MB |
| clang++ | random_02 |
|
151 ms | 6 MB |
| clang++ | random_03 |
|
140 ms | 13 MB |
| clang++ | random_04 |
|
101 ms | 4 MB |
| clang++ | small_00 |
|
6 ms | 4 MB |
| clang++ | small_01 |
|
5 ms | 4 MB |
| clang++ | small_02 |
|
5 ms | 4 MB |
| clang++ | small_03 |
|
5 ms | 4 MB |
| clang++ | small_04 |
|
5 ms | 4 MB |