求调P3373
查看原帖
求调P3373
921450
Szy0720楼主2024/5/27 19:41
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
int n,m,q;
struct node{
	ll la=1,laz;
	ll sum;
}t[4*N];
void pud(int p,int l,int r){
	int mid=l+r>>1;
	if(t[p].la!=1){
		t[p<<1].sum=t[p<<1].sum*t[p].la%m;
		t[p<<1|1].sum=t[p<<1|1].sum*t[p].la%m;
		t[p<<1].laz=t[p<<1].laz*t[p].la%m;
		t[p<<1|1].laz=t[p<<1|1].laz*t[p].la%m;
		t[p<<1].la=t[p<<1].la*t[p].la%m;
		t[p<<1|1].la=t[p<<1|1].la*t[p].la%m;
		t[p].la=1;
	}
	if(t[p].laz!=0){
		t[p<<1].sum=(t[p<<1].sum+(mid-l+1)*t[p].laz)%m;
		t[p<<1|1].sum=(t[p<<1|1].sum+(r-mid)*t[p].laz)%m;
		t[p<<1].laz=(t[p<<1].laz+t[p].laz)%m;
		t[p<<1|1].laz=(t[p<<1|1].laz+t[p].laz)%m;
		t[p].laz=0;
	}
}
void pus(int p){
	t[p].sum=(t[p<<1].sum+t[p<<1|1].sum)%m;
}
void bb(int p,int l,int r,int L,int R,int k){
	if(L<=l&&r<=R){
		t[p].sum=t[p].sum*k%m;
		t[p].la=t[p].la*k%m;
		t[p].laz=t[p].laz*k%m;
		return;
	}
	pud(p,l,r);
	int mid=l+r>>1;
	if(L<=mid)bb(p*2,l,mid,L,R,k);
	if(r>mid)bb(p*2+1,mid+1,r,L,R,k);
	pus(p);
}
void aa(int p,int l,int r,int L,int R,ll d){
	if(L<=l&&r<=R){
		t[p].sum=(t[p].sum+(r-l+1)*d)%m;
		t[p].laz=(t[p].laz+d)%m;
		return ;
	}
	pud(p,l,r);
	int mid=l+r>>1;
	if(L<=mid)aa(p<<1,l,mid,L,R,d);
	if(mid<r)aa(p<<1|1,mid+1,r,L,R,d);
	pus(p);
}
ll cc(int p,int l,int r,int L,int R){
	if(R<l||r<L)return 0;
	if(L<=l&&r<=R)return t[p].sum;
	pud(p,l,r);
	int mid=l+r>>1;
	return (cc(p<<1,l,mid,L,R)+cc(p<<1|1,mid+1,r,L,R))%m;
}
int main(){
	cin>>n>>q>>m;
	for(int i=1;i<=n;i++){
		int a;
		cin>>a;
		aa(1,1,n,i,i,a);
	}
	while(q--){
		int o,a,b,c,d;
		cin>>o>>a>>b;
		if(o==1){
			cin>>c>>d;
			bb(1,1,n,a,b,c);
		}
		else if(o==2){
			cin>>c;
			aa(1,1,n,a,b,c);
		}
		else{
			cout<<cc(1,1,n,a,b);
		}
	}
	return 0;
}
2024/5/27 19:41
加载中...