全RE求助
查看原帖
全RE求助
917683
rnf5114楼主2023/12/19 21:53

数据本地输出正确,洛谷提交Re

#include <iostream>
using namespace std;
#define int long long
int m,n,q,a[400010],t1[400010],t2[400010],yuan[400010],qwq[400010],awa[400010];
void apply(int p,int l,int r,int k1,int k2){
	a[p]*=k1;
	a[p]%=m;
	a[p]+=(r-l+1)*k2;
	a[p]%=m;
	t1[p]*=k1;
	t1[p]%=m;
	t2[p]*=k1;
	t2[p]%=m;
	t2[p]+=k2;
	t2[p]%=m;
	return ;
}
void pushdown(int p,int l,int r){
	apply(2*p,l,(l+r)/2,t1[p],t2[p]);
	apply(2*p+1,(l+r)/2+1,r,t1[p],t2[p]);
	t1[p]=1;
	t2[p]=0;
	return ;
}
void pushup(int p){
	a[p]=(a[p*2]+a[p*2+1])%m;
	return ;
}
int build(int l,int r,int p){
	qwq[p]=l;
	awa[p]=r;
	if(l==r){
		a[p]=yuan[l]%m;
		return a[p];
	}
	build(l,(l+r)/2,p*2);
	build((l+r)/2+1,r,p*2+1);
	pushup(p);
}
void jia(int p,int l,int r,int ql,int qr,int k){
	//cout<<"左边界="<<l<<" 右边界="<<r<<endl;
	if(l>qr||r<ql)
		return;
	if(ql<=l&&qr>=r){
		//cout<<1<<endl;
		apply(p,l,r,1,k);
		return ;
	}
	pushdown(p,l,r);
	jia(2*p,l,(l+r)/2,ql,qr,k);
	jia(2*p+1,(l+r)/2+1,r,ql,qr,k);
	pushup(p);
	return ;
}
void cheng(int p,int l,int r,int ql,int qr,int k){
	//cout<<"左边界="<<l<<" 右边界="<<r<<endl;
	if(l>qr||r<ql)
		return;
	if(ql<=l&&qr>=r){
		apply(p,l,r,k,0);
		return ;
	}
	pushdown(p,l,r);
	cheng(2*p,l,(l+r)/2,ql,qr,k);
	cheng(2*p+1,(l+r)/2+1,r,ql,qr,k);
	pushup(p);
	return ;
}
int an(int p,int l,int r,int ql,int qr){
	if(l>qr||r<ql)
		return 0;
	if(l>=ql&&r<=qr){
		//cout<<"节点编号为"<<p<<" 左边界为"<<l<<" 右边界为"<<r<<" 值为"<<a[p]<<endl;
		return a[p];
	}
	pushdown(p,l,r);
	int tmp1=(an(2*p,l,(l+r)/2,ql,qr))%m,tmp2=(an(2*p+1,(l+r)/2+1,r,ql,qr))%m;
	//cout<<"节点编号为"<<p<<" 左边界为"<<l<<" 右边界为"<<r<<" 左子树值为"<<tmp1<<" 右子树值为"<<tmp2<<endl;
	return (tmp1+tmp2)%m;
}
signed main(){
	cin>>n>>q>>m;
	for(int i=1;i<=n;i++)
		cin>>yuan[i];
	for(int i=0;i<=400005;i++)
		t1[i]=1;
	build(1,n,1);
	while(q--){
		int op,x,y,zhi;
		cin>>op;
		if(op==2){
			cin>>x>>y>>zhi;
			jia(1,1,n,x,y,zhi);
		}
		else if(op==1){
			cin>>x>>y>>zhi;
			cheng(1,1,n,x,y,zhi);
		}
		else{
			cin>>x>>y;
			cout<<an(1,1,n,x,y)<<endl;
		}
		/*cout<<"!!!!!!";
		for(int i=1;i<=20;i++)
			cout<<"左边界="<<qwq[i]<<" 右边界="<<awa[i]<<" 和为"<<a[i]<<" t1[i]="<<t1[i]<<" t2[i]=="<<t2[i]<<endl;
		cout<<endl;*/
	}
    return 0;
}


/*
       ┏┓   ┏┓
     ┏┛┻━━━┛┻┓
     ┃       ┃
     ┃   ━   ┃
     ┃ ┳┛ ┗┳ ┃
     ┃       ┃
     ┃   ┻   ┃
     ┃       ┃
     ┗━┓   ┏━┛Codes are far away from bugs with the animal protecting
         ┃   ┃    神兽保佑,代码无bug
         ┃   ┃
         ┃   ┗━━━┓
         ┃      ┣┓
         ┃     ┏┛
         ┗┓┓┏━┳┓┏┛
           ┃┫┫ ┃┫┫
           ┗┻┛ ┗┻┛     ○| ̄|_

*/

2023/12/19 21:53
加载中...