Problem solution · C++

Vertex Set Path Composite

Vertex Set Path Composite: a C++ solution using segment tree or range structure. Learn the idea, check the complexity, and read the full code, with credit to NyaanNyaan Competitive Programming Library.

Technique
Segment tree or range structure
Source
NyaanNyaan Competitive Programming Library
Length
59 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Segment tree or range structure

For Vertex Set Path Composite, the implementation stores interval information in a range-query data structure so updates and queries avoid rescanning the full input.

  1. Choose the aggregate stored for each interval or prefix.
  2. Build or initialize the structure from the input.
  3. Apply updates and combine the affected nodes to answer each query.

Code notes

  • 59 lines of C++ from the credited upstream file yosupo-vertex-set-path-composite.test.cpp.
  • The implementation visibly relies on sequence storage, ordered lookup.
  • No explicit loop blocks detected.

Complexity

Count the build once, then multiply the logarithmic update or query path by the number of operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from NyaanNyaan Competitive Programming Library by NyaanNyaan and is used under the CC0-1.0 licence.

Full codeVertex Set Path Composite · C++C++
Use this to learn the idea, then write your own version.
#define PROBLEM "https://judge.yosupo.jp/problem/vertex_set_path_composite" #include "../../template/template.hpp" #include "../../modint/montgomery-modint.hpp"using mint = LazyMontgomeryModInt<998244353>;using vm = vector<mint>;using vvm = vector<vm>;#include "../../modulo/binomial.hpp"Binomial<mint> C; #include "../../graph/graph-template.hpp"#include "../../math/affine-transformation.hpp"#include "../../segment-tree/segment-tree.hpp"#include "../../tree/heavy-light-decomposition.hpp"//using namespace Nyaan; void Nyaan::solve() {  ini(n, q);  vm a(n), b(n);  in2(a, b);  auto g = graph(n, n - 1, false, false);   HeavyLightDecomposition<vvi> hld(g);   using af = Affine<mint>;  auto m1 = [](af a_, af b_) { return a_ * b_; };  auto m2 = [](af a_, af b_) { return b_ * a_; };  SegmentTree<af, decltype(m1)> seg1(n, m1, af());  SegmentTree<af, decltype(m2)> seg2(n, m2, af());   rep(i, n) {    int j = hld.idx(i).first;    trc(j);    seg1.set(j, af(a[i], b[i]));    seg2.set(j, af(a[i], b[i]));  }  seg1.build(), seg2.build();   rep(_, q) {    ini(cmd, x, y, z);    if (cmd == 0) {      seg1.update(hld.idx(x).first, af(y, z));      seg2.update(hld.idx(x).first, af(y, z));    } else {      mint ans = z;      auto f = [&](int l, int r) {        if (l <= r)          ans = seg1.query(l, r)(ans);        else          ans = seg2.query(r, l)(ans);      };      hld.path_noncommutative_query(x, y, true, f);      out(ans);    }  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