Problem p02568 (C++) — buggy submission, verdict: Time Limit Exceeded

#line 1 "segment_tree_lazy.rsq_raffinq.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/range_affine_range_sum"
#line 2 "/home/koji0708/go/src/github.com/kjnh10/pcl/library/cpp/header.hpp"

//%snippet.set('header')%
//%snippet.fold()%
#ifndef HEADER_H
#define HEADER_H

// template version 2.0
using namespace std;
#include <bits/stdc++.h>

// varibable settings
const long long INF = 1e18;
template <class T> constexpr T inf = numeric_limits<T>::max() / 2.1;

#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define _rrep(i, n) rrepi(i, 0, n)
#define rrepi(i, a, b) for (ll i = (ll)((b)-1); i >= (ll)(a); --i)
#define r_rep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__)
#define each(i, a) for (auto &&i : a)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define mt(...) make_tuple(__VA_ARGS__)
#define ub upper_bound
#define lb lower_bound
#define lpos(A, x) (lower_bound(all(A), x) - A.begin())
#define upos(A, x) (upper_bound(all(A), x) - A.begin())
template <class T, class U> inline void chmax(T &a, const U &b) {
  if ((a) < (b))
    (a) = (b);
}
template <class T, class U> inline void chmin(T &a, const U &b) {
  if ((a) > (b))
    (a) = (b);
}
template <typename X, typename T> auto make_table(X x, T a) {
  return vector<T>(x, a);
}
template <typename X, typename Y, typename Z, typename... Zs>
auto make_table(X x, Y y, Z z, Zs... zs) {
  auto cont = make_table(y, z, zs...);
  return vector<decltype(cont)>(x, cont);
}

#define cdiv(a, b) (((a) + (b)-1) / (b))
#define is_in(x, a, b) ((a) <= (x) && (x) < (b))
#define uni(x)                                                                 \\
  sort(all(x));                                                                \\
  x.erase(unique(all(x)), x.end())
#define slice(l, r) substr(l, r - l)

typedef long long ll;
typedef long double ld;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;

template <typename T> using PQ = priority_queue<T, vector<T>, greater<T>>;
void check_input() {
  assert(cin.eof() == 0);
  int tmp;
  cin >> tmp;
  assert(cin.eof() == 1);
}

#if defined(PCM) || defined(LOCAL)
#else
#define dump(...) ;
#define dump_1d(...) ;
#define dump_2d(...) ;
#define cerrendl ;
#endif

#endif /* HEADER_H */
//%snippet.end()%
#line 3 "segment_tree_lazy.hpp"

//%snippet.set('segment_tree_lazy')%
//%snippet.config({'alias':'lazy'})%
//%snippet.fold()%

