cpp-library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub shino16/cpp-library

:heavy_check_mark: test/graph/tree.hld.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2667

#include "graph/tree/hld.hpp"
#include "ds/range_fenwick.hpp"

int n, q;
vector<int> G[150000];
int c, u, v;

int main() {
  scanf("%d%d", &n, &q);
  rep(_, n - 1) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
  }
  hld hld(G, 0);
  range_fenwick_tree fwk(n, addition<ll>{}, multiplies<ll>{});
  while (q--) {
    scanf("%d%d%d", &c, &u, &v);
    if (c == 0) {
      ll ans = 0;
      hld.paths(u, v, [&](int l, int r) { ans += fwk.sum(l, r); });
      printf("%lld\n", ans);
    } else {
      hld.subtree(u, [&](int l, int r) { fwk.add(l, r, v); });
    }
  }
}
#line 1 "test/graph/tree.hld.test.cpp"
// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2667

#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "graph/traits.hpp"

struct unit_edge {
  int to;
  operator int() const { return to; }
  int w() const { return 1; }
};

template <class Weight>
struct weighted_edge {
  int to;
  Weight weight;
  operator int() const { return to; }
  Weight w() const { return weight; }
};

template <class Inner>
struct basic_graph {
  using weight_type = void;
  const Inner& inner;
  basic_graph(const Inner& g) : inner(g) { }
  template <class F>
  void adj(int v, F f) const {
    for (auto u : inner[v]) f(unit_edge{u});
  }
  int deg(int v) const { return inner[v].size(); }
};

template <class Inner, class Weight>
struct basic_weighted_graph {
  using weight_type = Weight;
  const Inner& inner;
  basic_weighted_graph(const Inner& g) : inner(g) { }
  template <class F>
  void adj(int v, F f) const {
    for (auto [u, w] : inner[v]) f(weighted_edge<weight_type>{u, w});
  }
  int deg(int v) const { return inner[v].size(); }
};

template <class Inner>
struct graph_trait {
  using weight_type = typename Inner::weight_type;
  const Inner& g;
  graph_trait(const Inner& g) : g(g) { }
  int size() const { return g.size(); }
  template <class F>
  void adj(int v, F f) const {
    g.adj(v, f);
  }
  decltype(auto) operator[](int v) const { return g[v]; }
};

template <class T>
constexpr bool is_weighted_v =
    !is_same_v<typename graph_trait<T>::weight_type, void>;

template <class T>
using weight_t =
    conditional_t<is_weighted_v<T>, typename graph_trait<T>::weight_type, int>;

template <class T>
using edge_t =
    conditional_t<is_weighted_v<T>, weighted_edge<weight_t<T>>, unit_edge>;

template <size_t N>
struct graph_trait<vector<int>[N]> : basic_graph<vector<int>[N]> {
  using basic_graph<vector<int>[N]>::basic_graph;
  int size() const { return N; }
};

template <>
struct graph_trait<vector<vector<int>>> : basic_graph<vector<vector<int>>> {
  using basic_graph<vector<vector<int>>>::basic_graph;
  int size() const { return this->inner.size(); }
};

template <size_t N, class Weight>
struct graph_trait<vector<pair<int, Weight>>[N]>
    : basic_weighted_graph<vector<pair<int, Weight>>[N], Weight> {
      using basic_weighted_graph<
          vector<pair<int, Weight>>[N], Weight>::basic_weighted_graph;
      int size() const { return N; }
    };

template <class Weight>
struct graph_trait<vector<vector<pair<int, Weight>>>>
    : basic_weighted_graph<vector<vector<pair<int, Weight>>>, Weight> {
  using basic_weighted_graph<
      vector<vector<pair<int, Weight>>>, Weight>::basic_weighted_graph;
  int size() const { return this->inner.size(); }
};
#line 3 "range.hpp"

template <class It>
struct range : pair<It, It> {
  using pair<It, It>::pair;
  It begin() const { return this->first; }
  It end() const { return this->second; }
  It cbegin() const { return begin(); }
  It cend() const { return end(); }
  int size() const { return this->second - this->first; }
};
#line 4 "graph/csr.hpp"

template <size_t>
struct stdin_reader;

template <class Weight = void>
class csr_graph {
 private:
  struct directed_t {};

 public:
  using weight_type = Weight;

