Luzhiled's Library

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

View the Project on GitHub ei1333/library

:heavy_check_mark: test/verify/yosupo-range-affine-range-sum.test.cpp

Depends on

Code

// clang-format off
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/range_affine_range_sum
// clang-format on

#include <iostream>
#include <utility>
#include <vector>

#include "../../math/combinatorics/montgomery-mod-int.hpp"
#include "../../structure/class/range-affine-range-sum.hpp"
#include "../../structure/segment-tree/lazy-segment-tree.hpp"

using namespace std;

using mint = modint998244353;

int main() {
  int N, Q;
  cin >> N >> Q;
  using monoid = RangeAffineRangeSum<mint>;
  vector<monoid::S> vs(N);
  for (int i = 0; i < N; i++) {
    mint a;
    cin >> a;
    vs[i] = {a, 1};
  }
  LazySegmentTree seg(monoid(), vs);
  for (int i = 0; i < Q; i++) {
    int t;
    cin >> t;
    if (t == 0) {
      int l, r;
      mint b, c;
      cin >> l >> r >> b >> c;
      seg.apply(l, r, {b, c});
    } else {
      int l, r;
      cin >> l >> r;
      cout << seg.prod(l, r).first << "\n";
    }
  }
}
#line 1 "test/verify/yosupo-range-affine-range-sum.test.cpp"
// clang-format off
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/range_affine_range_sum
// clang-format on

#include <iostream>
#include <utility>
#include <vector>

#line 2 "math/combinatorics/montgomery-mod-int.hpp"

#include <cstdint>
#line 5 "math/combinatorics/montgomery-mod-int.hpp"

template <std::uint32_t mod_, bool fast = false>
struct MontgomeryModInt {
 private:
  using mint = MontgomeryModInt;
  using i32 = std::int32_t;
  using i64 = std::int64_t;
  using u32 = std::uint32_t;
  using u64 = std::uint64_t;

  static constexpr u32 get_r() {
    u32 ret = mod_;
    for (i32 i = 0; i < 4; i++) ret *= 2 - mod_ * ret;
    return ret;
  }

  static constexpr u32 r = get_r();

  static constexpr u32 n2 = -u64(mod_) % mod_;

  static_assert(r * mod_ == 1, "invalid, r * mod != 1");
  static_assert(mod_ < (1 << 30), "invalid, mod >= 2 ^ 30");
  static_assert((mod_ & 1) == 1, "invalid, mod % 2 == 0");

  u32 x;

 public:
  MontgomeryModInt() : x{} {}

  MontgomeryModInt(const i64& a)
      : x(reduce(u64(fast ? a : (a % mod() + mod())) * n2)) {}

  static constexpr u32 reduce(const u64& b) {
    return u32(b >> 32) + mod() - u32((u64(u32(b) * r) * mod()) >> 32);
  }

  mint& operator+=(const mint& p) {
    if (i32(x += p.x - 2 * mod()) < 0) x += 2 * mod();
    return *this;
  }

  mint& operator-=(const mint& p) {
    if (i32(x -= p.x) < 0) x += 2 * mod();
    return *this;
  }

  mint& operator*=(const mint& p) {
    x = reduce(u64(x) * p.x);
    return *this;
  }

  mint& operator/=(const mint& p) {
    *this *= p.inv();
    return *this;
  }

  mint operator-() const { return mint() - *this; }

  mint operator+(const mint& p) const { return mint(*this) += p; }

  mint operator-(const mint& p) const { return mint(*this) -= p; }

  mint operator*(const mint& p) const { return mint(*this) *= p; }

  mint operator/(const mint& p) const { return mint(*this) /= p; }

  bool operator==(const mint& p) const {
    return (x >= mod() ? x - mod() : x) == (p.x >= mod() ? p.x - mod() : p.x);
  }

  bool operator!=(const mint& p) const {
    return (x >= mod() ? x - mod() : x) != (p.x >= mod() ? p.x - mod() : p.x);
  }

  u32 val() const {
    u32 ret = reduce(x);
    return ret >= mod() ? ret - mod() : ret;
  }

