- Choose the aggregate stored for each interval or prefix.
- Build or initialize the structure from the input.
- Apply updates and combine the affected nodes to answer each query.
Code notes
- 57 lines of C++ from the credited upstream file yosupo-range-set-range-composite.test.cpp.
- The implementation visibly relies on sequence storage.
- 1 loop block 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.
Use this to learn the idea, then write your own version.
1#define PROBLEM "https://judge.yosupo.jp/problem/range_set_range_composite"23#include "../../template/template.hpp"45#include "../../math/affine-transformation.hpp"6#include "../../segment-tree/lazy-segment-tree.hpp"78#include "../../modint/montgomery-modint.hpp"910using namespace Nyaan;11using mint = LazyMontgomeryModInt<998244353>;1213using vm = vector<mint>;14using vvm = vector<vm>;15 16using namespace Nyaan;17 18void q() {19 ini(N, Q);20 21 using A = Affine<mint>;22 using D = pair<A, int>;23 24 D ti = make_pair(A{1, 0}, 0);25 A ei{0, 0};26 auto f = [](const D& a, const D& b) -> D {27 return make_pair(b.fi(a.fi), a.se + b.se);28 };29 auto g = [](const D& a, const A& b) -> D {30 return make_pair(Power(b, a.se, A{1, 0}), a.se);31 };32 auto h = [](const A&, const A& b) { return b; };33 34 V<D> v(N, make_pair(ei, 1));35 rep(i, N) in(v[i].fi.a, v[i].fi.b);36 37 LazySegmentTree seg(v, f, g, h, ti, ei);38 39 rep(i, Q) {40 ini(cmd);41 if (cmd == 0) {42 ini(l, r, c, d);43 seg.update(l, r, A{c, d});44 } else {45 ini(l, r, x);46 auto a = seg.query(l, r);47 out(a.fi(x));48 }49 }50}51 52void Nyaan::solve() {53 int t = 1;54 55 while (t--) q();56}57