不懂就问
查看原帖
不懂就问
705623
wantongxue楼主2024/7/28 23:29

rt

100pts代码

#include<bits/stdc++.h>
#define int long long
using namespace std;

int n,q,m;

struct node{
	int l,r,sum,tag,mul;
}tr[100010*8];

void build(int l,int r,int now){
	tr[now].l=l;tr[now].r=r;tr[now].mul=1;//就这有区别
	if(l==r){
		cin>>tr[now].sum;
		return;
	}
	int mid=l+r>>1;
	build(l,mid,now<<1);
	build(mid+1,r,now<<1|1);
	tr[now].sum=tr[now<<1].sum+tr[now<<1|1].sum;
	tr[now].sum%=m;
}

void pushdown(int now){
	tr[now<<1].sum=((tr[now<<1].sum*tr[now].mul%m)+tr[now].tag*(tr[now<<1].r-tr[now<<1].l+1))%m;
	tr[now<<1|1].sum=((tr[now<<1|1].sum*tr[now].mul%m)+tr[now].tag*(tr[now<<1|1].r-tr[now<<1|1].l+1))%m;
	tr[now<<1].mul=tr[now<<1].mul*tr[now].mul%m;
	tr[now<<1|1].mul=tr[now<<1|1].mul*tr[now].mul%m;
	tr[now<<1].tag=(tr[now<<1].tag*tr[now].mul+tr[now].tag)%m;
	tr[now<<1|1].tag=(tr[now<<1|1].tag*tr[now].mul+tr[now].tag)%m;
	tr[now].tag=0;
	tr[now].mul=1;
}

void modify_1(int l,int r,int k,int now){
	if(tr[now].l>=l&&tr[now].r<=r){
		tr[now].sum=tr[now].sum*k%m;
		tr[now].mul=tr[now].mul*k%m;
		tr[now].tag=tr[now].tag*k%m;
		return;
	}
	pushdown(now);
	int mid=tr[now].l+tr[now].r>>1;
	if(l<=mid) modify_1(l,r,k,now<<1);
	if(r>mid) modify_1(l,r,k,now<<1|1);
	tr[now].sum=tr[now<<1].sum+tr[now<<1|1].sum;
	tr[now].sum%=m;
}

void modify_2(int l,int r,int k,int now){
	if(tr[now].l>=l&&tr[now].r<=r){
		tr[now].sum=(tr[now].sum+k*(tr[now].r-tr[now].l+1))%m;
		tr[now].tag=(tr[now].tag+k)%m;
		return;
	}
	pushdown(now);
	int mid=tr[now].l+tr[now].r>>1;
	if(l<=mid) modify_2(l,r,k,now<<1);
	if(r>mid) modify_2(l,r,k,now<<1|1);
	tr[now].sum=tr[now<<1].sum+tr[now<<1|1].sum;
	tr[now].sum%=m;
}

int query(int l,int r,int now){
	pushdown(now);
	if(tr[now].l>=l&&tr[now].r<=r) return tr[now].sum;
	int mid=tr[now].l+tr[now].r>>1,ans=0;
	if(l<=mid) ans+=query(l,r,now<<1);
	if(r>mid) ans+=query(l,r,now<<1|1);
	ans%=m;
	return ans;
}

signed main(){
//	freopen("2.in","r",stdin);
//	freopen("2.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>q>>m;
	build(1,n,1);
	int op,x,y,k;
	while(q--){
		cin>>op>>x>>y;
		if(op==1){
			cin>>k;
			modify_1(x,y,k,1);
		}else if(op==2){
			cin>>k;
			modify_2(x,y,k,1);
		}else{
			cout<<query(x,y,1)%m<<endl;
		}
	}
	return 0;
}

30pts


#include<bits/stdc++.h>
#define int long long
using namespace std;

int n,q,m;

struct node{
	int l,r,sum,tag,mul;
}tr[100010*8];

void build(int l,int r,int now){
	tr[now].l=l;tr[now].r=r;
	if(l==r){
		cin>>tr[now].sum;
		return;
	}
	int mid=l+r>>1;
	build(l,mid,now<<1);
	build(mid+1,r,now<<1|1);
	tr[now].sum=tr[now<<1].sum+tr[now<<1|1].sum;
	tr[now].sum%=m;
}

void pushdown(int now){
	if(tr[now].mul==0) tr[now].mul=1;//就这有区别
	tr[now<<1].sum=((tr[now<<1].sum*tr[now].mul%m)+tr[now].tag*(tr[now<<1].r-tr[now<<1].l+1))%m;
	tr[now<<1|1].sum=((tr[now<<1|1].sum*tr[now].mul%m)+tr[now].tag*(tr[now<<1|1].r-tr[now<<1|1].l+1))%m;
	if(tr[now<<1].mul==0) tr[now<<1].mul=1;//就这有区别
	if(tr[now<<1|1].mul==0) tr[now<<1|1].mul=1;//就这有区别
	tr[now<<1].mul=tr[now<<1].mul*tr[now].mul%m;
	tr[now<<1|1].mul=tr[now<<1|1].mul*tr[now].mul%m;
	tr[now<<1].tag=(tr[now<<1].tag*tr[now].mul+tr[now].tag)%m;
	tr[now<<1|1].tag=(tr[now<<1|1].tag*tr[now].mul+tr[now].tag)%m;
	tr[now].tag=0;
	tr[now].mul=1;
}

void modify_1(int l,int r,int k,int now){
	if(tr[now].l>=l&&tr[now].r<=r){
		tr[now].sum=tr[now].sum*k%m;
		if(tr[now].mul==0) tr[now].mul=1;//就这有区别
		tr[now].mul=tr[now].mul*k%m;
		tr[now].tag=tr[now].tag*k%m;
		return;
	}
	pushdown(now);
	int mid=tr[now].l+tr[now].r>>1;
	if(l<=mid) modify_1(l,r,k,now<<1);
	if(r>mid) modify_1(l,r,k,now<<1|1);
	tr[now].sum=tr[now<<1].sum+tr[now<<1|1].sum;
	tr[now].sum%=m;
}

void modify_2(int l,int r,int k,int now){
	if(tr[now].l>=l&&tr[now].r<=r){
		tr[now].sum=(tr[now].sum+k*(tr[now].r-tr[now].l+1))%m;
		tr[now].tag=(tr[now].tag+k)%m;
		return;
	}
	pushdown(now);
	int mid=tr[now].l+tr[now].r>>1;
	if(l<=mid) modify_2(l,r,k,now<<1);
	if(r>mid) modify_2(l,r,k,now<<1|1);
	tr[now].sum=tr[now<<1].sum+tr[now<<1|1].sum;
	tr[now].sum%=m;
}

int query(int l,int r,int now){
	pushdown(now);
	if(tr[now].l>=l&&tr[now].r<=r) return tr[now].sum;
	int mid=tr[now].l+tr[now].r>>1,ans=0;
	if(l<=mid) ans+=query(l,r,now<<1);
	if(r>mid) ans+=query(l,r,now<<1|1);
	ans%=m;
	return ans;
}

signed main(){
	cin>>n>>q>>m;
	build(1,n,1);
	int op,x,y,k;
	while(q--){
		cin>>op>>x>>y;
		if(op==1){
			cin>>k;
			modify_1(x,y,k,1);
		}else if(op==2){
			cin>>k;
			modify_2(x,y,k,1);
		}else{
			cout<<query(x,y,1)%m<<endl;
		}
	}
	return 0;
}

2024/7/28 23:29
加载中...