CP-Algorithms Library

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

View the Project on GitHub cp-algorithms/cp-algorithms-aux

:heavy_check_mark: Range Affine Range Sum (verify/data_structures/segtree/range_affine_range_sum.test.cpp)

Depends on

Code

// @brief Range Affine Range Sum
#define PROBLEM "https://judge.yosupo.jp/problem/range_affine_range_sum"
#include "cp-algo/data_structures/segtree/metas/affine.hpp"
#include "cp-algo/data_structures/segtree.hpp"
#include "cp-algo/math/modint.hpp"
#include <bits/stdc++.h>

using namespace std;
using namespace cp_algo::data_structures;

using base = cp_algo::math::modint<998244353>;
using meta = segtree::metas::affine_meta<base>;

void solve() {
    int n, q;
    cin >> n >> q;
    vector<meta> a(n);
    for(int i = 0; i < n; i++) {
        int ai;
        cin >> ai;
        a[i] = {ai};
    }
    segtree_t<meta> me(a);
    while(q--) {
        int t;
        cin >> t;
        if(t == 0) {
            int l, r, b, c;
            cin >> l >> r >> b >> c;
            me.exec_on_segment(l, r, [&](auto& meta) {
                meta.to_push.prepend(meta::lin(b, c));
            });
        } else {
            int l, r;
            cin >> l >> r;
            base ans = 0;
            me.exec_on_segment(l, r, [&](auto meta) {
                ans += meta.sum;
            });
            cout << ans << "\n";
        }
    }
}

signed main() {
    //freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    while(t--) {
        solve();
    }
}
#line 1 "verify/data_structures/segtree/range_affine_range_sum.test.cpp"
// @brief Range Affine Range Sum
#define PROBLEM "https://judge.yosupo.jp/problem/range_affine_range_sum"
#line 1 "cp-algo/data_structures/segtree/metas/affine.hpp"


#line 1 "cp-algo/data_structures/segtree/metas/base.hpp"


namespace cp_algo::data_structures::segtree::metas {
    template<typename derived_meta>
    struct base_meta {
        using meta = derived_meta;
        virtual void pull(meta const&, meta const&, int, int) {};
        virtual void push(meta*, meta*, int, int) {};
    };
}

#line 1 "cp-algo/math/affine.hpp"


#include <optional>
#include <utility>
#include <cassert>
#include <tuple>
namespace cp_algo::math {
    // a * x + b
    template<typename base>
    struct lin {
        base a = 1, b = 0;
        std::optional<base> c;
        lin() {}
        lin(base b): a(0), b(b) {}
        lin(base a, base b): a(a), b(b) {}
        lin(base a, base b, base _c): a(a), b(b), c(_c) {}

        // polynomial product modulo x^2 - c
        lin operator * (const lin& t) {
            assert(c && t.c && *c == *t.c);
            return {a * t.b + b * t.a, b * t.b + a * t.a * (*c), *c};
        }

        // a * (t.a * x + t.b) + b
        lin apply(lin const& t) const {
            return {a * t.a, a * t.b + b};
        }

        void prepend(lin const& t) {
            *this = t.apply(*this);
        }

        base eval(base x) const {
            return a * x + b;
        }
    };

    // (ax+b) / (cx+d)
    template<typename base>
    struct linfrac {
        base a, b, c, d;
        linfrac(): a(1), b(0), c(0), d(1) {} // x, identity for composition
        linfrac(base a): a(a), b(1), c(1), d(0) {} // a + 1/x, for continued fractions
        linfrac(base a, base b, base c, base d): a(a), b(b), c(c), d(d) {}

        // composition of two linfracs
        linfrac operator * (linfrac t) const {
            return t.prepend(linfrac(*this));
        }

        linfrac operator-() const {
            return {-a, -b, -c, -d};
        }

        linfrac adj() const {
            return {d, -b, -c, a};
        }
        
        linfrac& prepend(linfrac const& t) {
            t.apply(a, c);
            t.apply(b, d);
            return *this;
        }

        // apply linfrac to A/B
        void apply(base &A, base &B) const {
            std::tie(A, B) = std::pair{a * A + b * B, c * A + d * B};
        }
    };
}

#line 5 "cp-algo/data_structures/segtree/metas/affine.hpp"
namespace cp_algo::data_structures::segtree::metas {
    template<typename base>
    struct affine_meta: base_meta<affine_meta<base>> {
        using meta = affine_meta;
        using lin = math::lin<base>;

        base sum = 0;
        lin to_push = {};

        affine_meta() {}
        affine_meta(base sum): sum(sum) {}

