样例不过,求助
查看原帖
样例不过,求助
835809
T_TLucas_Yin楼主2023/12/30 17:26
#include<bits/stdc++.h>
using namespace std;
int n,m,M,flag,x,y;
long long a[1000005],k;
struct node{
	int l,r;
	long long sum,flag1,flag2;
}f[1000005];
void build(int root,int l,int r){
	f[root].l=l,f[root].r=r,f[root].flag1=1;
	if(l==r){
		f[root].sum=a[l]%M;
		return;
	}
	int mid=(l+r)>>1;
	build(root<<1,l,mid);
	build(root<<1|1,mid+1,r);
	f[root].sum=(f[root<<1].sum+f[root<<1|1].sum)%M;
}
void pushdown(int root){
	f[root<<1].sum=(f[root<<1].sum*f[root].flag1+f[root].flag2*(f[root<<1].r-f[root<<1].l+1))%M;
	f[root<<1].flag1=f[root<<1].flag1*f[root].flag1%M,
	f[root<<1].flag2=(f[root<<1].flag2*f[root].flag1+f[root].flag2)%M;
	f[root<<1|1].sum=(f[root<<1|1].sum*f[root].flag1+f[root].flag2*(f[root<<1|1].r-f[root<<1|1].l+1))%M;
	f[root<<1|1].flag1=(f[root<<1|1].flag1*f[root].flag1)%M,
	f[root<<1|1].flag2=(f[root<<1|1].flag2*f[root].flag1+f[root].flag2)%M;
	f[root].flag1=1,f[root].flag2=0;
}
void update1(int root,int l,int r,int k){
	if(l<=f[root].l&&f[root].r<=r){
		f[root].sum=f[root].sum*k%M,
		f[root].flag1=f[root].flag1*k%M,
		f[root].flag2=f[root].flag2*k%M;
		return;
	}
	pushdown(root);
	int mid=(f[root].l+f[root].r)>>1;
	if(l<=mid) update1(root<<1,l,r,k);
	if(r>mid) update1(root<<1|1,l,r,k);
	f[root].sum=(f[root<<1].sum+f[root<<1|1].sum)%M;
}
void update2(int root,int l,int r,int k){
	if(l<=f[root].l&&f[root].r<=r){
		f[root].sum=(f[root].sum+k*(f[root].r-f[root].l+1))%M;
		f[root].flag1=(f[root].flag1+k)%M;
		return;
	}
	pushdown(root);
	int mid=(f[root].l+f[root].r)>>1;
	if(l<=mid) update2(root<<1,l,r,k);
	if(r>mid) update2(root<<1|1,l,r,k);
	f[root].sum=(f[root<<1].sum+f[root<<1|1].sum)%M;
}
long long getsum(int root,int l,int r){
	if(l<=f[root].l&&f[root].r<=r) return f[root].sum;
	pushdown(root);
	int mid=(f[root].l+f[root].r)>>1;
	long long sum=0;
	if(l<=mid) sum=getsum(root<<1,l,r);
	if(r>mid) sum+=getsum(root<<1|1,l,r);
	return sum%M;
}
int main(){
	scanf("%d %d %d",&n,&m,&M);
	for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
	build(1,1,n);
	for(int i=1;i<=m;i++){
		scanf("%d",&flag);
		if(flag==1){
			scanf("%d %d %lld",&x,&y,&k);
			update1(1,x,y,k);
		}
		else if(flag==2){
			scanf("%d %d %lld",&x,&y,&k);
			update2(1,x,y,k);
		}
		else{
			scanf("%d %d",&x,&y);
			printf("%lld\n",getsum(1,x,y));
		}
	}
	return 0;
}
2023/12/30 17:26
加载中...