  mint pow(u64 n) const {
    mint ret(1), mul(*this);
    while (n > 0) {
      if (n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  mint inv() const { return pow(mod() - 2); }

  friend std::ostream& operator<<(std::ostream& os, const mint& p) {
    return os << p.val();
  }

  friend std::istream& operator>>(std::istream& is, mint& a) {
    i64 t;
    is >> t;
    a = mint(t);
    return is;
  }

  static constexpr u32 mod() { return mod_; }
};

template <std::uint32_t mod>
using modint = MontgomeryModInt<mod>;
using modint998244353 = modint<998244353>;
using modint1000000007 = modint<1000000007>;
#line 2 "structure/class/range-affine-range-sum.hpp"

#line 4 "structure/class/range-affine-range-sum.hpp"

#line 2 "structure/class/affine.hpp"

template <typename T>
struct Affine {
  T a, b;  // ax+b
  Affine() : a(1), b(0) {}
  Affine(T a, T b) : a(a), b(b) {}
  T eval(T x) const { return a * x + b; }
  static constexpr Affine op(const Affine& l, const Affine& r) {
    return {l.a * r.a, l.b * r.a + r.b};
  }
  constexpr bool operator==(const Affine& p) const {
    return a == p.a and b == p.b;
  }
  constexpr bool operator!=(const Affine& p) const {
    return a != p.a or b != p.b;
  }
};
#line 6 "structure/class/range-affine-range-sum.hpp"

template <typename T>
struct RangeAffineRangeSum {
  using S = std::pair<T, T>;
  using F = Affine<T>;
  static constexpr S op(const S& a, const S& b) {
    return {a.first + b.first, a.second + b.second};
  }
  static constexpr S e() { return {0, 0}; }
  static constexpr S mapping(const S& x, const F& f) {
    return {x.first * f.a + x.second * f.b, x.second};
  }
  static constexpr F composition(const F& f, const F& g) { return F::op(f, g); }
  static constexpr F id() { return F(); }
};
#line 2 "structure/segment-tree/lazy-segment-tree.hpp"

#include <cassert>
#include <optional>
#include <set>
#line 7 "structure/segment-tree/lazy-segment-tree.hpp"

#line 2 "structure/class/acted-monoid.hpp"

template <typename S2, typename Op, typename E, typename F2, typename Mapping,
          typename Composition, typename Id>
struct LambdaActedMonoid {
  using S = S2;
  using F = F2;

  S op(const S& a, const S& b) const { return _op(a, b); }

  S e() const { return _e(); }

  S mapping(const S& x, const F& f) const { return _mapping(x, f); }

  F composition(const F& f, const F& g) const { return _composition(f, g); }

  F id() const { return _id(); }

  LambdaActedMonoid(Op _op, E _e, Mapping _mapping, Composition _composition,
                    Id _id)
      : _op(_op),
        _e(_e),
        _mapping(_mapping),
        _composition(_composition),
        _id(_id) {}

 private:
  Op _op;

  E _e;

  Mapping _mapping;

  Composition _composition;

  Id _id;
};

template <typename Op, typename E, typename Mapping, typename Composition,
          typename Id>
LambdaActedMonoid(Op _op, E _e, Mapping _mapping, Composition _composition,
                  Id _id)
    -> LambdaActedMonoid<decltype(_e()), Op, E, decltype(_id()), Mapping,
                         Composition, Id>;

/*
struct ActedMonoid {
  using S = ?;
  using F = ?;
  static constexpr S op(const S& a, const S& b) {}
  static constexpr S e() {}
  static constexpr S mapping(const S &x, const F &f) {}
  static constexpr F composition(const F &f, const F &g) {}
  static constexpr F id() {}
};
*/
#line 9 "structure/segment-tree/lazy-segment-tree.hpp"

template <typename ActedMonoid>
struct LazySegmentTree {
  using S = typename ActedMonoid::S;
  using F = typename ActedMonoid::F;

 private:
  ActedMonoid m;

  int n{}, sz{}, height{};

  std::vector<S> data;

  std::vector<F> lazy;

  inline void update(int k) {
    data[k] = m.op(data[2 * k + 0], data[2 * k + 1]);
  }

  inline void all_apply(int k, const F& x) {
    data[k] = m.mapping(data[k], x);
    if (k < sz) lazy[k] = m.composition(lazy[k], x);
  }

  inline void propagate(int k) {
    if (lazy[k] != m.id()) {
      all_apply(2 * k + 0, lazy[k]);
      all_apply(2 * k + 1, lazy[k]);
      lazy[k] = m.id();
    }
  }

 public:
  LazySegmentTree() = default;

  explicit LazySegmentTree(ActedMonoid m, int n) : m(m), n(n) {
    sz = 1;
    height = 0;
    while (sz < n) sz <<= 1, height++;
    data.assign(2 * sz, m.e());
    lazy.assign(2 * sz, m.id());
  }

  explicit LazySegmentTree(ActedMonoid m, const std::vector<S>& v)
      : LazySegmentTree(m, static_cast<int>(v.size())) {
    build(v);
  }

  void build(const std::vector<S>& v) {
    assert(n == (int)v.size());
    for (int k = 0; k < n; k++) data[k + sz] = v[k];
    for (int k = sz - 1; k > 0; k--) update(k);
  }

  void set(int k, const S& x) {
    k += sz;
    for (int i = height; i > 0; i--) propagate(k >> i);
    data[k] = x;
    for (int i = 1; i <= height; i++) update(k >> i);
  }

  S get(int k) {
    k += sz;
    for (int i = height; i > 0; i--) propagate(k >> i);
    return data[k];
  }

  S operator[](int k) { return get(k); }

  S prod(int l, int r) {
    if (l >= r) return m.e();
    l += sz;
    r += sz;
    for (int i = height; i > 0; i--) {
      if (((l >> i) << i) != l) propagate(l >> i);
      if (((r >> i) << i) != r) propagate((r - 1) >> i);
    }
    S L = m.e(), R = m.e();
    for (; l < r; l >>= 1, r >>= 1) {
      if (l & 1) L = m.op(L, data[l++]);
      if (r & 1) R = m.op(data[--r], R);
    }
    return m.op(L, R);
  }

  S all_prod() const { return data[1]; }

  void apply(int k, const F& f) {
    k += sz;
    for (int i = height; i > 0; i--) propagate(k >> i);
    data[k] = m.mapping(data[k], f);
    for (int i = 1; i <= height; i++) update(k >> i);
  }

  void apply(int l, int r, const F& f) {
    if (l >= r) return;
    l += sz;
    r += sz;
    for (int i = height; i > 0; i--) {
      if (((l >> i) << i) != l) propagate(l >> i);
      if (((r >> i) << i) != r) propagate((r - 1) >> i);
    }
    {
      int l2 = l, r2 = r;
      for (; l < r; l >>= 1, r >>= 1) {
        if (l & 1) all_apply(l++, f);
        if (r & 1) all_apply(--r, f);
      }
      l = l2, r = r2;
    }
    for (int i = 1; i <= height; i++) {
      if (((l >> i) << i) != l) update(l >> i);
      if (((r >> i) << i) != r) update((r - 1) >> i);
    }
  }

  template <typename C>
  std::optional<int> find_first(int l, const C& check) {
    if (l >= n) return std::nullopt;
    l += sz;
    for (int i = height; i > 0; i--) propagate(l >> i);
    S sum = m.e();
    do {
      while ((l & 1) == 0) l >>= 1;
      if (check(m.op(sum, data[l]))) {
        while (l < sz) {
          propagate(l);
          l <<= 1;
          auto nxt = m.op(sum, data[l]);
          if (not check(nxt)) {
            sum = nxt;
            l++;
          }
        }
        return l + 1 - sz;
      }
      sum = m.op(sum, data[l++]);
    } while ((l & -l) != l);
    return std::nullopt;
  }

  template <typename C>
  std::optional<int> find_last(int r, const C& check) {
    if (r <= 0) return std::nullopt;
    r += sz;
    for (int i = height; i > 0; i--) propagate((r - 1) >> i);
    S sum = m.e();
    do {
      r--;
      while (r > 1 and (r & 1)) r >>= 1;
      if (check(m.op(data[r], sum))) {
        while (r < sz) {
          propagate(r);
          r = (r << 1) + 1;
          auto nxt = m.op(data[r], sum);
          if (not check(nxt)) {
            sum = nxt;
            r--;
          }
        }
        return r - sz;
      }
      sum = m.op(data[r], sum);
    } while ((r & -r) != r);
    return std::nullopt;
  }
};
#line 12 "test/verify/yosupo-range-affine-range-sum.test.cpp"

using namespace std;

using mint = modint998244353;

int main() {
  int N, Q;
  cin >> N >> Q;
  using monoid = RangeAffineRangeSum<mint>;
  vector<monoid::S> vs(N);
  for (int i = 0; i < N; i++) {
    mint a;
    cin >> a;
    vs[i] = {a, 1};
  }
  LazySegmentTree seg(monoid(), vs);
  for (int i = 0; i < Q; i++) {
    int t;
    cin >> t;
    if (t == 0) {
      int l, r;
      mint b, c;
      cin >> l >> r >> b >> c;
      seg.apply(l, r, {b, c});
    } else {
      int l, r;
      cin >> l >> r;
      cout << seg.prod(l, r).first << "\n";
    }
  }
}

Test cases

Env Name Status Elapsed Memory
g++ example_00 :heavy_check_mark: AC 2 ms 4 MB
g++ max_random_00 :heavy_check_mark: AC 1286 ms 24 MB
g++ max_random_01 :heavy_check_mark: AC 1342 ms 24 MB
g++ max_random_02 :heavy_check_mark: AC 1263 ms 24 MB
g++ random_00 :heavy_check_mark: AC 988 ms 23 MB
g++ random_01 :heavy_check_mark: AC 1075 ms 23 MB
g++ random_02 :heavy_check_mark: AC 711 ms 6 MB
g++ small_00 :heavy_check_mark: AC 3 ms 4 MB
g++ small_01 :heavy_check_mark: AC 3 ms 4 MB
g++ small_02 :heavy_check_mark: AC 4 ms 4 MB
g++ small_03 :heavy_check_mark: AC 3 ms 4 MB
g++ small_04 :heavy_check_mark: AC 4 ms 4 MB
g++ small_05 :heavy_check_mark: AC 3 ms 4 MB
g++ small_06 :heavy_check_mark: AC 3 ms 4 MB
g++ small_07 :heavy_check_mark: AC 3 ms 4 MB
g++ small_08 :heavy_check_mark: AC 3 ms 4 MB
g++ small_09 :heavy_check_mark: AC 3 ms 4 MB
g++ small_random_00 :heavy_check_mark: AC 4 ms 4 MB
g++ small_random_01 :heavy_check_mark: AC 4 ms 4 MB
clang++ example_00 :heavy_check_mark: AC 2 ms 4 MB
clang++ max_random_00 :heavy_check_mark: AC 1208 ms 24 MB
clang++ max_random_01 :heavy_check_mark: AC 1202 ms 24 MB
clang++ max_random_02 :heavy_check_mark: AC 1503 ms 24 MB
clang++ random_00 :heavy_check_mark: AC 1002 ms 23 MB
clang++ random_01 :heavy_check_mark: AC 1108 ms 23 MB
clang++ random_02 :heavy_check_mark: AC 826 ms 6 MB
clang++ small_00 :heavy_check_mark: AC 3 ms 4 MB
clang++ small_01 :heavy_check_mark: AC 3 ms 4 MB
clang++ small_02 :heavy_check_mark: AC 3 ms 4 MB
clang++ small_03 :heavy_check_mark: AC 3 ms 4 MB
clang++ small_04 :heavy_check_mark: AC 3 ms 4 MB
clang++ small_05 :heavy_check_mark: AC 3 ms 4 MB
clang++ small_06 :heavy_check_mark: AC 3 ms 4 MB
clang++ small_07 :heavy_check_mark: AC 3 ms 4 MB
clang++ small_08 :heavy_check_mark: AC 4 ms 4 MB
clang++ small_09 :heavy_check_mark: AC 3 ms 4 MB
clang++ small_random_00 :heavy_check_mark: AC 4 ms 4 MB
clang++ small_random_01 :heavy_check_mark: AC 4 ms 4 MB
Back to top page