Skip to the content.

:heavy_check_mark: verify/segment-tree/LC_point_set_range_composite.test.cpp

Depends on

Code

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

#include "template/template.hpp"
#include "segment-tree/segment-tree.hpp"
#include "modint/modint.hpp"
using mint = ModInt<998244353>;
struct F {
  mint a, b;
  mint eval(mint x) { return a * x + b; }
};
F op(F f, F g) { return {f.a * g.a, f.b * g.a + g.b}; }
F e() { return {1, 0}; }

int main() {
  int n, q;
  in(n, q);
  vector<F> f(n);
  rep(i, 0, n) {
    mint a, b;
    in(a, b);
    f[i] = {a, b};
  }
  SegmentTree<F, op, e> seg(f);
  while (q--) {
    int t;
    in(t);
    if (t == 0) {
      int p;
      mint c, d;
      in(p, c, d);
      seg.set(p, {c, d});
    } else {
      int l, r;
      mint x;
      in(l, r, x);
      out(seg.prod(l, r).eval(x));
    }
  }
}
#line 1 "verify/segment-tree/LC_point_set_range_composite.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/point_set_range_composite"

#line 2 "template/template.hpp"
#include <bits/stdc++.h>
using namespace std;

#line 2 "template/macro.hpp"
#define rep(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b) - 1; i >= (a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())
#define YN(b) cout << ((b) ? "YES" : "NO") << "\n";
#define Yn(b) cout << ((b) ? "Yes" : "No") << "\n";
#define yn(b) cout << ((b) ? "yes" : "no") << "\n";
#line 6 "template/template.hpp"

#line 2 "template/util.hpp"
using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;

template <class T, class S = T>
S SUM(const vector<T> &a) {
  return accumulate(ALL(a), S(0));
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T>
int popcnt(T x) {
  return __builtin_popcountll(x);
}
template <class T>
int topbit(T x) {
  return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <class T>
int lowbit(T x) {
  return (x == 0 ? -1 : __builtin_ctzll(x));
}
#line 8 "template/template.hpp"

#line 2 "template/inout.hpp"
struct Fast {
  Fast() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} fast;

template <class T1, class T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << " " << p.second;
}
template <class T>
istream &operator>>(istream &is, vector<T> &a) {
  for (auto &v : a) is >> v;
  return is;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &a) {
  for (auto it = a.begin(); it != a.end();) {
    os << *it;
    if (++it != a.end()) os << " ";
  }
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const set<T> &st) {
  os << "{";
  for (auto it = st.begin(); it != st.end();) {
    os << *it;
    if (++it != st.end()) os << ",";
  }
  os << "}";
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
  os << "{";
  for (auto it = mp.begin(); it != mp.end();) {
    os << it->first << ":" << it->second;
    if (++it != mp.end()) os << ",";
  }
  os << "}";
  return os;
}

void in() {}
template <typename T, class... U>
void in(T &t, U &...u) {
  cin >> t;
  in(u...);
}
void out() { cout << "\n"; }
template <typename T, class... U, char sep = ' '>
void out(const T &t, const U &...u) {
  cout << t;
  if (sizeof...(u)) cout << sep;
  out(u...);
}
#line 10 "template/template.hpp"

#line 2 "template/debug.hpp"
#ifdef LOCAL
#define debug 1
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define debug 0
#define show(...) true
#endif
template <class T>
void _show(int i, T name) {
  cerr << '\n';
}
template <class T1, class T2, class... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
  for (; a[i] != ',' && a[i] != '\0'; i++) cerr << a[i];
  cerr << ":" << b << " ";
  _show(i + 1, a, c...);
}
#line 2 "segment-tree/segment-tree.hpp"

template <class T, T (*op)(T, T), T (*e)()>
struct SegmentTree {
 private:
  int n;
  vector<T> d;
  void update(int p) { d[p] = op(d[2 * p], d[2 * p + 1]); }

 public:
  SegmentTree() : SegmentTree(0) {}
  explicit SegmentTree(int sz) : SegmentTree(vector<T>(sz, e())) {}
  explicit SegmentTree(const vector<T>& v) : n(v.size()) {
    d.assign(2 * n, e());
    for (int i = 0; i < v.size(); i++) d[n + i] = v[i];
    for (int i = n - 1; i > 0; i--) update(i);
  }
  void clear() { fill(d.begin(), d.end(), e()); }

  void set_without_update(int p, T v) { d[n + p] = v; }
  void all_update() {
    for (int i = n - 1; i > 0; i--) update(i);
  }
  T get(int p) {
    assert(0 <= p && p <= n);
    return d[p + n];
  }
  void set(int p, T v) {
    assert(0 <= p && p <= n);
    d[p += n] = v;
    for (p >>= 1; p > 0; p >>= 1) update(p);
  }
  void apply(int p, T v) {
    assert(0 <= p && p <= n);
    p += n;
    d[p] = op(d[p], v);
    for (p >>= 1; p > 0; p >>= 1) update(p);
  }
  T prod(int l, int r) {
    if (l >= r) return e();
    assert(0 <= l && l <= r && r <= n);
    T sl = e(), sr = e();
    l += n, r += n;
    while (l < r) {
      if ((l & 1) != 0) sl = op(sl, d[l++]);
      if ((r & 1) != 0) sr = op(d[--r], sr);
      l >>= 1, r >>= 1;
    }
    return op(sl, sr);
  }
  T all_prod() { return prod(0, n); }