  csr_graph() = default;
  template <class It>
  csr_graph(int n, It e, It e_last) : n(n), m(distance(e, e_last)) {
    init<false>(e, e_last);
  }
  template <size_t Size = 1 << 26>
  csr_graph(int n, int m, stdin_reader<Size>& read) : n(n), m(m) {
    auto e = read_e(read);
    init<false>(all(e));
  }

  template <class It>
  static csr_graph directed(int n, It e, It e_last) {
    return csr_graph(directed_t{}, n, e, e_last);
  }
  template <size_t Size = 1 << 26>
  static csr_graph directed(int n, int m, stdin_reader<Size>& read) {
    return csr_graph(directed_t{}, n, m, read);
  }

  template <size_t Size = 1 << 26>
  static csr_graph tree(int n, stdin_reader<Size>& read) {
    return csr_graph(n, n - 1, read);
  }
  template <size_t Size = 1 << 26>
  static csr_graph tree(stdin_reader<Size>& read) {
    int n = read;
    return csr_graph(n, n - 1, read);
  }

  int size() const { return n; }
  range<typename vector<edge_t<csr_graph>>::iterator> operator[](int v) const {
    return {ls[v], rs[v]};
  }
  int deg(int v) { return rs[v] - ls[v]; }
  template <class F>
  void adj(int v, F f) const {
    for_each(ls[v], rs[v], f);
  }

 private:
  template <class It>
  csr_graph(directed_t, int n, It e, It e_last) : n(n), m(distance(e, e_last)) {
    init<true>(e, e_last);
  }
  template <size_t Size = 1 << 26>
  csr_graph(directed_t, int n, int m, stdin_reader<Size>& read) : n(n), m(m) {
    auto e = read_e(read);
    init<true>(all(e));
  }

  vector<typename vector<edge_t<csr_graph>>::iterator> ls, rs;
  int n, m;
  vector<edge_t<csr_graph>> es;
  template <bool OneBased = true, size_t Size = 1 << 26>
  auto read_e(stdin_reader<Size>& read) {
    using E = conditional_t<is_weighted_v<csr_graph>, tuple<int, int, Weight>,
                            pair<int, int>>;
    vector<E> res(m);
    for (auto& e : res) {
      read(e);
      if (OneBased) get<0>(e)--, get<1>(e)--;
    }
    return res;
  }
  template <bool Directed, class It>
  void init(It e, It e_last) {
    if (!Directed) m *= 2;
    es.resize(m);
    ls.resize(n), rs.resize(n);
    vector<int> sz(n);
    for (auto it = e; it != e_last; it++) {
      int from = get<0>(*it), to = get<1>(*it);
      sz[from]++;
      if (!Directed) sz[to]++;
    }
    partial_sum(all(sz), sz.begin());
    rep(v, n) ls[v] = rs[v] = es.begin() + sz[v];
    for (auto it = e; it != e_last; it++) {
      int from = get<0>(*it), to = get<1>(*it);
      if constexpr (is_weighted_v<csr_graph>)
        *--ls[from] = edge_t<csr_graph>{to, get<2>(*it)};
      else
        *--ls[from] = edge_t<csr_graph>{to};
      if (!Directed) {
        if constexpr (is_weighted_v<csr_graph>)
          *--ls[to] = edge_t<csr_graph>{from, get<2>(*it)};
        else
          *--ls[to] = edge_t<csr_graph>{from};
      }
    }
  }
};
#line 3 "graph/tree/hld.hpp"

class hld {
 public:
  template <class G>
  hld(const G& graph, int r = 0) {
    graph_trait<G> g(graph);
    in.resize(g.size());
    out.resize(g.size());
    par.resize(g.size());
    heavy.assign(g.size(), -1);
    head.resize(g.size());
    par[r] = -1;
    dfs(g, r);
    int t = 0;
    decompose(g, t, r);
  }

  int idx(int v) const { return in[v]; }
  int lca(int u, int v) const {
    while (head[u] != head[v]) {
      if (in[u] > in[v]) swap(u, v);
      v = par[head[v]];
    }
    return in[u] < in[v] ? u : v;
  }

  // Call f(l[0], r[0]), f(l[1], r[1]), ...
  // s.t. union of [l[i], r[i]) == {idx(v) for v in u-v-path}.
  template <class F>
  void paths(int u, int v, F&& f, bool exclude_lca = true) const {
    while (head[u] != head[v]) {
      if (in[u] > in[v]) swap(u, v);
      call_f(f, idx(head[v]), idx(v) + 1);
      v = par[head[v]];
    }
    if (in[u] > in[v]) swap(u, v);
    call_f(f, idx(u) + exclude_lca, idx(v) + 1);
  }

