This documentation is automatically generated by online-judge-tools/verification-helper
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/range_affine_range_sum
#include "../../template/template.hpp"
#include "../../math/combinatorics/montgomery-mod-int.hpp"
#include "../../other/vector-pool.hpp"
#include "../../structure/bbst/lazy-weight-balanced-tree.hpp"
using mint = modint998244353;
int main() {
int N, Q;
cin >> N >> Q;
using pi = pair< mint, int >;
using qi = pair< mint, mint >;
auto f = [](const pi &a, const pi &b) -> pi {
return {a.first + b.first, a.second + b.second};
};
auto g = [](const pi &a, const qi &b) -> pi {
return {a.first * b.first + mint(a.second) * b.second, a.second};
};
auto h = [](const qi &a, const qi &b) -> qi {
return {a.first * b.first, a.second * b.first + b.second};
};
LazyWeightBalancedTree< pi, qi, decltype(f), decltype(g), decltype(h) > rbt(2 * N, f, g, h, pi(0, 0), pi(1, 0));
vector< pi > A(N);
for(int i = 0; i < N; i++) {
mint a;
cin >> a;
A[i] = {a, 1};
}
auto root = rbt.build(A);
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;
rbt.set_propagate(root, l, r, qi(b, c));
} else {
int l, r;
cin >> l >> r;
cout << rbt.query(root, l, r).first << "\n";
}
}
}
#line 1 "test/verify/yosupo-range-affine-range-sum-3.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/range_affine_range_sum
#line 1 "template/template.hpp"
#include<bits/stdc++.h>
using namespace std;
using int64 = long long;
const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
os << p.first << " " << p.second;
return os;
}
template< typename T1, typename T2 >
istream &operator>>(istream &is, pair< T1, T2 > &p) {
is >> p.first >> p.second;
return is;
}
template< typename T >
ostream &operator<<(ostream &os, const vector< T > &v) {
for(int i = 0; i < (int) v.size(); i++) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
for(T &in : v) is >> in;
return is;
}
template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
template< typename T = int64 >
vector< T > make_v(size_t a) {
return vector< T >(a);
}
template< typename T, typename... Ts >
auto make_v(size_t a, Ts... ts) {
return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...));
}
template< typename T, typename V >
typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) {
t = v;
}
template< typename T, typename V >
typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) {
for(auto &e : t) fill_v(e, v);
}
template< typename F >
struct FixPoint : F {
explicit FixPoint(F &&f) : F(forward< F >(f)) {}
template< typename... Args >
decltype(auto) operator()(Args &&... args) const {
return F::operator()(*this, forward< Args >(args)...);
}
};
template< typename F >
inline decltype(auto) MFP(F &&f) {
return FixPoint< F >{forward< F >(f)};
}
#line 4 "test/verify/yosupo-range-affine-range-sum-3.test.cpp"
#line 2 "math/combinatorics/montgomery-mod-int.hpp"
template< uint32_t mod_, bool fast = false >
struct MontgomeryModInt {
private:
using mint = MontgomeryModInt;
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = 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 ostream &operator<<(ostream &os, const mint &p) {
return os << p.val();
}
friend istream &operator>>(istream &is, mint &a) {
i64 t;
is >> t;
a = mint(t);
return is;
}
static constexpr u32 mod() { return mod_; }
};
template< uint32_t mod >
using modint = MontgomeryModInt< mod >;
using modint998244353 = modint< 998244353 >;
using modint1000000007 = modint< 1000000007 >;
#line 6 "test/verify/yosupo-range-affine-range-sum-3.test.cpp"
#line 1 "other/vector-pool.hpp"
template< class T >
struct VectorPool {
vector< T > pool;
vector< T * > stock;
int ptr;
VectorPool() = default;
VectorPool(int sz) : pool(sz), stock(sz) {}
inline T *alloc() { return stock[--ptr]; }
inline void free(T *t) { stock[ptr++] = t; }
void clear() {
ptr = (int) pool.size();
for(int i = 0; i < pool.size(); i++) stock[i] = &pool[i];
}
};
#line 8 "test/verify/yosupo-range-affine-range-sum-3.test.cpp"
#line 1 "structure/bbst/lazy-weight-balanced-tree.hpp"
/**
* @brief Lazy-Weight-Balanced-Tree(遅延伝搬重み平衡木)
*/
template< typename Monoid, typename OperatorMonoid, typename F, typename G, typename H >
struct LazyWeightBalancedTree {
public:
struct Node {
Node *l, *r;
int cnt;
Monoid key, sum;
OperatorMonoid lazy;
Node() {}
Node(const Monoid &k, const OperatorMonoid &laz) : key(k), sum(k), l(nullptr), r(nullptr), cnt(1), lazy(laz) {}
Node(Node *l, Node *r, const Monoid &k, const OperatorMonoid &laz) : key(k), l(l), r(r), lazy(laz) {}
bool is_leaf() { return !l || !r; }
};
private:
Node *propagate(Node *t) {
t = clone(t);
if(t->lazy != OM0) {
if(t->is_leaf()) {
t->key = g(t->key, t->lazy);
} else {
if(t->l) {
t->l = clone(t->l);
t->l->lazy = h(t->l->lazy, t->lazy);
t->l->sum = g(t->l->sum, t->lazy);
}
if(t->r) {
t->r = clone(t->r);
t->r->lazy = h(t->r->lazy, t->lazy);
t->r->sum = g(t->r->sum, t->lazy);
}
}
t->lazy = OM0;
}
return update(t);
}
Node *update(Node *t) {
t->cnt = count(t->l) + count(t->r) + t->is_leaf();
t->sum = f(f(sum(t->l), t->key), sum(t->r));
return t;
}
inline Node *alloc(Node *l, Node *r) {
auto t = &(*pool.alloc() = Node(l, r, M1, OM0));
return update(t);
}
Node *submerge(Node *l, Node *r) {
if(count(l) > count(r) * 4) {
l = propagate(l);
auto nl = propagate(l->l);
auto nr = submerge(l->r, r);
if(count(nl) * 4 >= count(nr)) {
l->r = nr;
return update(l);
}
l->r = nr->l;
nr->l = l;
update(l);
return update(nr);
}
if(count(l) * 4 < count(r)) {
r = propagate(r);
auto nl = submerge(l, r->l);
auto nr = propagate(r->r);
if(count(nl) <= count(nr) * 4) {
r->l = nl;
return update(r);
}
r->l = nl->r;
nl->r = r;
update(r);
return update(nl);
}
return alloc(l, r);
}
Node *build(int l, int r, const vector< Monoid > &v) {
if(l + 1 >= r) return alloc(v[l]);
return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v));
}
void dump(Node *r, typename vector< Monoid >::iterator &it, OperatorMonoid lazy) {
if(r->lazy != OM0) lazy = h(lazy, r->lazy);
if(r->is_leaf()) {
*it++ = g(r->key, lazy);
return;
}
dump(r->l, it, lazy);
dump(r->r, it, lazy);
}
virtual Node *clone(Node *t) {
return t;
}
Node *merge(Node *l) {
return l;
}
public:
VectorPool< Node > pool;
const F f;
const G g;
const H h;
const Monoid M1;
const OperatorMonoid OM0;
LazyWeightBalancedTree(int sz, const F &f, const G &g, const H &h, const Monoid &M1, const OperatorMonoid &OM0)
: pool(sz), M1(M1), f(f), g(g), h(h), OM0(OM0) {
pool.clear();
}
inline Node *alloc(const Monoid &key) {
return &(*pool.alloc() = Node(key, OM0));
}
static inline int count(const Node *t) { return t ? t->cnt : 0; }
inline const Monoid &sum(const Node *t) { return t ? t->sum : M1; }
pair< Node *, Node * > split(Node *t, int k) {
if(!t) return {nullptr, nullptr};
t = propagate(t);
if(k == 0) return {nullptr, t};
if(k >= count(t)) return {t, nullptr};
Node *l = t->l, *r = t->r;
pool.free(t);
if(k < count(l)) {
auto pp = split(l, k);
return {pp.first, merge(pp.second, r)};
}
if(k > count(l)) {
auto pp = split(r, k - count(l));
return {merge(l, pp.first), pp.second};
}
return {l, r};
}
tuple< Node *, Node *, Node * > split3(Node *t, int a, int b) {
auto x = split(t, a);
auto y = split(x.second, b - a);
return make_tuple(x.first, y.first, y.second);
}
template< typename ... Args >
Node *merge(Node *l, Args ...rest) {
Node *r = merge(rest...);
if(!l || !r) return l ? l : r;
return submerge(l, r);
}
Node *build(const vector< Monoid > &v) {
return build(0, (int) v.size(), v);
}
vector< Monoid > dump(Node *r) {
vector< Monoid > v((size_t) count(r));
auto it = begin(v);
dump(r, it, OM0);
return v;
}
string to_string(Node *r) {
auto s = dump(r);
string ret;
for(int i = 0; i < s.size(); i++) {
ret += std::to_string(s[i]);
ret += ", ";
}
return ret;
}
void insert(Node *&t, int k, const Monoid &v) {
auto x = split(t, k);
t = merge(merge(x.first, alloc(v)), x.second);
}
Monoid erase(Node *&t, int k) {
auto x = split(t, k);
auto y = split(x.second, 1);
auto v = y.first->c;
pool.free(y.first);
t = merge(x.first, y.second);
return v;
}
Monoid query(Node *&t, int a, int b) {
auto x = split(t, a);
auto y = split(x.second, b - a);
Monoid ret = sum(y.first);
t = merge(x.first, y.first, y.second);
return ret;
}
void set_propagate(Node *&t, int a, int b, const OperatorMonoid &pp) {
auto x = split(t, a);
auto y = split(x.second, b - a);
y.first->lazy = h(y.first->lazy, pp);
t = merge(x.first, propagate(y.first), y.second);
}
void set_element(Node *&t, int k, const Monoid &x) {
t = propagate(t);
if(t->is_leaf()) {
t->key = t->sum = x;
return;
}
if(k < count(t->l)) set_element(t->l, k, x);
else set_element(t->r, k - count(t->l), x);
t = update(t);
}
void push_front(Node *&t, const Monoid &v) {
t = merge(alloc(v), t);
}
void push_back(Node *&t, const Monoid &v) {
t = merge(t, alloc(v));
}
Monoid pop_front(Node *&t) {
auto ret = split(t, 1);
t = ret.second;
return ret.first->key;
}
Monoid pop_back(Node *&t) {
auto ret = split(t, count(t) - 1);
t = ret.first;
return ret.second->key;
}
};
#line 10 "test/verify/yosupo-range-affine-range-sum-3.test.cpp"
using mint = modint998244353;
int main() {
int N, Q;
cin >> N >> Q;
using pi = pair< mint, int >;
using qi = pair< mint, mint >;
auto f = [](const pi &a, const pi &b) -> pi {
return {a.first + b.first, a.second + b.second};
};
auto g = [](const pi &a, const qi &b) -> pi {
return {a.first * b.first + mint(a.second) * b.second, a.second};
};
auto h = [](const qi &a, const qi &b) -> qi {
return {a.first * b.first, a.second * b.first + b.second};
};
LazyWeightBalancedTree< pi, qi, decltype(f), decltype(g), decltype(h) > rbt(2 * N, f, g, h, pi(0, 0), pi(1, 0));
vector< pi > A(N);
for(int i = 0; i < N; i++) {
mint a;
cin >> a;
A[i] = {a, 1};
}
auto root = rbt.build(A);
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;
rbt.set_propagate(root, l, r, qi(b, c));
} else {
int l, r;
cin >> l >> r;
cout << rbt.query(root, l, r).first << "\n";
}
}
}
Env | Name | Status | Elapsed | Memory |
---|---|---|---|---|
g++ | example_00 | AC | 6 ms | 4 MB |
g++ | max_random_00 | AC | 1953 ms | 62 MB |
g++ | max_random_01 | AC | 1980 ms | 62 MB |
g++ | max_random_02 | AC | 1991 ms | 62 MB |
g++ | random_00 | AC | 1406 ms | 49 MB |
g++ | random_01 | AC | 1458 ms | 58 MB |
g++ | random_02 | AC | 788 ms | 10 MB |
g++ | small_00 | AC | 7 ms | 4 MB |
g++ | small_01 | AC | 6 ms | 4 MB |
g++ | small_02 | AC | 6 ms | 4 MB |
g++ | small_03 | AC | 6 ms | 4 MB |
g++ | small_04 | AC | 6 ms | 4 MB |
g++ | small_05 | AC | 6 ms | 4 MB |
g++ | small_06 | AC | 6 ms | 4 MB |
g++ | small_07 | AC | 7 ms | 4 MB |
g++ | small_08 | AC | 6 ms | 4 MB |
g++ | small_09 | AC | 6 ms | 4 MB |
g++ | small_random_00 | AC | 7 ms | 4 MB |
g++ | small_random_01 | AC | 7 ms | 4 MB |
clang++ | example_00 | AC | 6 ms | 4 MB |
clang++ | max_random_00 | AC | 2011 ms | 62 MB |
clang++ | max_random_01 | AC | 2061 ms | 62 MB |
clang++ | max_random_02 | AC | 2034 ms | 62 MB |
clang++ | random_00 | AC | 1530 ms | 49 MB |
clang++ | random_01 | AC | 1597 ms | 58 MB |
clang++ | random_02 | AC | 850 ms | 10 MB |
clang++ | small_00 | AC | 7 ms | 4 MB |
clang++ | small_01 | AC | 6 ms | 4 MB |
clang++ | small_02 | AC | 7 ms | 4 MB |
clang++ | small_03 | AC | 6 ms | 4 MB |
clang++ | small_04 | AC | 6 ms | 4 MB |
clang++ | small_05 | AC | 6 ms | 4 MB |
clang++ | small_06 | AC | 6 ms | 4 MB |
clang++ | small_07 | AC | 6 ms | 4 MB |
clang++ | small_08 | AC | 6 ms | 4 MB |
clang++ | small_09 | AC | 7 ms | 4 MB |
clang++ | small_random_00 | AC | 8 ms | 4 MB |
clang++ | small_random_01 | AC | 7 ms | 4 MB |