Codebook

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

View the Project on GitHub crackersamdjam/Codebook

:heavy_check_mark: tests/segment_tree.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/point_set_range_composite"
#include "../content/data_structures/segment_tree.h"

using p = pair<ll, ll>;
constexpr ll mod = 998244353;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin.exceptions(cin.failbit);
	
	int n, q;
	cin>>n>>q;
	
	segment_tree<p, p> ST;
	ST.init(n+5, {1, 0},
		[](auto x, auto y){ return p(x.first*y.first % mod, (y.first*x.second + y.second) % mod); },
		[](auto x, auto y){ return y; });
	
	for(int i = 0; i < n; i++){
		ll a, b;
		cin>>a>>b;
		ST.update(i, {a, b});
	}
	while(q--){
		int op;
		cin>>op;
		if(op == 0){
			int i; ll a, b;
			cin>>i>>a>>b;
			ST.update(i, {a, b});
		}
		else{
			int l, r; ll x;
			cin>>l>>r>>x;
			auto [a, b] = ST.query(l, r-1);
			cout<<(a*x+b)%mod<<'\n';
		}
	}
}
#line 1 "tests/segment_tree.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/point_set_range_composite"
#line 1 "content/utils/template.h"
/**
 * @brief My starter code
 */

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define makeunique(x) (x).erase(unique((x).begin(), (x).end()), (x).end());

#ifdef LOCAL
template<typename T> void pr(T a){std::cerr<<a<<std::endl;}
template<typename T, typename... Args> void pr(T a, Args... args){std::cerr<<a<<' ',pr(args...);}
#else
template<typename... Args> void pr(Args... args){}
#endif

using namespace std;
using ll = long long;
using pii = pair<int, int>;
#line 3 "content/data_structures/segment_tree.h"

/**
 * @brief Segment Tree
 */

template<class T, class L> struct segment_tree{
	int SZ;
	vector<T> val;
	function<T(T, T)> merge;
	function<T(T, L)> apply;
	
	void init(int n, T def, function<T(T, T)> f, function<T(T, L)> g){
		SZ = n;
		while(n&(n-1)) n++; // next largest power of 2
		val.resize(2*n, def);
		merge = f;
		apply = g;
	}

	#define lc (rt<<1)
	#define rc (rt<<1|1)
	#define nm ((nl+nr)/2)
	
	T _query(int l, int r, int nl, int nr, int rt){
		if(r < nl or nr < l) return val[0];
		if(l <= nl and nr <= r) return val[rt];
		return merge(_query(l, r, nl, nm, lc), _query(l, r, nm+1, nr, rc));
	}
	T query(int l, int r){ return _query(l, r, 0, SZ-1, 1); }
	
	void _update(int i, L x, int nl, int nr, int rt){
		if(nl == nr){
			val[rt] = apply(val[rt], x);
			return;
		}
		i <= nm ? _update(i, x, nl, nm, lc) : _update(i, x, nm+1, nr, rc);
		val[rt] = merge(val[lc], val[rc]);
	}
	void update(int i, L x){ _update(i, x, 0, SZ-1, 1); };

	#undef lc
	#undef rc
	#undef nm
};
#line 3 "tests/segment_tree.test.cpp"

using p = pair<ll, ll>;
constexpr ll mod = 998244353;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin.exceptions(cin.failbit);
	
	int n, q;
	cin>>n>>q;
	
	segment_tree<p, p> ST;
	ST.init(n+5, {1, 0},
		[](auto x, auto y){ return p(x.first*y.first % mod, (y.first*x.second + y.second) % mod); },
		[](auto x, auto y){ return y; });
	
	for(int i = 0; i < n; i++){
		ll a, b;
		cin>>a>>b;
		ST.update(i, {a, b});
	}
	while(q--){
		int op;
		cin>>op;
		if(op == 0){
			int i; ll a, b;
			cin>>i>>a>>b;
			ST.update(i, {a, b});
		}
		else{
			int l, r; ll x;
			cin>>l>>r>>x;
			auto [a, b] = ST.query(l, r-1);
			cout<<(a*x+b)%mod<<'\n';
		}
	}
}
Back to top page