李煜东写法30pts求调
查看原帖
李煜东写法30pts求调
1305692
xiangixuan楼主2025/1/18 15:42
#include<bits/stdc++.h>
#define int long long
#define N 100001
using namespace std;
int n, m, a[N], opt, x, y, k, P;
struct seg{
	int v, l, r, tag_add, tag_tim;
	#define l(x) t[x].l
	#define r(x) t[x].r
	#define v(x) t[x].v
	#define tag_a(x) t[x].tag_add
	#define tag_t(x) t[x].tag_tim
} t[4*N];
int ls(int x) {return x<<1; }
int rs(int x) {return (x<<1)+1; }
int mid(int x) {return (l(x)+r(x))>>1; }
void build(int p, int l, int r) {
	l(p)=l, r(p)=r, tag_t(p)=1;
	if (l==r) {v(p)=a[l]%P; return; }
	build(ls(p), l, mid(p));
	build(rs(p), mid(p)+1, r);
	v(p)=(v(ls(p))+v(rs(p)))%P;
}
void spread(int p) {
	if (tag_a(p)==0 && tag_t(p)==1) return;
	v(ls(p))=(tag_t(p)*v(ls(p))%P+tag_a(p)*(r(ls(p))-l(ls(p))+1)%P)%P;
	v(rs(p))=(tag_t(p)*v(rs(p))%P+tag_a(p)*(r(rs(p))-l(rs(p))+1)%P)%P;
		
	tag_t(ls(p))=tag_t(ls(p))*tag_t(p)%P;
	tag_t(rs(p))=tag_t(rs(p))*tag_t(p)%P;
	
	tag_a(ls(p))=(tag_a(ls(p))+tag_a(p))%P;
	tag_a(rs(p))=(tag_a(rs(p))+tag_a(p))%P;
	
	tag_t(p)=1; tag_a(p)=0;
}
void add(int p, int l, int r, int v) {
	if (l<=l(p) && r>=r(p)) {
		tag_a(p)=(tag_a(p)+v)%P;
		v(p)=(v(p)+v*(r(p)-l(p)+1)%P)%P;
		return;
	}
	spread(p);
	if (l<=mid(p)) add(ls(p), l, r, v);
	if (r>mid(p)) add(rs(p), l, r, v);
	v(p)=(v(ls(p))+v(rs(p)))%P;
}
void tim(int p, int l, int r, int v) {
	if (l<=l(p) && r>=r(p)) {
		tag_a(p)=tag_a(p)*v%P;
		tag_t(p)=tag_t(p)*v%P;
		v(p)=v(p)*v%P; return;
	}
	spread(p);
	if (l<=mid(p)) tim(ls(p), l, r, v);
	if (r>mid(p)) tim(rs(p), l, r, v);
	v(p)=(v(ls(p))+v(rs(p)))%P;
}
int query(int p, int l, int r) {
	if (l<=l(p) && r>=r(p)) return v(p);
	spread(p);
	int v=0;
	if (l<=mid(p)) v=(v+query(ls(p), l, r))%P;
	if (r>mid(p)) v=(v+query(rs(p), l, r))%P;
	return v;
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> m >> P;
	for (int i=1; i<=n; ++i) cin >> a[i];
	build(1, 1, n);
	while (m--) {
		cin >> opt;
		if (opt==1) {
			cin >> x >> y >> k;
			tim(1, x, y, k);
		} else if (opt==2){
			cin >> x >> y >> k;
			add(1, x, y, k);
		} else {
			cin >> x >> y;
			cout << query(1, x, y) << '\n';
		}
	}
	return 0;
}
2025/1/18 15:42
加载中...