        void push(meta *L, meta *R, int l, int r) override {
            if(to_push.a != 1 || to_push.b != 0) {
                sum = to_push.a * sum + to_push.b * (r - l);
                if(r - l > 1) {
                    L->to_push.prepend(to_push);
                    R->to_push.prepend(to_push);
                }
                to_push = {};
            }
        }

        void pull(meta const& L, meta const& R, int, int) override {
            sum = L.sum + R.sum;
        }
    };
}

#line 1 "cp-algo/data_structures/segtree.hpp"


#include <vector>
namespace cp_algo::data_structures {
    template<typename meta>
    struct segtree_t {
        const int N;
        std::vector<meta> _meta;

        segtree_t(int n): N(n), _meta(4 * N) {}

        segtree_t(std::vector<meta> leafs): N(size(leafs)), _meta(4 * N) {
            build(leafs);
        }

        void pull(int v, int l, int r) {
            if(r - l > 1) {
                _meta[v].pull(_meta[2 * v], _meta[2 * v + 1], l, r);
            }
        }

        void push(int v, int l, int r) {
            if(r - l > 1) {
                _meta[v].push(&_meta[2 * v], &_meta[2 * v + 1], l, r);
            } else {
                _meta[v].push(nullptr, nullptr, l, r);
            }
        }

        void build(auto &a, int v, size_t l, size_t r) {
            if(r - l == 1) {
                if(l < size(a)) {
                    _meta[v] = a[l];
                }
            } else {
                size_t m = (l + r) / 2;
                build(a, 2 * v, l, m);
                build(a, 2 * v + 1, m, r);
                pull(v, l, r);
            }
        }

        void build(auto &a) {
            build(a, 1, 0, N);
        }

        void exec_on_segment(int a, int b, auto func, auto proceed, auto stop, int v, int l, int r) {
            push(v, l, r);
            if(r <= a || b <= l || stop(_meta[v])) {
                return;
            } else if(a <= l && r <= b && proceed(_meta[v])) {
                func(_meta[v]);
                push(v, l, r);
            } else {
                int m = (l + r) / 2;
                exec_on_segment(a, b, func, proceed, stop, 2 * v, l, m);
                exec_on_segment(a, b, func, proceed, stop, 2 * v + 1, m, r);
                pull(v, l, r);
            }
        }

        static constexpr auto default_true = [](auto const&){return true;};
        static constexpr auto default_false = [](auto const&){return false;};

        void exec_on_segment(int a, int b, auto func, auto proceed, auto stop) {
            exec_on_segment(a, b, func, proceed, stop, 1, 0, N);
        }

        void exec_on_segment(int a, int b, auto func) {
            exec_on_segment(a, b, func, default_true, default_false);
        }
    };
}

#line 1 "cp-algo/math/modint.hpp"


#line 1 "cp-algo/math/common.hpp"


#include <functional>
#include <cstdint>
namespace cp_algo::math {
#ifdef CP_ALGO_MAXN
    const int maxn = CP_ALGO_MAXN;
#else
    const int maxn = 1 << 19;
#endif
    const int magic = 64; // threshold for sizes to run the naive algo

    auto bpow(auto const& x, int64_t n, auto const& one, auto op) {
        if(n == 0) {
            return one;
        } else {
            auto t = bpow(x, n / 2, one, op);
            t = op(t, t);
            if(n % 2) {
                t = op(t, x);
            }
            return t;
        }
    }
    auto bpow(auto x, int64_t n, auto ans) {
        return bpow(x, n, ans, std::multiplies{});
    }
    template<typename T>
    T bpow(T const& x, int64_t n) {
        return bpow(x, n, T(1));
    }
}

#line 4 "cp-algo/math/modint.hpp"
#include <iostream>
namespace cp_algo::math {
    template<typename modint>
    struct modint_base {
        static int64_t mod() {
            return modint::mod();
        }
        modint_base(): r(0) {}
        modint_base(int64_t rr): r(rr % mod()) {
            r = std::min(r, r + mod());
        }
        modint inv() const {
            return bpow(to_modint(), mod() - 2);
        }
        modint operator - () const {return std::min(-r, mod() - r);}
        modint& operator /= (const modint &t) {
            return to_modint() *= t.inv();
        }
        modint& operator *= (const modint &t) {
            if(mod() <= uint32_t(-1)) {
                r = r * t.r % mod();
            } else {
                r = __int128(r) * t.r % mod();
            }
            return to_modint();
        }
        modint& operator += (const modint &t) {
            r += t.r; r = std::min(r, r - mod());
            return to_modint();
        }
        modint& operator -= (const modint &t) {
            r -= t.r; r = std::min(r, r + mod());
            return to_modint();
        }
        modint operator + (const modint &t) const {return modint(to_modint()) += t;}
        modint operator - (const modint &t) const {return modint(to_modint()) -= t;}
        modint operator * (const modint &t) const {return modint(to_modint()) *= t;}
        modint operator / (const modint &t) const {return modint(to_modint()) /= t;}
        auto operator <=> (const modint_base &t) const = default;
        int64_t rem() const {return 2 * r > (uint64_t)mod() ? r - mod() : r;}