template <typename X, typename M> struct segment_tree_lazy {
  using FX = function<X(X, X)>;
  using FA = function<X(X, M)>;
  using FM = function<M(M, M)>;
  using FP = function<M(M, int)>;
  int n;
  FX fx;
  FA fa;
  FM fm;
  const X ex;
  const M em;
  vector<X> dat;
  vector<M> lazy;
  segment_tree_lazy(FX fx_, FA fa_, FM fm_, X ex_, M em_)
      : fx(fx_), fa(fa_), fm(fm_), ex(ex_), em(em_) {}

  void set(int i, X x) { dat[i + n - 1] = x; }
  void init(int n_) {
    n = 1;
    while (n < n_)
      n <<= 1;
    dat.assign(2 * n, ex);
    lazy.assign(2 * n, em);
  }

  void build(const vector<X> &v) {
    int n_ = v.size();
    init(n_);
    for (int i = 0; i < n_; i++)
      dat[i + n - 1] = v[i];
    for (int k = n - 2; k >= 0; k--)
      dat[k] = fx(dat[2 * k + 1], dat[2 * k + 2]);
  }

  /* lazy eval */
  void eval(int k) {
    if (lazy[k] == em)
      return;        // 更新するものが無ければ終了
    if (k < n - 1) { // 葉でなければ子に伝搬
      lazy[k * 2 + 1] = fm(lazy[k * 2 + 1], lazy[k]);
      lazy[k * 2 + 2] = fm(lazy[k * 2 + 2], lazy[k]);
    }
    // 自身を更新
    dat[k] = fa(dat[k], lazy[k]);
    lazy[k] = em;
  }

  void update(int a, int b, M x, int k, int l, int r) {
    eval(k);
    if (a <= l && r <= b) { // 完全に内側の時
      lazy[k] = fm(lazy[k], x);
      eval(k);
    } else if (a < r && l < b) {                  // 一部区間が被る時
      update(a, b, x, k * 2 + 1, l, (l + r) / 2); // 左の子
      update(a, b, x, k * 2 + 2, (l + r) / 2, r); // 右の子
      dat[k] = fx(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  void update(int a, int b, M x) { update(a, b, x, 0, 0, n); }

  X query_sub(int a, int b, int k, int l, int r) {
    eval(k);
    if (r <= a || b <= l) { // 完全に外側の時
      return ex;
    } else if (a <= l && r <= b) { // 完全に内側の時
      return dat[k];
    } else { // 一部区間が被る時
      X vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
      X vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
      return fx(vl, vr);
    }
  }
  X query(int a, int b) { return query_sub(a, b, 0, 0, n); }
};

/* SegTreeLazyProportional<X,M>(n,fx,fa,fm,fp,ex,em): モノイド(集合X,
   二項演算fx,fa,fm,p 単位元ex,em)についてサイズnで構築 set(int i, X x),
   build(): i番目の要素をxにセット。まとめてセグ木を構築する。O(n) update(i,x):
   i 番目の要素を x に更新。O(log(n)) query(a,b):  [a,b)
   全てにfxを作用させた値を取得。O(log(n))
*/

//%snippet.end()%

#line 3 "segment_tree_lazy.rsq_raffinq.test.cpp"
// #include "../../math/mint.hpp"

#include <atcoder/modint>
using namespace atcoder;
using mint = modint998244353;

struct monoidX {
  mint x;
  int len;
  monoidX(){};
  monoidX(mint x_, ll len_) : x(x_), len(len_){};
};
monoidX operator+(monoidX a, monoidX b) {
  return monoidX(a.x + b.x, a.len + b.len);
}

struct monoidM {
  mint a, b;
  monoidM(){};
  monoidM(mint a_, mint b_) : a(a_), b(b_){};
};
monoidM operator+(monoidM x, monoidM y) {
  return monoidM(x.a * y.a, x.b * y.a + y.b);
}
bool operator==(monoidM m1, monoidM m2) { return m1.a == m2.a && m1.b == m2.b; }

int main() {
  ll n, q;
  cin >> n >> q;
  vector<ll> a(n);

  rep(i, n) { cin >> a[i]; }

  using X = monoidX; // (X, merge) is monoid
  auto merge = [](X x1, X x2) { return x1 + x2; };
  X ex = X(0, 0); // s.t merge(x, ex) = x

  using M = monoidM; // (M, composition) is monoid
  M em = M(1, 0);
  // s.t composition(m, em) = m && s.t composition(em, m) = m && apply(x, em) =
  // x
  auto composition = [&](M m1, M m2) { return m1 + m2; };
  auto apply = [&](X x, M m) { return X(x.x * m.a + m.b * x.len, x.len); };

  // Regarding apply and merge, the conditions below should holds.
  // apply(merge(x1, x2), m) = merge(apply(x1, m), apply(x2, m))
  // apply(apply(x, m1), m2) = apply(x, composition(m1, m2))

  segment_tree_lazy<X, M> lseg(merge, apply, composition, ex, em);
  vector<X> b(n);
  rep(i, n) b[i] = X(a[i], 1);
  lseg.build(b);

  rep(i, q) {
    int ty;
    cin >> ty;
    if (ty == 0) {
      ll l, r, b, c;
      cin >> l >> r >> b >> c;
      lseg.update(l, r, M(b, c));
    }
    if (ty == 1) {
      ll l, r;
      cin >> l >> r;
      cout << lseg.query(l, r).x.val() << endl;
    }
  }
}


Error: TLE

Fixed submission:

#line 1 "segment_tree_lazy.rsq_raffinq.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/range_affine_range_sum"
#line 2 "/home/koji0708/go/src/github.com/kjnh10/pcl/library/cpp/header.hpp"

//%snippet.set('header')%
//%snippet.fold()%
#ifndef HEADER_H
#define HEADER_H

// template version 2.0
using namespace std;
#include <bits/stdc++.h>

// varibable settings
const long long INF = 1e18;
template <class T> constexpr T inf = numeric_limits<T>::max() / 2.1;

#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define _rrep(i, n) rrepi(i, 0, n)
#define rrepi(i, a, b) for (ll i = (ll)((b)-1); i >= (ll)(a); --i)
#define r_rep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__)
#define each(i, a) for (auto &&i : a)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define mt(...) make_tuple(__VA_ARGS__)
#define ub upper_bound
#define lb lower_bound
#define lpos(A, x) (lower_bound(all(A), x) - A.begin())
#define upos(A, x) (upper_bound(all(A), x) - A.begin())
template <class T, class U> inline void chmax(T &a, const U &b) {
  if ((a) < (b))
    (a) = (b);
}
template <class T, class U> inline void chmin(T &a, const U &b) {
  if ((a) > (b))
    (a) = (b);
}
template <typename X, typename T> auto make_table(X x, T a) {
  return vector<T>(x, a);
}
template <typename X, typename Y, typename Z, typename... Zs>
auto make_table(X x, Y y, Z z, Zs... zs) {
  auto cont = make_table(y, z, zs...);
  return vector<decltype(cont)>(x, cont);
}

#define cdiv(a, b) (((a) + (b)-1) / (b))
#define is_in(x, a, b) ((a) <= (x) && (x) < (b))
#define uni(x)                                                                 \\
  sort(all(x));                                                                \\
  x.erase(unique(all(x)), x.end())
#define slice(l, r) substr(l, r - l)

typedef long long ll;
typedef long double ld;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;

template <typename T> using PQ = priority_queue<T, vector<T>, greater<T>>;
void check_input() {
  assert(cin.eof() == 0);
  int tmp;
  cin >> tmp;
  assert(cin.eof() == 1);
}

#if defined(PCM) || defined(LOCAL)
#else
#define dump(...) ;
#define dump_1d(...) ;
#define dump_2d(...) ;
#define cerrendl ;
#endif

#endif /* HEADER_H */
//%snippet.end()%
#line 3 "segment_tree_lazy.hpp"

//%snippet.set('segment_tree_lazy')%
//%snippet.config({'alias':'lazy'})%
//%snippet.fold()%

template <typename X, typename M> struct segment_tree_lazy {
  using FX = function<X(X, X)>;
  using FA = function<X(X, M)>;
  using FM = function<M(M, M)>;
  using FP = function<M(M, int)>;
  int n;
  FX fx;
  FA fa;
  FM fm;
  const X ex;
  const M em;
  vector<X> dat;
  vector<M> lazy;
  segment_tree_lazy(FX fx_, FA fa_, FM fm_, X ex_, M em_)
      : fx(fx_), fa(fa_), fm(fm_), ex(ex_), em(em_) {}

  void set(int i, X x) { dat[i + n - 1] = x; }
  void init(int n_) {
    n = 1;
    while (n < n_)
      n <<= 1;
    dat.assign(2 * n, ex);
    lazy.assign(2 * n, em);
  }

  void build(const vector<X> &v) {
    int n_ = v.size();
    init(n_);
    for (int i = 0; i < n_; i++)
      dat[i + n - 1] = v[i];
    for (int k = n - 2; k >= 0; k--)
      dat[k] = fx(dat[2 * k + 1], dat[2 * k + 2]);
  }

  /* lazy eval */
  void eval(int k) {
    if (lazy[k] == em)
      return;        // 更新するものが無ければ終了
    if (k < n - 1) { // 葉でなければ子に伝搬
      lazy[k * 2 + 1] = fm(lazy[k * 2 + 1], lazy[k]);
      lazy[k * 2 + 2] = fm(lazy[k * 2 + 2], lazy[k]);
    }
    // 自身を更新
    dat[k] = fa(dat[k], lazy[k]);
    lazy[k] = em;
  }

  void update(int a, int b, M x, int k, int l, int r) {
    eval(k);
    if (a <= l && r <= b) { // 完全に内側の時
      lazy[k] = fm(lazy[k], x);
      eval(k);
    } else if (a < r && l < b) {                  // 一部区間が被る時
      update(a, b, x, k * 2 + 1, l, (l + r) / 2); // 左の子
      update(a, b, x, k * 2 + 2, (l + r) / 2, r); // 右の子
      dat[k] = fx(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  void update(int a, int b, M x) { update(a, b, x, 0, 0, n); }

  X query_sub(int a, int b, int k, int l, int r) {
    eval(k);
    if (r <= a || b <= l) { // 完全に外側の時
      return ex;
    } else if (a <= l && r <= b) { // 完全に内側の時
      return dat[k];
    } else { // 一部区間が被る時
      X vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
      X vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
      return fx(vl, vr);
    }
  }
  X query(int a, int b) { return query_sub(a, b, 0, 0, n); }
};

/* SegTreeLazyProportional<X,M>(n,fx,fa,fm,fp,ex,em): モノイド(集合X,
   二項演算fx,fa,fm,p 単位元ex,em)についてサイズnで構築 set(int i, X x),
   build(): i番目の要素をxにセット。まとめてセグ木を構築する。O(n) update(i,x):
   i 番目の要素を x に更新。O(log(n)) query(a,b):  [a,b)
   全てにfxを作用させた値を取得。O(log(n))
*/

//%snippet.end()%

#line 3 "/home/koji0708/go/src/github.com/kjnh10/pcl/library/cpp/math/mint.hpp"

//%snippet.set('mint')%
int mod = 1e9 + 7;
// int mod = 998244353;
struct mint { //{{{
  ll x;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}

  // ?= operator
  mint &operator+=(const mint a) {
    (x += a.x) %= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    (x += mod - a.x) %= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint &operator/=(const mint &rhs) {
    if (rhs.x == 0)
      throw runtime_error("mint zero division");
    return *this *= rhs.inv();
  }

  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }

  mint pow(ll n) const {
    mint res(1), x(*this);
    if (n < 0) {
      n = -n;
      x = (*this).inv();
    }
    while (n) {
      if (n & 1)
        res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }

  mint inv() const {
    if (x == 0)
      throw runtime_error("inv does not exist");
    return pow(mod - 2);
  }
  // mint inv()const{
  //     int x,y;
  //     int g=extgcd(v,mod,x,y);
  //     assert(g==1);
  //     if(x<0)x+=mod;
  //     return mint(x);
  // }

  bool operator<(const mint &r) const { return x < r.x; }
  bool operator==(const mint &r) const { return x == r.x; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
//}}}
#line 4 "segment_tree_lazy.rsq_raffinq.test.cpp"

struct monoidX {
  mint x;
  int len;
  monoidX(mint x_, ll len_) : x(x_), len(len_){};
};
monoidX operator+(monoidX a, monoidX b) {
  return monoidX(a.x + b.x, a.len + b.len);
}

struct monoidM {
  mint a, b;
  monoidM(mint a_, mint b_) : a(a_), b(b_){};
};
monoidM operator+(monoidM x, monoidM y) {
  return monoidM(x.a * y.a, x.b * y.a + y.b);
}
bool operator==(monoidM m1, monoidM m2) { return m1.a == m2.a && m1.b == m2.b; }

int main() {
  ll n, q;
  cin >> n >> q;
  vector<ll> a(n);
  mod = 998244353;

  rep(i, n) { cin >> a[i]; }

  using X = monoidX; // (X, merge) is monoid
  auto merge = [](X x1, X x2) { return x1 + x2; };
  X ex = X(0, 0); // s.t merge(x, ex) = x

  using M = monoidM; // (M, composition) is monoid
  M em = M(1, 0);
  // s.t composition(m, em) = m && s.t composition(em, m) = m && apply(x, em) =
  // x
  auto composition = [&](M m1, M m2) { return m1 + m2; };
  auto apply = [&](X x, M m) { return X(x.x * m.a + m.b * x.len, x.len); };

  // Regarding apply and merge, the conditions below should holds.
  // apply(merge(x1, x2), m) = merge(apply(x1, m), apply(x2, m))
  // apply(apply(x, m1), m2) = apply(x, composition(m1, m2))

  segment_tree_lazy<X, M> lseg(merge, apply, composition, ex, em);
  vector<X> b;
  rep(i, n) b.emplace_back(a[i], 1);
  lseg.build(b);

  rep(i, q) {
    int ty;
    cin >> ty;
    if (ty == 0) {
      ll l, r, b, c;
      cin >> l >> r >> b >> c;
      lseg.update(l, r, M(b, c));
    }
    if (ty == 1) {
      ll l, r;
      cin >> l >> r;
      cout << lseg.query(l, r).x << endl;
    }
  }
}