  template <class F>
  void subtree(int v, F&& f, bool exclude_root = true) const {
    call_f(f, in[v] + exclude_root, out[v]);
  }

 private:
  vector<int> in, out, par, heavy, head;

  template <class G>
  int dfs(graph_trait<G> g, int v) {
    int total_size = 1, max_size = 0;
    g.adj(v, [&](int u) {
      if (u != par[v]) {
        par[u] = v;
        int sz = dfs(g, u);
        total_size += sz;
        if (sz > max_size) max_size = sz, heavy[v] = u;
      }
    });
    return total_size;
  }

  template <class G>
  void decompose(graph_trait<G> g, int& t, int v) {
    in[v] = t++;
    if (heavy[v] != -1) head[heavy[v]] = head[v], decompose(g, t, heavy[v]);
    g.adj(v, [&](int u) {
      if (u != par[v] && u != heavy[v]) head[u] = u, decompose(g, t, u);
    });
    out[v] = t;
  }

  template <class F>
  void call_f(F& f, int l, int r) const {
    if (r - l) f(l, r);
  }
};
#line 3 "algebra.hpp"

#define CONST(val) [=] { return val; }
#define WRAP_FN(func) \
  [](auto&&... args) { return func(forward<decltype(args)>(args)...); }

template <class Unit, class Op>
struct monoid : private Unit, private Op {
  using type = decltype(declval<Unit>()());
  monoid(Unit unit, Op op) : Unit(unit), Op(op) {}
  type unit() const { return Unit::operator()(); }
  type op(type a, type b) const { return Op::operator()(a, b); }
};

template <class Unit, class Op, class Inv>
struct group : monoid<Unit, Op>, private Inv {
  using type = typename monoid<Unit, Op>::type;
  group(Unit unit, Op op, Inv inv) : monoid<Unit, Op>(unit, op), Inv(inv) {}
  type inv(type a) const { return Inv::operator()(a); }
};

template <class T>
struct addition {
  using type = T;
  type unit() const { return 0; }
  type op(type a, type b) const { return a + b; }
  type inv(type a) const { return -a; }
};

template <class T>
struct maximum {
  using type = T;
  type unit() const { return numeric_limits<T>::min(); }
  type op(type a, type b) const { return a > b ? a : b; }
};

template <class T>
struct minimum {
  using type = T;
  type unit() const { return numeric_limits<T>::max(); }
  type op(type a, type b) const { return a > b ? b : a; }
};

template <class T, T nul = -1>
struct assign {
  using type = T;
  type unit() const { return nul; }
  type op(type a, type b) const { return b == nul ? a : b; }
};
#line 3 "bit/clz.hpp"

#pragma GCC target("lzcnt")

template <class T>
int clz(T x) {
    if (!x) return sizeof(T) * 8;
    if constexpr (sizeof(T) <= sizeof(unsigned)) {
        return __builtin_clz((unsigned)x);
    } else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
        return __builtin_clzll((unsigned long long)x);
    } else if constexpr (sizeof(T) <= sizeof(unsigned long long) * 2) {
        int l = clz((unsigned long long)(x >> sizeof(unsigned long long) * 8));
        return l != sizeof(unsigned long long) * 8 ? l : l + clz((unsigned long long)x);
    }
}
#line 4 "bit/ilog2.hpp"

template <class T>
__attribute__((pure)) int ilog2(T x) { assert(x != 0); return sizeof(T) * 8 - 1 - clz(x); }

template <class T>
__attribute__((pure)) int ilog2_ceil(T x) { return x == 0 || x == 1 ? 0 : ilog2(x - 1) + 1; }

template <class T, enable_if_t<is_signed_v<T>>* = nullptr>
__attribute__((pure)) T bit_floor(T x) { return T(1) << ilog2(x); }

template <class T, enable_if_t<is_signed_v<T>>* = nullptr>
__attribute__((pure)) T bit_ceil(T x) { return T(1) << ilog2_ceil(x); }
#line 4 "ds/fenwick.hpp"

