algo

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub dnx04/algo

:heavy_check_mark: tests/Vertex_Add_Path_Sum.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/vertex_add_path_sum"

#include "../misc/macros.h"
#include "../ds/Fenwick.h"
#include "../ds/HLD.h"

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, q;
  cin >> n >> q;
  Fenwick<i64> fw(n);
  vector<i64> a(n);
  vector<vi> g(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].eb(v), g[v].eb(u);
  }
  auto hld = HLD(g);
  for (int i = 0; i < n; ++i) fw.add(hld.idx(i), a[i]);
  while (q--) {
    int cmd;
    cin >> cmd;
    if (cmd == 0) {
      int p, x;
      cin >> p >> x;
      fw.add(hld.idx(p), x);
    } else {
      int u, v;
      cin >> u >> v;
      i64 res = 0;
      auto paths = hld.query_path(u, v);
      for (auto [v1, v2] : paths) {
        int u = v1, v = v2;
        if (u > v) swap(u, v);
        res += fw.sum(u, v);
      }
      cout << res << '\n';
    }
  }
}
#line 1 "tests/Vertex_Add_Path_Sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_add_path_sum"

#line 1 "misc/macros.h"
// #pragma GCC optimize("Ofast,unroll-loops")       // unroll long, simple loops
// #pragma GCC target("avx2,fma")                   // vectorizing code
// #pragma GCC target("lzcnt,popcnt,abm,bmi,bmi2")  // for fast bitset operation

#include <bits/extc++.h>
#include <tr2/dynamic_bitset>

using namespace std;
using namespace __gnu_pbds;  // ordered_set, gp_hash_table
// using namespace __gnu_cxx; // rope

// for templates to work
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#define pb push_back
#define eb emplace_back
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using ld = long double;
using pii = pair<i32, i32>;
using vi = vector<i32>;

// fast map
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash {  // customize hash function for gp_hash_table
  int operator()(int x) const { return x ^ RANDOM; }
};
gp_hash_table<int, int, chash> table;

/* ordered set
    find_by_order(k): returns an iterator to the k-th element (0-based)
    order_of_key(k): returns the number of elements in the set that are strictly less than k
*/
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

/*  rope
    rope <int> cur = v.substr(l, r - l + 1);
    v.erase(l, r - l + 1);
    v.insert(v.mutable_begin(), cur);
*/
#line 1 "ds/Fenwick.h"
// for 2d fenwick just add a for
template <class T>
struct Fenwick {  // 1-indexed
  int n;
  vector<T> t;
  Fenwick(int n) : n(n), t(n + 1, T(0)) {}
  void add(int p, T v) {
    while (p <= n) t[p] += v, p += (p & -p);
  }
  T sum(int p) {
    T res = 0;
    while (p) res += t[p], p -= (p & -p);
    return res;
  }
  // [l, r)
  T sum(int l, int r) {
    if (l > r) return T(0);
    return sum(r) - sum(l - 1);
  }
};
#line 1 "ds/HLD.h"
template <class G>
struct HLD {
  const G& g;
  int n, t = 0;
  vi sub, dep, par, head, pos, heavy;
  HLD(const G& g, int root = 0) : g(g), n(sz(g)), sub(n), dep(n), par(n), head(n), pos(n), heavy(n, -1) {
    par[root] = -1;
    dfs_sub(root);
    dfs_hld(root, root);
  }
  void dfs_sub(int u) {
    sub[u] = 1;
    for (int v : g[u])
      if (v != par[u]) {
        dep[v] = dep[u] + 1, par[v] = u;
        dfs_sub(v);
        sub[u] += sub[v];
        if (heavy[u] == -1 || sub[v] > sub[heavy[u]]) heavy[u] = v;
      }
  }
  void dfs_hld(int u, int h) {
    head[u] = h, pos[u] = ++t;
    if (heavy[u] != -1) dfs_hld(heavy[u], h);
    for (int v : g[u])
      if (v != par[u] && v != heavy[u]) dfs_hld(v, v);
  }
  int idx(int u) const { return pos[u]; }
  pii query_subtree(int u) { return {pos[u], pos[u] + sub[u] - 1}; }
  vector<pii> query_path(int u, int v) {
    vector<pii> l, r;
    while (head[u] != head[v]) {
      if (dep[head[u]] > dep[head[v]]) {
        l.pb({pos[u], pos[head[u]]});
        u = par[head[u]];
      } else {
        r.pb({pos[head[v]], pos[v]});
        v = par[head[v]];
      }
    }
    if (dep[u] > dep[v]) l.pb({pos[u], pos[v]});
    else r.pb({pos[u], pos[v]});
    reverse(all(r));
    l.insert(l.end(), all(r));
    return l;
  }
  int lca(int u, int v) {
    for (; head[u] != head[v]; u = par[head[u]])
      if (dep[head[u]] < dep[head[v]]) swap(u, v);
    return dep[u] < dep[v] ? u : v;
  }
};
#line 6 "tests/Vertex_Add_Path_Sum.test.cpp"

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, q;
  cin >> n >> q;
  Fenwick<i64> fw(n);
  vector<i64> a(n);
  vector<vi> g(n);
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    cin >> u >> v;
    g[u].eb(v), g[v].eb(u);
  }
  auto hld = HLD(g);
  for (int i = 0; i < n; ++i) fw.add(hld.idx(i), a[i]);
  while (q--) {
    int cmd;
    cin >> cmd;
    if (cmd == 0) {
      int p, x;
      cin >> p >> x;
      fw.add(hld.idx(p), x);
    } else {
      int u, v;
      cin >> u >> v;
      i64 res = 0;
      auto paths = hld.query_path(u, v);
      for (auto [v1, v2] : paths) {
        int u = v1, v = v2;
        if (u > v) swap(u, v);
        res += fw.sum(u, v);
      }
      cout << res << '\n';
    }
  }
}
Back to top page