algo

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub dnx04/algo

:heavy_check_mark: tests/Range_Affine_Range_Sum.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/range_affine_range_sum"

#include "../misc/macros.h"
#include "../math/ModInt.h"
#include "../ds/LazySegTree.h"

using Fp = modint<998244353>;

void solve() {
  int n, q;
  cin >> n >> q;

  using P = pair<Fp, Fp>;
  auto f = [](P a, P b) { return P{a.first + b.first, a.second + b.second}; };
  auto m = [](P a, P b) { return P{a.first * b.first + a.second * b.second, a.second}; };
  auto c = [](P a, P b) { return P{a.first * b.first, a.second * b.first + b.second}; };
  P I = {0, 0};
  P L0 = {1, 0};
  LazySegTree t(n, I, L0, f, m, c);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    t.set(i, P{x, 1});
  }
  while (q--) {
    int cmd;
    cin >> cmd;
    if (cmd == 0) {
      int l, r, c, d;
      cin >> l >> r >> c >> d;
      t.apply(l, r, P{c, d});
    } else {
      int l, r;
      cin >> l >> r;
      cout << t.query(l, r).first << '\n';
    }
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  int tc = 1;
  // cin >> tc;
  for (int i = 1; i <= tc; ++i) {
    solve();
  }
}
#line 1 "tests/Range_Affine_Range_Sum.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/range_affine_range_sum"

#line 1 "misc/macros.h"
// #pragma GCC optimize("Ofast,unroll-loops")       // unroll long, simple loops
// #pragma GCC target("avx2,fma")                   // vectorizing code
// #pragma GCC target("lzcnt,popcnt,abm,bmi,bmi2")  // for fast bitset operation

#include <bits/extc++.h>
#include <tr2/dynamic_bitset>

using namespace std;
using namespace __gnu_pbds;  // ordered_set, gp_hash_table
// using namespace __gnu_cxx; // rope

// for templates to work
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#define pb push_back
#define eb emplace_back
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using ld = long double;
using pii = pair<i32, i32>;
using vi = vector<i32>;

// fast map
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash {  // customize hash function for gp_hash_table
  int operator()(int x) const { return x ^ RANDOM; }
};
gp_hash_table<int, int, chash> table;

/* ordered set
    find_by_order(k): returns an iterator to the k-th element (0-based)
    order_of_key(k): returns the number of elements in the set that are strictly less than k
*/
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

/*  rope
    rope <int> cur = v.substr(l, r - l + 1);
    v.erase(l, r - l + 1);
    v.insert(v.mutable_begin(), cur);
*/
#line 2 "math/ModInt.h"