template <class M>
class fenwick_tree {
 public:
  using value_type = typename M::type;
  fenwick_tree() = default;
  fenwick_tree(vector<value_type> v, M m = M()) : m(m), data(move(v)) {
    data.insert(data.cbegin(), m.unit());
    for (int i = 1; i < data.size(); i++) {
      if (i + lsb(i) < data.size())
        data[i + lsb(i)] = m.op(data[i + lsb(i)], data[i]);
    }
  }
  template <class Iter>
  fenwick_tree(Iter f, Iter l, M m = M())
      : fenwick_tree(vector<value_type>(f, l), m) {}
  fenwick_tree(int n, M m = M()) : m(m), data(n + 1, m.unit()) {}
  int size() const { return data.size() - 1; }
  void clear() { fill(data.begin(), data.end(), m.unit()); }
  void add(int i, value_type v) {
    for (i++; i < data.size(); i += lsb(i)) data[i] = m.op(data[i], v);
  }
  void sub(int i, value_type v) { add(i, m.inv(v)); }
  void assign(int i, value_type v) { add(i, m.op(v, m.inv(sum(i, i + 1)))); }
  value_type sum(int r) const {
    value_type res = m.unit();
    for (; r; r -= lsb(r)) res = m.op(res, data[r]);
    return res;
  }
  value_type sum(int l, int r) const { return m.op(m.inv(sum(l)), sum(r)); }
  template <class F>
  int partition_point(F pred = F()) const {
    int i = 0;
    value_type s = m.unit();
    if (!pred(s)) return i;
    for (int w = bit_floor(data.size()); w; w >>= 1) {
      if (i + w < data.size()) {
        value_type s2 = m.op(s, data[i + w]);
        if (pred(s2)) i += w, s = s2;
      }
    }
    return i + 1;
  }
  // min i s.t. sum(i) >= x
  template <class Comp = less<value_type>>
  int lower_bound(value_type x, Comp comp = Comp()) const {
    return partition_point([&](value_type s) { return comp(s, x); });
  }

 private:
  M m;
  vector<value_type> data;
  static int lsb(int a) { return a & -a; }
};
#line 3 "ds/range_fenwick.hpp"

template <class M, class F = multiplies<>>
class range_fenwick_tree {
 public:
  using value_type = typename M::type;
  range_fenwick_tree(vector<value_type> data, M m = M(), F mul = F())
      : m(m), mul(mul), tri(data.size(), m), rect(move(data), m) {}
  range_fenwick_tree(int n = 0, M m = M(), F mul = F())
      : m(m), mul(mul), rect(n, m), tri(n, m) {}
  int size() const { return rect.size(); }
  void clear() { rect.clear(), tri.clear(); }
  void add(int i, value_type v) { rect.add(i, v); }
  void add(int l, int r, value_type v) {
    tri.add(l, v), tri.sub(r, v);
    rect.sub(l, mul(v, l)), rect.add(r, mul(v, r));
  }
  void sub(int i, value_type v) { add(i, m.inv(v)); }
  void sub(int l, int r, value_type v) { add(l, r, m.inv(v)); }
  value_type sum(int r) const { return m.op(rect.sum(r), mul(tri.sum(r), r)); }
  value_type sum(int l, int r) const { return m.op(m.inv(sum(l)), sum(r)); }
  // template <class F>
  // int partition_point(F pred = F()) const {
  //   int i = 0;
  //   value_type s = m.unit();
  //   if (!pred(s)) return i;
  //   for (int w = bit_floor(rect.data.size()); w; w >>= 1) {
  //     if (i + w < rect.data.size()) {
  //       value_type s2 =
  //           m.op(m.op(s, rect.data[i + w]), mul(tri.data[i + w], i + w + 1));
  //       if (pred(s2)) i += w, s = s2;
  //     }
  //   }
  //   return i + 1;
  // }
  // // min i s.t. !comp(sum(i), x)
  // template <class Comp = less<value_type>>
  // int lower_bound(value_type x, Comp comp = Comp()) const {
  //   return partition_point([&](value_type s) { return comp(s, x); });
  // }

 private:
  M m;
  F mul;
  fenwick_tree<M> rect, tri;
};
#line 5 "test/graph/tree.hld.test.cpp"

int n, q;
vector<int> G[150000];
int c, u, v;