  template <bool (*f)(T)>
  int max_right(int l) const {
    return max_right(l, [](T x) { return f(x); });
  }
  template <class F>
  int max_right(int l, F f) const {
    assert(0 <= l && l <= n);
    assert(f(e()));
    if (l == n) return n;
    int x = n + l, w = 1;
    T s = e();
    for (; l + w <= n; x >>= 1, w <<= 1)
      if (x & 1) {
        if (!f(op(s, d[x]))) break;
        s = op(s, d[x++]);
        l += w;
      }
    while (x <<= 1, w >>= 1) {
      if (l + w <= n && f(op(s, d[x]))) {
        s = op(s, d[l++]);
        l += w;
      }
    }
    return l;
  }

  template <bool (*f)(T)>
  int min_left(int r) const {
    return min_left(r, [](T x) { return f(x); });
  }
  template <class F>
  int min_left(int r, F f) const {
    assert(0 <= r && r <= n);
    assert(f(e()));
    if (r == 0) return 0;
    int x = n + r, w = 1;
    T s = e();
    for (; r - w >= 0; x >>= 1, w <<= 1)
      if (x & 1) {
        if (!f(op(d[x - 1], s))) break;
        s = op(d[--x], s);
        r -= w;
      }
    while (x <<= 1, w >>= 1) {
      if (r - w >= 0 && f(op(d[x - 1], s))) {
        s = op(d[--x], s);
        r -= w;
      }
    }
    return r;
  }
};

/**
 * @brief Segment Tree
 * @docs docs/segment-tree/segment-tree.md
 */
#line 2 "modint/modint.hpp"

template <unsigned int m = 998244353>
struct ModInt {
  using mint = ModInt;
  unsigned int _v;
  static constexpr unsigned int get_mod() { return m; }
  static mint raw(int v) {
    mint x;
    x._v = v;
    return x;
  }
  ModInt() : _v(0) {}
  ModInt(int64_t v) {
    long long x = (long long)(v % (long long)(umod()));
    if (x < 0) x += umod();
    _v = (unsigned int)(x);
  }
  unsigned int val() const { return _v; }
  mint &operator++() {
    _v++;
    if (_v == umod()) _v = 0;
    return *this;
  }
  mint &operator--() {
    if (_v == 0) _v = umod();
    _v--;
    return *this;
  }
  mint operator++(int) {
    mint result = *this;
    ++*this;
    return result;
  }
  mint operator--(int) {
    mint result = *this;
    --*this;
    return result;
  }

  mint &operator+=(const mint &rhs) {
    _v += rhs._v;
    if (_v >= umod()) _v -= umod();
    return *this;
  }
  mint &operator-=(const mint &rhs) {
    _v -= rhs._v;
    if (_v >= umod()) _v += umod();
    return *this;
  }
  mint &operator*=(const mint &rhs) {
    unsigned long long z = _v;
    z *= rhs._v;
    _v = (unsigned int)(z % umod());
    return *this;
  }
  mint &operator/=(const mint &rhs) { return *this = *this * rhs.inv(); }

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

  mint pow(long long n) const {
    assert(0 <= n);
    mint x = *this, r = 1;
    while (n) {
      if (n & 1) r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  mint inv() const {
    assert(_v);
    return pow(umod() - 2);
  }

  friend mint operator+(const mint &lhs, const mint &rhs) {
    return mint(lhs) += rhs;
  }
  friend mint operator-(const mint &lhs, const mint &rhs) {
    return mint(lhs) -= rhs;
  }
  friend mint operator*(const mint &lhs, const mint &rhs) {
    return mint(lhs) *= rhs;
  }
  friend mint operator/(const mint &lhs, const mint &rhs) {
    return mint(lhs) /= rhs;
  }
  friend bool operator==(const mint &lhs, const mint &rhs) {
    return lhs._v == rhs._v;
  }
  friend bool operator!=(const mint &lhs, const mint &rhs) {
    return lhs._v != rhs._v;
  }
  friend istream &operator>>(istream &is, mint &x) {
    return is >> x._v;
  }
  friend ostream &operator<<(ostream &os, const mint &x) {
    return os << x.val();
  }

 private:
  static constexpr unsigned int umod() { return m; }
};
#line 6 "verify/segment-tree/LC_point_set_range_composite.test.cpp"
using mint = ModInt<998244353>;
struct F {
  mint a, b;
  mint eval(mint x) { return a * x + b; }
};
F op(F f, F g) { return {f.a * g.a, f.b * g.a + g.b}; }
F e() { return {1, 0}; }

int main() {
  int n, q;
  in(n, q);
  vector<F> f(n);
  rep(i, 0, n) {
    mint a, b;
    in(a, b);
    f[i] = {a, b};
  }
  SegmentTree<F, op, e> seg(f);
  while (q--) {
    int t;
    in(t);
    if (t == 0) {
      int p;
      mint c, d;
      in(p, c, d);
      seg.set(p, {c, d});
    } else {
      int l, r;
      mint x;
      in(l, r, x);
      out(seg.prod(l, r).eval(x));
    }
  }
}
Back to top page