This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub plasmatic1/templates
#define PROBLEM "https://judge.yosupo.jp/problem/range_affine_range_sum" #include "../../template.hpp" #include "../test_utils.hpp" #include "../../ds/segment_tree_lazy.hpp" #include "../../math/mod.hpp" // A = (ax+b), B = (cx+d) // c(ax+b)+d = acx + bc + d using MI = ModInt<int, 998244353>; using pm = pair<MI, MI>; struct AffineComp { using Data = MI; using Lazy = pm; const Data vdef = 0; const Lazy ldef = {1, 0}; Data merge(Data l, Data r) const { return l + r; } Lazy mergeLazy(Lazy to, Lazy v) const { auto [a, b] = to; auto [c, d] = v; return {a * c, b * c + d}; } void applyLazy(Data &to, Lazy &v, int l, int r) { to = v.first * to + (r - l + 1) * v.second; } }; int main() { fast_io(); int N = readi(), Q = readi(); LazySegmentTree<AffineComp> seg; seg.init(N); for (auto [i, v] : enumerate(readv<int>(N), 1)) seg.setPoint(i, v); while (Q--) { if (readi() == 0) { int l = readi() + 1, r = readi(), b = readi(), c = readi(); seg.update(l, r, {b, c}); } else { int l = readi()+1, r = readi(); print(seg.query(l, r)); } } }
#line 1 "tests/ds/segment_tree_lazy.test.cpp" #define PROBLEM "https://judge.yosupo.jp/problem/range_affine_range_sum" #line 2 "template.hpp" #include <bits/stdc++.h> #define DEBUG 1 using namespace std; // Defines #define fs first #define sn second #define pb push_back #define eb emplace_back #define mpr make_pair #define mtp make_tuple #define all(x) (x).begin(), (x).end() // Basic type definitions #if __cplusplus == 201703L // CPP17 only things template <typename T> using opt_ref = optional<reference_wrapper<T>>; // for some templates #endif using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair<int, int>; using pll = pair<long long, long long>; #ifdef __GNUG__ // PBDS order statistic tree #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; template <typename T, class comp = less<T>> using os_tree = tree<T, null_type, comp, rb_tree_tag, tree_order_statistics_node_update>; template <typename K, typename V, class comp = less<K>> using treemap = tree<K, V, comp, rb_tree_tag, tree_order_statistics_node_update>; // HashSet #include <ext/pb_ds/assoc_container.hpp> template <typename T, class Hash> using hashset = gp_hash_table<T, null_type, Hash>; template <typename K, typename V, class Hash> using hashmap = gp_hash_table<K, V, Hash>; const ll RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count(); struct chash { ll operator()(ll x) const { return x ^ RANDOM; } }; #endif // More utilities int SZ(string &v) { return v.length(); } template <typename C> int SZ(C &v) { return v.size(); } template <typename C> void UNIQUE(vector<C> &v) { sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); } template <typename T, typename U> void maxa(T &a, U b) { a = max(a, b); } template <typename T, typename U> void mina(T &a, U b) { a = min(a, b); } const ll INF = 0x3f3f3f3f, LLINF = 0x3f3f3f3f3f3f3f3f; #line 3 "tests/test_utils.hpp" // I/O template <typename T> void print(T v) { cout << v << '\n'; } template <typename T, typename... Rest> void print(T v, Rest... vs) { cout << v << ' '; print(vs...); } void fast_io() { ios_base::sync_with_stdio(false); cin.tie(NULL); } // Reading operators template <typename T, typename U> istream& operator>>(istream& in, pair<T, U> &o) { return in >> o.first >> o.second; } // Read helpers int readi() { int x; cin >> x; return x; } ll readl() { ll x; cin >> x; return x; } template <typename T> vector<T> readv(int n) { vector<T> res(n); for (auto &x : res) cin >> x; return res; } // Functional stuff template <typename T> vector<pair<int, T>> enumerate(vector<T> v, int start = 0) { vector<pair<int, T>> res; for (auto &x : v) res.emplace_back(start++, x); return res; } #line 3 "ds/segment_tree_lazy.hpp" // Example comparator: Range min + Range increment // In the functions mergeLazy and applyLazy, objects are merged from `v` to `to`. In the function merge, data is merged from left to right struct Comp { using Data = int; using Lazy = int; const Data vdef = INF; const Lazy ldef = 0; Data merge(Data l, Data r) const { return min(l, r); } Lazy mergeLazy(Lazy to, Lazy v) const { return to + v; } void applyLazy(Data &to, Lazy &v, int l, int r) { to += v; } }; #define MID int mid = (l + r) / 2, lhs = i + 1, rhs = i + (mid - l + 1) * 2; template <class Comp> struct LazySegmentTree { using Data = typename Comp::Data; using Lazy = typename Comp::Lazy; Comp C; int N; vector<Data> seg; vector<Lazy> lazy; void init(int n0) { N = n0; seg.assign(2 * N + 2, C.vdef); lazy.assign(2 * N + 2, C.ldef); } void push(int i, int l, int r) { if (lazy[i] != C.ldef) { MID; C.applyLazy(seg[i], lazy[i], l, r); if (l != r) { lazy[lhs] = C.mergeLazy(lazy[lhs], lazy[i]); lazy[rhs] = C.mergeLazy(lazy[rhs], lazy[i]); } lazy[i] = C.ldef; } } Data _query(int ql, int qr, int i, int l, int r) { if (ql > r || qr < l) return C.vdef; push(i, l, r); if (l >= ql && r <= qr) return seg[i]; MID; return C.merge(_query(ql, qr, lhs, l, mid), _query(ql, qr, rhs, mid + 1, r)); } Data _update(int ql, int qr, Lazy v, int i, int l, int r) { push(i, l, r); if (ql > r || qr < l) return seg[i]; if (l >= ql && r <= qr) { lazy[i] = v; push(i, l, r); return seg[i]; } MID; return seg[i] = C.merge(_update(ql, qr, v, lhs, l, mid), _update(ql, qr, v, rhs, mid + 1, r)); } Data _setPoint(int q, Data v, int i, int l, int r) { push(i, l, r); if (q > r || q < l) return seg[i]; if (l >= q && r <= q) return seg[i] = v; MID; return seg[i] = C.merge(_setPoint(q, v, lhs, l, mid), _setPoint(q, v, rhs, mid + 1, r)); } Data query(int ql, int qr) { return _query(ql, qr, 1, 1, N); } void update(int ql, int qr, Lazy v) { _update(ql, qr, v, 1, 1, N); } void setPoint(int q, Data v) { _setPoint(q, v, 1, 1, N); } }; #line 3 "math/eea.hpp" /* * ax + by = gcd(a, b) * * we know * bx' + (a%b)y' = gcd(a, b) * * bx' + (a-b*(a//b))y' = gcd(a, b) * bx' + ay' - b*(a//b)y' = gcd(a, b) * ay' + b(x' - (a//b)y') = gcd(a, b) */ template <typename T> T extgcd(T a, T b, T &x, T &y) { if (b == 0) { x = 1; y = 0; return a; } T x0, y0, res = extgcd(b, a%b, x0, y0); x = y0; y = x0 - (a / b) * y0; return res; } #line 4 "math/mod.hpp" // based on Tourist modInt orz template <typename MD> struct _ModInt { using T = typename decay<decltype(MD::value)>::type; static_assert(sizeof(T) >= 4, "size of T must be at least 32 bits"); static_assert(sizeof(T) <= 8, "size of T must be at most 64 bits"); static_assert(is_integral<T>::value, "T must be an integral type"); #ifdef __SIZEOF_INT128__ using mul_t = typename conditional<sizeof(T) <= 4, int64_t, __int128>::type; #else using mul_t = int64_t; static_assert(sizeof(T) <= 4, "int128 not available, cannot use 64-bit size of T"); #endif constexpr static T mod() { return MD::value; } template <typename U> static T normalize(const U& x) { T res = x; res %= mod(); if (res < 0) res += mod(); return res; } T value; constexpr _ModInt() : value() {} template <typename U> _ModInt(const U& x) { value = normalize(x); } const T& operator()() const { return value; } template <typename U> operator U() const { return static_cast<U>(value); } // FastPow template <typename U> static _ModInt pow(_ModInt x, U y) { _ModInt res(1); for (; y; y /= 2) { if (y & 1) res *= x; x *= x; } return res; } static _ModInt inv(const _ModInt &x) { T inv, _; extgcd(x.value, mod(), inv, _); return _ModInt(inv); } // Arithmetic Operators w/ _ModInt // Assignment operators here _ModInt& operator+=(const _ModInt &o) { if ((value += o.value) >= mod()) value -= mod(); return *this; } template <typename U> _ModInt& operator+=(const U &o) { return *this += _ModInt(o); } _ModInt& operator-=(const _ModInt &o) { if ((value -= o.value) < 0) value += mod(); return *this; } template <typename U> _ModInt& operator-=(const U &o) { return *this -= _ModInt(o); } _ModInt& operator++() { return *this += 1; } _ModInt operator++(int) { _ModInt res(*this); *this += 1; return res; } _ModInt& operator--() { return *this -= 1; } _ModInt operator--(int) { _ModInt res(*this); *this -= 1; return res; } _ModInt& operator*=(const _ModInt &o) { value = (mul_t)value * o.value % mod(); if (value < 0) value += mod(); return *this; } // make sure cast to mul_t!!! template <typename U> _ModInt& operator*=(const U &o) { return *this *= _ModInt(o); } _ModInt& operator/=(const _ModInt &o) { return *this *= inv(o.value); } template <typename U> _ModInt& operator/=(const U &o) { return *this /= _ModInt(o); } _ModInt operator-() const { return _ModInt(value); } // Other Operators T& operator()() { return value; } // Definitions of some operators }; // Binary operators #define OP_CMP(op) template <typename T> bool operator op(const _ModInt<T> &lhs, const _ModInt<T> &rhs) { return lhs.value op rhs.value; } \ template <typename T, typename U> bool operator op(const _ModInt<T> &lhs, U rhs) { return lhs op _ModInt<T>(rhs); } \ template <typename T, typename U> bool operator op(U lhs, const _ModInt<T> &rhs) { return _ModInt<T>(lhs) op rhs; } #define OP_ARI(op) template <typename T> _ModInt<T> operator op(const _ModInt<T> &lhs, const _ModInt<T> &rhs) { return _ModInt<T>(lhs) op##= rhs; } \ template <typename T, typename U> _ModInt<T> operator op(U lhs, const _ModInt<T> &rhs) { return _ModInt<T>(lhs) op##= rhs; } \ template <typename T, typename U> _ModInt<T> operator op(const _ModInt<T> &lhs, U rhs) { return _ModInt<T>(lhs) op##= rhs; } OP_CMP(==) OP_CMP(!=) OP_CMP(<) OP_CMP(>) OP_CMP(<=) OP_CMP(>=) OP_ARI(+) OP_ARI(-) OP_ARI(*) OP_ARI(/) #undef OP_CMP #undef OP_ARI template <typename T> istream& operator>>(istream& in, _ModInt<T> &o) { return in >> o(); } template <typename T> ostream& operator<<(ostream& out, _ModInt<T> &o) { return out << o(); } // Definitions template <typename T, T mod> using ModInt = _ModInt<integral_constant<T, mod>>; template <typename T> struct VarMod { static T value; static void read(istream& in) { in >> value; } static void set(T v0) { value = v0; } }; template <typename T> using VarModInt = _ModInt<VarMod<T>>; #line 6 "tests/ds/segment_tree_lazy.test.cpp" // A = (ax+b), B = (cx+d) // c(ax+b)+d = acx + bc + d using MI = ModInt<int, 998244353>; using pm = pair<MI, MI>; struct AffineComp { using Data = MI; using Lazy = pm; const Data vdef = 0; const Lazy ldef = {1, 0}; Data merge(Data l, Data r) const { return l + r; } Lazy mergeLazy(Lazy to, Lazy v) const { auto [a, b] = to; auto [c, d] = v; return {a * c, b * c + d}; } void applyLazy(Data &to, Lazy &v, int l, int r) { to = v.first * to + (r - l + 1) * v.second; } }; int main() { fast_io(); int N = readi(), Q = readi(); LazySegmentTree<AffineComp> seg; seg.init(N); for (auto [i, v] : enumerate(readv<int>(N), 1)) seg.setPoint(i, v); while (Q--) { if (readi() == 0) { int l = readi() + 1, r = readi(), b = readi(), c = readi(); seg.update(l, r, {b, c}); } else { int l = readi()+1, r = readi(); print(seg.query(l, r)); } } }