This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_add_path_sum"
#include "../../Src/Template/IOSetting.hpp"
#include "../../Src/DataStructure/FenwickTree/FenwickTree.hpp"
#include "../../Src/Algebra/Group/AdditiveGroup.hpp"
#include "../../Src/Graph/Tree/HeavyLightDecomposition.hpp"
#include <cassert>
#include <iostream>
#include <utility>
#include <vector>
int main() {
using namespace zawa;
SetFastIO();
int N, Q;
std::cin >> N >> Q;
std::vector<int> A(N);
for (int& a : A) std::cin >> a;
std::vector<std::vector<int>> T(N);
for (int _{} ; _ < N - 1 ; _++) {
int u, v;
std::cin >> u >> v;
T[u].push_back(v);
T[v].push_back(u);
// AddEdge(T, u, v);
}
HeavyLightDecomposition hld(T);
std::vector<long long> init(N);
for (int v{} ; v < N ; v++) {
init[hld[v]] = A[v];
}
FenwickTree<AdditiveGroup<long long>> fen{init};
while (Q--) {
int t;
std::cin >> t;
if (t == 0) {
int p, x;
std::cin >> p >> x;
fen.operation(hld[p], x);
}
else if (t == 1) {
int u, v;
std::cin >> u >> v;
long long ans{};
for (auto [u, v] : hld(u, v)) {
u = hld[u];
v = hld[v];
if (u > v) std::swap(u, v);
ans += fen.product(u, v + 1);
}
std::cout << ans << '\n';
}
else {
assert(false);
}
}
}
#line 1 "Test/LC/vertex_add_path_sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_add_path_sum"
#line 2 "Src/Template/IOSetting.hpp"
#line 2 "Src/Template/TypeAlias.hpp"
#include <cstdint>
#include <cstddef>
namespace zawa {
using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using usize = std::size_t;
} // namespace zawa
#line 4 "Src/Template/IOSetting.hpp"
#include <iostream>
#include <iomanip>
namespace zawa {
void SetFastIO() {
std::cin.tie(nullptr)->sync_with_stdio(false);
}
void SetPrecision(u32 dig) {
std::cout << std::fixed << std::setprecision(dig);
}
} // namespace zawa
#line 2 "Src/DataStructure/FenwickTree/FenwickTree.hpp"
#line 2 "Src/Algebra/Group/GroupConcept.hpp"
#line 2 "Src/Algebra/Monoid/MonoidConcept.hpp"
#line 2 "Src/Algebra/Semigroup/SemigroupConcept.hpp"
#include <concepts>
namespace zawa {
namespace concepts {
template <class T>
concept Semigroup = requires {
typename T::Element;
{ T::operation(std::declval<typename T::Element>(), std::declval<typename T::Element>()) } -> std::same_as<typename T::Element>;
};
} // namespace concepts
} // namespace zawa
#line 4 "Src/Algebra/Monoid/MonoidConcept.hpp"
#line 6 "Src/Algebra/Monoid/MonoidConcept.hpp"
namespace zawa {
namespace concepts {
template <class T>
concept Identitiable = requires {
typename T::Element;
{ T::identity() } -> std::same_as<typename T::Element>;
};
template <class T>
concept Monoid = Semigroup<T> and Identitiable<T>;
} // namespace
} // namespace zawa
#line 4 "Src/Algebra/Group/GroupConcept.hpp"
namespace zawa {
namespace concepts {
template <class T>
concept Inversible = requires {
typename T::Element;
{ T::inverse(std::declval<typename T::Element>()) } -> std::same_as<typename T::Element>;
};
template <class T>
concept Group = Monoid<T> and Inversible<T>;
} // namespace Concept
} // namespace zawa
#line 5 "Src/DataStructure/FenwickTree/FenwickTree.hpp"
#include <vector>
#include <cassert>
#include <ostream>
#include <functional>
#include <type_traits>
namespace zawa {
template <concepts::Group Group>
class FenwickTree {
public:
using VM = Group;
using V = typename VM::Element;
FenwickTree() = default;
explicit FenwickTree(usize n) : m_n{ n }, m_bitwidth{ std::__lg(n) + 1 }, m_a(n), m_dat(n + 1, VM::identity()) {
m_dat.shrink_to_fit();
m_a.shrink_to_fit();
}
explicit FenwickTree(const std::vector<V>& a) : m_n{ a.size() }, m_bitwidth{ std::__lg(a.size()) + 1 }, m_a(a), m_dat(a.size() + 1, VM::identity()) {
m_dat.shrink_to_fit();
m_a.shrink_to_fit();
for (i32 i{} ; i < static_cast<i32>(m_n) ; i++) {
addDat(i, a[i]);
}
}
inline usize size() const noexcept {
return m_n;
}
// return a[i]
const V& get(usize i) const noexcept {
assert(i < size());
return m_a[i];
}
// return a[i]
const V& operator[](usize i) const noexcept {
assert(i < size());
return m_a[i];
}
// a[i] <- a[i] + v
void operation(usize i, const V& v) {
assert(i < size());
addDat(i, v);
m_a[i] = VM::operation(m_a[i], v);
}
// a[i] <- v
void assign(usize i, const V& v) {
assert(i < size());
addDat(i, VM::operation(VM::inverse(m_a[i]), v));
m_a[i] = v;
}
// return a[0] + a[1] + ... + a[r - 1]
V prefixProduct(usize r) const {
assert(r <= size());
return product(r);
}
// return a[l] + a[l + 1] ... + a[r - 1]
V product(usize l, usize r) const {
assert(l <= r and r <= size());
return VM::operation(VM::inverse(product(l)), product(r));
}
template <class Function>
usize maxRight(usize l, const Function& f) const {
static_assert(std::is_convertible_v<decltype(f), std::function<bool(V)>>, "maxRight's argument f must be function bool(T)");
assert(l < size());
V sum{ VM::inverse(product(l)) };
usize r{};
for (usize bit{ m_bitwidth } ; bit ; ) {
bit--;
usize nxt{ r | (1u << bit) };
if (nxt < m_dat.size() and f(VM::operation(sum, m_dat[nxt]))) {
sum = VM::operation(sum, m_dat[nxt]);
r = std::move(nxt);
}
}
assert(l <= r);
return r;
}
template <class Function>
usize minLeft(usize r, const Function& f) const {
static_assert(std::is_convertible_v<decltype(f), std::function<bool(V)>>, "minLeft's argument f must be function bool(T)");
assert(r <= size());
V sum{ product(r) };
usize l{};
for (usize bit{ m_bitwidth } ; bit ; ) {
bit--;
usize nxt{ l | (1u << bit) };
if (nxt <= r and not f(VM::operation(VM::inverse(m_dat[nxt]), sum))) {
sum = VM::operation(VM::inverse(m_dat[nxt]), sum);
l = std::move(nxt);
}
}
assert(l <= r);
return l;
}
// debug print
friend std::ostream& operator<<(std::ostream& os, const FenwickTree& ft) {
for (usize i{} ; i <= ft.size() ; i++) {
os << ft.prefixProduct(i) << (i == ft.size() ? "" : " ");
}
return os;
}
private:
usize m_n{};
usize m_bitwidth{};
std::vector<V> m_a, m_dat;
constexpr i32 lsb(i32 x) const noexcept {
return x & -x;
}
// a[i] <- a[i] + v
void addDat(i32 i, const V& v) {
assert(0 <= i and i < static_cast<i32>(m_n));
for ( i++ ; i < static_cast<i32>(m_dat.size()) ; i += lsb(i)) {
m_dat[i] = VM::operation(m_dat[i], v);
}
}
// return a[0] + a[1] + .. + a[i - 1]
V product(i32 i) const {
assert(0 <= i and i <= static_cast<i32>(m_n));
V res{ VM::identity() };
for ( ; i > 0 ; i -= lsb(i)) {
res = VM::operation(res, m_dat[i]);
}
return res;
}
};
} // namespace zawa
#line 2 "Src/Algebra/Group/AdditiveGroup.hpp"
namespace zawa {
template <class T>
class AdditiveGroup {
public:
using Element = T;
static constexpr T identity() noexcept {
return T{};
}
static constexpr T operation(const T& l, const T& r) noexcept {
return l + r;
}
static constexpr T inverse(const T& v) noexcept {
return -v;
}
};
} // namespace zawa
#line 2 "Src/Graph/Tree/HeavyLightDecomposition.hpp"
#line 4 "Src/Graph/Tree/HeavyLightDecomposition.hpp"
#include <algorithm>
#line 7 "Src/Graph/Tree/HeavyLightDecomposition.hpp"
#include <cmath>
#include <limits>
#include <utility>
#line 11 "Src/Graph/Tree/HeavyLightDecomposition.hpp"
namespace zawa {
template <class V>
class HeavyLightDecomposition {
public:
static constexpr V Invalid() noexcept {
return INVALID;
}
HeavyLightDecomposition() = default;
HeavyLightDecomposition(std::vector<std::vector<V>> T, V root = 0u)
: n_{T.size()}, par_(n_), top_(n_), idx_(n_),
inv_(n_), size_(n_, usize{1}), dep_(n_) {
auto dfs1{[&](auto dfs, V v, V p, usize d) -> usize {
par_[v] = p;
dep_[v] = d;
if (p != INVALID) {
for (u32 i{} ; i + 1 < T[v].size() ; i++) if (T[v][i] == p) {
std::swap(T[v][i], T[v].back());
break;
}
assert(T[v].back() == p);
T[v].pop_back();
}
for (V x : T[v]) {
size_[v] += dfs(dfs, x, v, d + 1);
}
for (u32 i{1} ; i < T[v].size() ; i++) if (size_[T[v][0]] < size_[T[v][i]]) {
std::swap(T[v][0], T[v][i]);
}
return size_[v];
}};
auto dfs2{[&](auto dfs, V v, V idx, V top) -> V {
idx_[v] = idx++;
inv_[idx_[v]] = v;
top_[v] = top;
if (T[v].size()) {
idx = dfs(dfs, T[v][0], idx, top);
for (u32 i{1} ; i < T[v].size() ; i++) {
idx = dfs(dfs, T[v][i], idx, T[v][i]);
}
}
return idx;
}};
dfs1(dfs1, root, INVALID, 0u);
dfs2(dfs2, root, 0u, root);
}
inline usize size() const noexcept {
return n_;
}
usize size(V v) const noexcept {
assert(v < (V)size());
return size_[v];
}
usize depth(V v) const noexcept {
assert(v < (V)size());
return dep_[v];
}
V parent(V v) const noexcept {
assert(v < (V)size());
return par_[v];
}
V index(V v) const noexcept {
assert(v < (V)size());
return idx_[v];
}
V operator[](V v) const noexcept {
assert(v < (V)size());
return idx_[v];
}
std::vector<std::pair<V, V>> decomp(V s, V t) const {
assert(s < (V)size());
assert(t < (V)size());
std::vector<std::pair<V, V>> res, ser;
while (top_[s] != top_[t]) {
if (dep_[top_[s]] >= dep_[top_[t]]) {
res.emplace_back(s, top_[s]);
s = top_[s];
if (par_[s] != INVALID) s = par_[s];
}
else {
ser.emplace_back(top_[t], t);
t = top_[t];
if (par_[t] != INVALID) t = par_[t];
}
}
res.emplace_back(s, t);
std::reverse(ser.begin(), ser.end());
res.insert(res.end(), ser.begin(), ser.end());
return res;
}
std::vector<std::pair<V, V>> operator()(V s, V t) const {
return decomp(s, t);
}
V lca(V u, V v) const {
assert(u < (V)size());
assert(v < (V)size());
while (top_[u] != top_[v]) {
if (dep_[top_[u]] >= dep_[top_[v]]) {
u = top_[u];
if (par_[u] != INVALID) u = par_[u];
}
else {
v = top_[v];
if (par_[v] != INVALID) v = par_[v];
}
}
return (dep_[u] <= dep_[v] ? u : v);
}
// pはvの祖先か?
bool isAncestor(V v, V p) {
assert(v < size());
assert(p < size());
if (dep_[v] < dep_[p]) return false;
while (v != INVALID and top_[v] != top_[p]) {
v = par_[top_[v]];
}
return v != INVALID;
}
V levelAncestor(V v, usize step) const {
assert(v < (V)size());
if (step > dep_[v]) return INVALID;
while (true) {
usize dist{dep_[v] - dep_[top_[v]]};
if (dist >= step) break;
step -= dist + 1;
v = par_[top_[v]];
}
step = (dep_[v] - dep_[top_[v]]) - step;
return inv_[idx_[top_[v]] + step];
}
V jump(V s, V t, usize step) const {
assert(s < (V)size());
assert(t < (V)size());
V uu{INVALID}, vv{INVALID};
usize d{};
for (auto [u, v] : decomp(s, t)) {
usize dist{std::max(dep_[u], dep_[v]) - std::min(dep_[u], dep_[v])};
if (dist >= step) {
uu = u;
vv = v;
d = dist;
break;
}
step -= dist + 1;
}
if (uu == INVALID) return INVALID;
if (dep_[uu] <= dep_[vv]) {
return inv_[idx_[uu] + step];
}
else {
return inv_[idx_[vv] + (d - step)];
}
}
usize distance(V s, V t) const {
assert(s < (V)size());
assert(t < (V)size());
usize res{};
for (auto [u, v] : decomp(s, t)) {
if (dep_[u] > dep_[v]) std::swap(u, v);
res += dep_[v] - dep_[u];
}
return res;
}
private:
static constexpr V INVALID{static_cast<V>(-1)};
usize n_{};
std::vector<V> par_{}, top_{}, idx_{}, inv_{};
std::vector<usize> size_{}, dep_{};
};
} // namespace zawa
#line 7 "Test/LC/vertex_add_path_sum.test.cpp"
#line 12 "Test/LC/vertex_add_path_sum.test.cpp"
int main() {
using namespace zawa;
SetFastIO();
int N, Q;
std::cin >> N >> Q;
std::vector<int> A(N);
for (int& a : A) std::cin >> a;
std::vector<std::vector<int>> T(N);
for (int _{} ; _ < N - 1 ; _++) {
int u, v;
std::cin >> u >> v;
T[u].push_back(v);
T[v].push_back(u);
// AddEdge(T, u, v);
}
HeavyLightDecomposition hld(T);
std::vector<long long> init(N);
for (int v{} ; v < N ; v++) {
init[hld[v]] = A[v];
}
FenwickTree<AdditiveGroup<long long>> fen{init};
while (Q--) {
int t;
std::cin >> t;
if (t == 0) {
int p, x;
std::cin >> p >> x;
fen.operation(hld[p], x);
}
else if (t == 1) {
int u, v;
std::cin >> u >> v;
long long ans{};
for (auto [u, v] : hld(u, v)) {
u = hld[u];
v = hld[v];
if (u > v) std::swap(u, v);
ans += fen.product(u, v + 1);
}
std::cout << ans << '\n';
}
else {
assert(false);
}
}
}