int main() {
  scanf("%d%d", &n, &q);
  rep(_, n - 1) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
  }
  hld hld(G, 0);
  range_fenwick_tree fwk(n, addition<ll>{}, multiplies<ll>{});
  while (q--) {
    scanf("%d%d%d", &c, &u, &v);
    if (c == 0) {
      ll ans = 0;
      hld.paths(u, v, [&](int l, int r) { ans += fwk.sum(l, r); });
      printf("%lld\n", ans);
    } else {
      hld.subtree(u, [&](int l, int r) { fwk.add(l, r, v); });
    }
  }
}

Test cases

Env Name Status Elapsed Memory
g++-12 00_sample_00.in :heavy_check_mark: AC 17 ms 12 MB
g++-12 00_sample_01.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_00.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_01.in :heavy_check_mark: AC 15 ms 12 MB
g++-12 10_small_02.in :heavy_check_mark: AC 15 ms 12 MB
g++-12 10_small_03.in :heavy_check_mark: AC 15 ms 12 MB
g++-12 10_small_04.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_05.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_06.in :heavy_check_mark: AC 17 ms 12 MB
g++-12 10_small_07.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_08.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_09.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_10.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_11.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_12.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_13.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_14.in :heavy_check_mark: AC 15 ms 12 MB
g++-12 10_small_15.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_16.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_17.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_18.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_19.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_20.in :heavy_check_mark: AC 15 ms 12 MB
g++-12 10_small_21.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_22.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_23.in :heavy_check_mark: AC 16 ms 12 MB
g++-12 10_small_24.in :heavy_check_mark: AC 15 ms 12 MB
g++-12 11_manyadd_00.in :heavy_check_mark: AC 320 ms 162 MB
g++-12 11_manyadd_01.in :heavy_check_mark: AC 314 ms 161 MB
g++-12 11_manyadd_02.in :heavy_check_mark: AC 312 ms 162 MB
g++-12 11_manyadd_03.in :heavy_check_mark: AC 312 ms 162 MB
g++-12 11_manyadd_04.in :heavy_check_mark: AC 160 ms 24 MB
g++-12 11_manyadd_05.in :heavy_check_mark: AC 160 ms 24 MB
g++-12 11_manyadd_06.in :heavy_check_mark: AC 159 ms 24 MB
g++-12 11_manyadd_07.in :heavy_check_mark: AC 166 ms 24 MB
g++-12 12_manydist_00.in :heavy_check_mark: AC 357 ms 162 MB
g++-12 12_manydist_01.in :heavy_check_mark: AC 355 ms 162 MB
g++-12 12_manydist_02.in :heavy_check_mark: AC 355 ms 162 MB
g++-12 12_manydist_03.in :heavy_check_mark: AC 351 ms 162 MB
g++-12 12_manydist_04.in :heavy_check_mark: AC 419 ms 24 MB
g++-12 12_manydist_05.in :heavy_check_mark: AC 414 ms 24 MB
g++-12 12_manydist_06.in :heavy_check_mark: AC 425 ms 24 MB
g++-12 12_manydist_07.in :heavy_check_mark: AC 419 ms 24 MB
g++-12 13_fromroot_00.in :heavy_check_mark: AC 286 ms 24 MB
g++-12 13_fromroot_01.in :heavy_check_mark: AC 294 ms 24 MB
g++-12 13_fromroot_02.in :heavy_check_mark: AC 316 ms 24 MB
g++-12 13_fromroot_03.in :heavy_check_mark: AC 323 ms 24 MB
g++-12 14_maximum_00.in :heavy_check_mark: AC 296 ms 24 MB
g++-12 14_maximum_01.in :heavy_check_mark: AC 296 ms 24 MB
g++-12 14_maximum_02.in :heavy_check_mark: AC 301 ms 24 MB
g++-12 14_maximum_03.in :heavy_check_mark: AC 299 ms 24 MB
g++-12 15_dfskiller_00.in :heavy_check_mark: AC 431 ms 299 MB
g++-12 15_dfskiller_01.in :heavy_check_mark: AC 433 ms 299 MB
g++-12 16_manyleaves_00.in :heavy_check_mark: AC 141 ms 17 MB
g++-12 16_manyleaves_01.in :heavy_check_mark: AC 143 ms 17 MB
g++-12 17_leafroot_00.in :heavy_check_mark: AC 259 ms 24 MB
g++-12 17_leafroot_01.in :heavy_check_mark: AC 259 ms 24 MB
Back to top page