0pts WA玄关求条
查看原帖
0pts WA玄关求条
519276
W_Sibo楼主2024/10/2 20:27

实在不知道错哪里了

#include <bits/stdc++.h>
using namespace std;
const int N=100005;
struct tree{
	long long v,l,r,ad,mu=1;
}tr[N*4];
long long a[N],n,q,m;
void build(int p,int l,int r){
	tr[p].l=l;
	tr[p].r=r;
	tr[p].mu=1;
	if(l==r){
		tr[p].v=a[l];
		return;
	}
	int mid=(l+r)>>1;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	tr[p].v=tr[p*2].v+tr[p*2+1].v;
	tr[p].v%=m;
}
void domod(int p){
	tr[p].ad%=m;
	tr[p].mu%=m;
	tr[p].v%=m;
}
void pushdown(int p){
	tr[p*2].v=(tr[p*2].v*tr[p].mu+tr[p].ad*(tr[p*2].r-tr[p*2].l+1));
	tr[p*2+1].v=(tr[p*2+1].v*tr[p].mu+tr[p].ad*(tr[p*2+1].r-tr[p*2+1].l+1));
	tr[p*2+1].mu*=tr[p].mu;
	tr[p*2].mu*=tr[p].mu;
	tr[p*2].ad*=tr[p].mu;
	tr[p*2+1].ad*=tr[p].mu;
	tr[p].mu=1;
	tr[p*2].ad+=tr[p].ad;
	tr[p*2+1].ad+=tr[p].ad;
	domod(p*2);
	domod(p*2+1);
	tr[p].ad=0;
}
void mul(int p,int l,int r,int k){
	if(tr[p].r<l||r<tr[p].l){
		return;
	}
	if(l<=tr[p].l&&tr[p].r<=r){
		tr[p].v*=k;
		tr[p].mu*=k;
		tr[p].ad*=k;
		return;
	}
	pushdown(p);
	int mid=(tr[p].l+tr[p].r)/2;
	if(l<=mid) mul(p*2,l,r,k);
	if(r>mid) mul(p*2+1,l,r,k);
	tr[p].v=tr[p*2].v+tr[p*2+1].v;
	domod(p);
}
void add(int p,int l,int r,int k){
	if(tr[p].r<l||r<tr[p].l){
		return;
	}
	if(l<=tr[p].l&&tr[p].r<=r){
		tr[p].v+=k;
		tr[p].ad+=k;
		return;
	}
	pushdown(p);
	int mid=(tr[p].l+tr[p].r)/2;
	if(l<=mid) add(p*2,l,r,k);
	if(r>mid) add(p*2+1,l,r,k);
	tr[p].v=tr[p*2].v+tr[p*2+1].v;
	domod(p);
}
int query(int p,int l,int r){
	if(tr[p].r<l||r<tr[p].l){
		return 0;
	}
	if(l<=tr[p].l&&tr[p].r<=r){
		return tr[p].v;
	}
	int ans=0;
	pushdown(p);
	int mid=(tr[p].l+tr[p].r)/2;
	if(l<=mid) ans+=query(p*2,l,r);
	if(r>mid) ans+=query(p*2+1,l,r);
	ans%=m;
	return ans;
}
int main(){
	freopen("P3373_1.in","r",stdin);
	cin>>n>>q>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		a[i]%=m;
	}
	build(1,1,n);
	for(int i=1;i<=q;i++){
		int u;
		cin>>u;
		if(u==1){
			int x,y,k;
			cin>>x>>y>>k;
			mul(1,x,y,k);
		}else if(u==2){
			int x,y,k;
			cin>>x>>y>>k;
			add(1,x,y,k);
		}else{
			int x,y;
			cin>>x>>y;
			cout<<query(1,x,y)<<'\n';
		}
	}
} 
2024/10/2 20:27
加载中...