cp-library

競技プログラミング用 C++ ライブラリ

View the Project on GitHub qandori/cp-library

:heavy_check_mark: test/dynamic_lazy_segtree.test.cpp

test/dynamic_lazy_segtree.test.cpp · GitHub で見る

検証問題

依存ライブラリ

コード

#define PROBLEM "https://judge.yosupo.jp/problem/range_affine_range_sum"
#include "../data_structure/dynamic_lazy_segtree.hpp"
#include "../components/fast_io.hpp"
#include "../math/mint.hpp"

struct Sum { mint sum, length; };
struct Affine { mint a, b; };
Sum op(Sum a, Sum b) { return {a.sum + b.sum, a.length + b.length}; }
Sum e() { return {0, 0}; }
Sum mapping(Affine f, Sum x) { return {f.a * x.sum + f.b * x.length, x.length}; }
Affine composition(Affine f, Affine g) { return {f.a * g.a, f.a * g.b + f.b}; }
Affine id() { return {1, 0}; }
Sum initial(std::int64_t l, std::int64_t r) { return {0, r - l}; }

int main() {
	FastInput in;
	FastOutput out;
	int n = 0, q = 0;
	in.read(n); in.read(q);
	std::vector<Sum> a(n);
	for (auto &x : a) { unsigned v = 0; in.read(v); x = {mint::raw(v), 1}; }
	DynamicLazySegtree<Sum, op, e, Affine, mapping, composition, id, initial> seg(a);
	while (q--) {
		int type = 0, l = 0, r = 0;
		in.read(type); in.read(l); in.read(r);
		if (type == 0) {
			unsigned b = 0, c = 0;
			in.read(b); in.read(c);
			seg.apply(l, r, Affine{mint::raw(b), mint::raw(c)});
		} else out.println(seg.prod(l, r).sum.val());
	}
}
Back to top page