        // Only use if you really know what you're doing!
        uint64_t modmod() const {return 8ULL * mod() * mod();};
        void add_unsafe(uint64_t t) {r += t;}
        void pseudonormalize() {r = std::min(r, r - modmod());}
        modint const& normalize() {
            if(r >= (uint64_t)mod()) {
                r %= mod();
            }
            return to_modint();
        }
        uint64_t& setr() {return r;}
        uint64_t getr() const {return r;}
    private:
        uint64_t r;
        modint& to_modint() {return static_cast<modint&>(*this);}
        modint const& to_modint() const {return static_cast<modint const&>(*this);}
    };
    template<typename modint>
    std::istream& operator >> (std::istream &in, modint_base<modint> &x) {
        return in >> x.setr();
    }
    template<typename modint>
    std::ostream& operator << (std::ostream &out, modint_base<modint> const& x) {
        return out << x.getr();
    }

    template<typename modint>
    concept modint_type = std::is_base_of_v<modint_base<modint>, modint>;

    template<int64_t m>
    struct modint: modint_base<modint<m>> {
        static constexpr int64_t mod() {return m;}
        using Base = modint_base<modint<m>>;
        using Base::Base;
    };

    struct dynamic_modint: modint_base<dynamic_modint> {
        static int64_t mod() {return m;}
        static void switch_mod(int64_t nm) {m = nm;}
        using Base = modint_base<dynamic_modint>;
        using Base::Base;

        // Wrapper for temp switching
        auto static with_mod(int64_t tmp, auto callback) {
            struct scoped {
                int64_t prev = mod();
                ~scoped() {switch_mod(prev);}
            } _;
            switch_mod(tmp);
            return callback();
        }
    private:
        static int64_t m;
    };
    int64_t dynamic_modint::m = 0;
}

#line 6 "verify/data_structures/segtree/range_affine_range_sum.test.cpp"
#include <bits/stdc++.h>

using namespace std;
using namespace cp_algo::data_structures;

using base = cp_algo::math::modint<998244353>;
using meta = segtree::metas::affine_meta<base>;

void solve() {
    int n, q;
    cin >> n >> q;
    vector<meta> a(n);
    for(int i = 0; i < n; i++) {
        int ai;
        cin >> ai;
        a[i] = {ai};
    }
    segtree_t<meta> me(a);
    while(q--) {
        int t;
        cin >> t;
        if(t == 0) {
            int l, r, b, c;
            cin >> l >> r >> b >> c;
            me.exec_on_segment(l, r, [&](auto& meta) {
                meta.to_push.prepend(meta::lin(b, c));
            });
        } else {
            int l, r;
            cin >> l >> r;
            base ans = 0;
            me.exec_on_segment(l, r, [&](auto meta) {
                ans += meta.sum;
            });
            cout << ans << "\n";
        }
    }
}

signed main() {
    //freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    while(t--) {
        solve();
    }
}

Test cases

Env Name Status Elapsed Memory
g++ example_00 :heavy_check_mark: AC 6 ms 3 MB
g++ max_random_00 :heavy_check_mark: AC 836 ms 144 MB
g++ max_random_01 :heavy_check_mark: AC 845 ms 144 MB
g++ max_random_02 :heavy_check_mark: AC 830 ms 144 MB
g++ random_00 :heavy_check_mark: AC 699 ms 113 MB
g++ random_01 :heavy_check_mark: AC 722 ms 133 MB
g++ random_02 :heavy_check_mark: AC 426 ms 18 MB
g++ small_00 :heavy_check_mark: AC 6 ms 3 MB
g++ small_01 :heavy_check_mark: AC 6 ms 3 MB
g++ small_02 :heavy_check_mark: AC 6 ms 3 MB
g++ small_03 :heavy_check_mark: AC 6 ms 3 MB
g++ small_04 :heavy_check_mark: AC 6 ms 3 MB
g++ small_05 :heavy_check_mark: AC 6 ms 3 MB
g++ small_06 :heavy_check_mark: AC 6 ms 3 MB
g++ small_07 :heavy_check_mark: AC 6 ms 3 MB
g++ small_08 :heavy_check_mark: AC 6 ms 3 MB
g++ small_09 :heavy_check_mark: AC 6 ms 3 MB
g++ small_random_00 :heavy_check_mark: AC 7 ms 4 MB
g++ small_random_01 :heavy_check_mark: AC 7 ms 4 MB
Back to top page