蒟蒻萌新线段树写挂,有没有大佬能帮忙QAQ
查看原帖
蒟蒻萌新线段树写挂,有没有大佬能帮忙QAQ
561621
NoMoreLove楼主2023/10/9 23:07
#include<bits/stdc++.h>
using namespace std;
int n,m,op,l,r,k,q,a[100005];
struct node{
	int data,mulazy,addlazy;
}tree[400005];
void build(int l,int r,int p){
	tree[p].mulazy=1;
	if(l==r){
		tree[p].data=a[l];
		return;
	}
	int mid=l+(r-l>>1);
	build(l,mid,p<<1);
	build(mid+1,r,p<<1|1);
	tree[p].data=(tree[p<<1].data+tree[p<<1|1].data)%m;
}
void pushdown(int p,int l,int r,int mid){
	tree[p<<1].data=(tree[p<<1].data*tree[p].mulazy%m+(mid-l+1)*tree[p].addlazy%m)%m;
	tree[p<<1|1].data=(tree[p<<1|1].data*tree[p].mulazy%m+(r-mid)*tree[p].addlazy%m)%m;
	tree[p<<1].mulazy=(tree[p<<1].mulazy*tree[p].mulazy)%m;
	tree[p<<1|1].mulazy=(tree[p<<1|1].mulazy*tree[p].mulazy)%m;
	tree[p<<1].addlazy+=tree[p].addlazy,tree[p<<1|1].addlazy+=tree[p].addlazy;
	tree[p].mulazy=1,tree[p].addlazy=0;
}
void updateadd(int l,int r,int s,int c,int t,int p){
	if(l<=s && c<=r){
		tree[p].data=(tree[p].data+(c-s+1)*t)%m,tree[p].addlazy+=t;
		return;
	}
	int mid=s+(c-s>>1);
	pushdown(p,s,c,mid);
	if(l<=mid) updateadd(l,r,s,mid,t,p<<1);
	if(r>mid) updateadd(l,r,mid+1,c,t,p<<1|1);
	tree[p].data=(tree[p<<1].data+tree[p<<1|1].data)%m;
}
void updatemul(int l,int r,int s,int c,int t,int p){
	if(l<=s && c<=r){
		tree[p].data=tree[p].data*t%m,
		tree[p].mulazy=tree[p].mulazy*t%m,
		tree[p].addlazy=tree[p].addlazy*t%m;
		return;
	}
	int mid=s+(c-s>>1);
	pushdown(p,s,c,mid);
	if(l<=mid) updatemul(l,r,s,mid,t,p<<1);
	if(r>mid) updatemul(l,r,mid+1,c,t,p<<1|1);
	tree[p].data=(tree[p<<1].data+tree[p<<1|1].data)%m;
}
int query(int l,int r,int s,int c,int p){
	if(l<=s && c<=r) return tree[p].data%m;
	int mid=s+(c-s>>1);
	pushdown(p,s,c,mid);
	int sum=0;
	if(l<=mid) sum+=query(l,r,s,mid,p<<1);
	if(r>mid) sum+=query(l,r,mid+1,c,p<<1|1);
	return sum%m;
}
int main(){
	cin>>n>>q>>m;
	for(int i=1;i<=n;i++) cin>>a[i],a[i]%=m;
	build(1,n,1);
	while(q--){
		cin>>op>>l>>r;
		if(op==1) cin>>k,updatemul(l,r,1,n,k%m,1);
		if(op==2) cin>>k,updateadd(l,r,1,n,k%m,1);
		if(op==3) cout<<query(l,r,1,n,1)<<"\n";
	}
	return 0;
}
2023/10/9 23:07
加载中...