template <int mod>
struct modint {
  using M = modint;
  static_assert(mod > 0 && mod <= 2147483647);
  static constexpr int modulo = mod;
  static constexpr u32 r1 = []() {
    u32 r1 = mod;
    for (int i = 0; i < 5; ++i) r1 *= 2 - mod * r1;
    return -r1;
  }();
  static constexpr u32 r2 = -u64(mod) % mod;
  static u32 reduce(u64 x) {
    u32 y = u32(x) * r1, r = (x + u64(y) * mod) >> 32;
    return r >= mod ? r - mod : r;
  }
  u32 x;
  modint() : x(0) {}
  modint(i64 x) : x(reduce(u64(x % mod + mod) * r2)) {}
  M& operator+=(const M& a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  M& operator-=(const M& a) {
    if ((x += mod - a.x) >= mod) x -= mod;
    return *this;
  }
  M& operator*=(const M& a) {
    x = reduce(u64(x) * a.x);
    return *this;
  }
  M& operator/=(const M& a) { return *this *= a.inv(); }
  M operator-() const { return M(0) - *this; }
  M operator+(const M& a) const { return M(*this) += a; }
  M operator-(const M& a) const { return M(*this) -= a; }
  M operator*(const M& a) const { return M(*this) *= a; }
  M operator/(const M& a) const { return M(*this) /= a; }
  bool operator==(const M& a) const { return x == a.x; }
  bool operator!=(const M& a) const { return x != a.x; }
  M pow(u64 k) const {
    M res(1), b = *this;
    while (k) {
      if (k & 1) res *= b;
      b *= b, k >>= 1;
    }
    return res;
  }
  M inv() const { return pow(mod - 2); }
  friend ostream& operator<<(ostream& os, const M& a) {
    return os << reduce(a.x);
  }
  friend istream& operator>>(istream& is, M& a) {
    i64 v;
    is >> v;
    a = M(v);
    return is;
  }
};

u64 modmul(u64 x, u64 y, u64 m) { return u128(x) * y % m; }
u64 modpow(u64 x, u64 k, u64 m) {
  u64 res = 1;
  while (k) {
    if (k & 1) res = modmul(res, x, m);
    x = modmul(x, x, m);
    k >>= 1;
  }
  return res;
}
#line 1 "ds/LazySegTree.h"
// 0-indexed
template <class T, class L, class F, class M, class C>
struct LazySegTree {
 private:
  int n, h;
  vector<T> seg;
  vector<L> laz;
  const T I;   // Identity node (e.g., 0 for sum, INF for min)
  const L L0;  // Identity laz (e.g., 0 for add, -1 for set)
  const F f;   // f: Merge 2 nodes (T, T) -> T
  const M m;   // m: Mapping laz to node (T, L) -> T
  const C c;   // c: Composition 2 laz (L prev, L next) -> next(prev)
  void apply(int p, L val) {
    seg[p] = m(seg[p], val);
    if (p < n) laz[p] = c(laz[p], val);
  }
  void pull(int p) {
    while (p > 1) {
      p >>= 1;
      seg[p] = m(f(seg[p << 1], seg[p << 1 | 1]), laz[p]);
    }
  }
  void push(int p) {
    for (int s = h; s > 0; --s) {
      int i = p >> s;
      if (laz[i] != L0) apply(i << 1, laz[i]), apply(i << 1 | 1, laz[i]), laz[i] = L0;
    }
  }

 public:
  LazySegTree(int n, T I, L L0, F f, M m, C c) : n(n), h(32 - __builtin_clz(n)), seg(n << 1 | 1, I), laz(n, L0), I(I), L0(L0), f(f), m(m), c(c) {}
  // set p to x
  void set(int p, T x) {
    p += n;
    for (int i = h; i > 0; --i) {
      int k = p >> i;
      if (laz[k] != L0) apply(k << 1, laz[k]), apply(k << 1 | 1, laz[k]), laz[k] = L0;
    }
    seg[p] = x, pull(p);
  }
  // Apply op -> [l, r)
  void apply(int l, int r, L op) {
    l += n, r += n;
    int l0 = l, r0 = r;
    push(l0), push(r0 - 1);
    for (; l < r; l >>= 1, r >>= 1) {
      if (l & 1) apply(l++, op);
      if (r & 1) apply(--r, op);
    }
    pull(l0), pull(r0 - 1);
  }
  // Query [l, r)
  T query(int l, int r) {
    l += n, r += n;
    push(l), push(r - 1);
    T resL = I, resR = I;
    for (; l < r; l >>= 1, r >>= 1) {
      if (l & 1) resL = f(resL, seg[l++]);
      if (r & 1) resR = f(seg[--r], resR);
    }
    return f(resL, resR);
  }
};
#line 6 "tests/Range_Affine_Range_Sum.test.cpp"

using Fp = modint<998244353>;

void solve() {
  int n, q;
  cin >> n >> q;

  using P = pair<Fp, Fp>;
  auto f = [](P a, P b) { return P{a.first + b.first, a.second + b.second}; };
  auto m = [](P a, P b) { return P{a.first * b.first + a.second * b.second, a.second}; };
  auto c = [](P a, P b) { return P{a.first * b.first, a.second * b.first + b.second}; };
  P I = {0, 0};
  P L0 = {1, 0};
  LazySegTree t(n, I, L0, f, m, c);
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    t.set(i, P{x, 1});
  }
  while (q--) {
    int cmd;
    cin >> cmd;
    if (cmd == 0) {
      int l, r, c, d;
      cin >> l >> r >> c >> d;
      t.apply(l, r, P{c, d});
    } else {
      int l, r;
      cin >> l >> r;
      cout << t.query(l, r).first << '\n';
    }
  }
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  int tc = 1;
  // cin >> tc;
  for (int i = 1; i <= tc; ++i) {
    solve();
  }
}
Back to top page