跳到正文

个人记录

7891

gaotengyue约 4 分钟
原文
文章信息与版本

文章信息

发布
2026年4月25日
收录
2026年4月25日
更新
2026年4月25日
篇幅
约 4 分钟

版本

  1. v1当前

https://www.cnblogs.com/heffo-hard/articles/19825847

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int n,m,t,a[N],tree[N<<2],tag[N<<2];
int ls(int p){
	return p<<1;
}
int rs(int p){
	return p<<1|1;
}
void push_up(int p){
	tree[p]=(tree[ls(p)]+tree[rs(p)])%t;
}
void build(int p,int pl,int pr){
	tag[p]=0;
	if(pl==pr){
		tree[p]=a[pl]%t;
		return;
	}
	int mid=(pl+pr)>>1;
	build(ls(p),pl,mid);
	build(rs(p),mid+1,pr);
	push_up(p);
}
void addtag(int p,int pl,int pr,int d){
	tag[p]=(tag[p]+d)%t;
	tree[p]=(tree[p]+d*(pr-pl+1)%t)%t;
}
void timestag(int p,int pl,int pr,int d){
	tag[p]=tag[p]*d%t;
	tree[p]=(tree[p]+d*(pr-pl+1)%t)%t;
}
void push_down(int p,int pl,int pr){
	if(tag[p]){
		int mid=(pl+pr)>>1;
		addtag(ls(p),pl,mid,tag[p]);
		addtag(rs(p),mid+1,pr,tag[p]);
		tag[p]=0;
	}
}
void update1(int L,int R,int p,int pl,int pr,int d){
	if(L<=pl && pr<=R){
		timestag(p,pl,pr,d);
		return;
	}
	push_down(p,pl,pr);
	int mid=(pl+pr)>>1;
	if(L<=mid){
		update1(L,R,ls(p),pl,mid,d);
	}
	if(R>mid){
		update1(L,R,rs(p),mid+1,pr,d);
	}
	push_up(p);
}
void update2(int L,int R,int p,int pl,int pr,int d){
	if(L<=pl && pr<=R){
		addtag(p,pl,pr,d);
		return;
	}
	push_down(p,pl,pr);
	int mid=(pl+pr)>>1;
	if(L<=mid){
		update2(L,R,ls(p),pl,mid,d);
	}
	if(R>mid){
		update2(L,R,rs(p),mid+1,pr,d);
	}
	push_up(p);
}
int query(int L,int R,int p,int pl,int pr){
	if(pl>=L && R>=pr){
		return tree[p]%m;
	}
	push_down(p,pl,pr);
	int res=0,mid=(pl+pr)>>1;
	if(L<=mid){
		res+=query(L,R,ls(p),pl,mid);
	}
	if(R>mid){
		res+=query(L,R,rs(p),mid+1,pr);
	}
	return res;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>t;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		a[i]%=t;
	}
	build(1,1,n);
	while(m--){
		int q,L,R,d;
		cin>>q;
		if(q==1){
			cin>>L>>R>>d;
			update1(L,R,1,1,n,d);
		}
		else if(q==2){
			cin>>L>>R>>d;
			update2(L,R,1,1,n,d);
		}
		else{
			cin>>L>>R;
			cout<<query(L,R,1,1,n)<<"\n";
		}
	}
	return 0;
}
原文