求助!
查看原帖
求助!
502789
homo_114楼主2022/7/30 17:50

线段树实在调不出来了

#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-48;ch=getchar();
	}
	return x*f;
}
struct Node{
	int l,r;
	long long sum,addlazy,mullazy;
}tree[400010];
int n,m,mod,a[100010];
inline void pushup(int p){
	tree[p].sum=(tree[p*2].sum+tree[p*2+1].sum)%mod;
}
inline void build(int p,int l,int r){
	tree[p].l=l,tree[p].r=r,tree[p].mullazy=1;
	if(l==r){
		tree[p].sum=a[l]%mod;
		return;
	}
	int mid=(l+r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	pushup(p);
}
inline void pushdown(int p){
	if((tree[p].addlazy!=0)||(tree[p].mullazy!=1)){
		tree[p*2].sum=(tree[p*2].sum*tree[p].mullazy+(tree[p*2].r-tree[p*2].l+1)*tree[p].addlazy)%mod;
    	tree[p*2+1].sum=(tree[p*2+1].sum*tree[p].mullazy+(tree[p*2+1].r-tree[p*2+1].l+1)*tree[p].addlazy)%mod;
		tree[p*2].mullazy=(tree[p*2].mullazy*tree[p].mullazy)%mod;
    	tree[p*2+1].mullazy=(tree[p*2+1].mullazy*tree[p].mullazy)%mod;
    	tree[p*2].addlazy=(tree[p*2].addlazy*tree[p].mullazy+tree[p].addlazy)%mod;
    	tree[p*2+1].addlazy=(tree[p*2+1].addlazy*tree[p].mullazy+tree[p].addlazy)%mod;
    	tree[p].addlazy=0;
    	tree[p].mullazy=1;
	}
}
inline void add(int p,int l,int r,int k){
	if(l<=tree[p].l&&r>=tree[p].r){
		tree[p].sum=(tree[p].sum+(long long)k*(tree[p].r-tree[p].l+1))%mod;
		tree[p].addlazy+=(tree[p].sum+k)%mod;
		return;
	}
	pushdown(p);
	int mid=(tree[p].l+tree[p].r)/2;
	if(l<=mid){
		add(p*2,l,r,k);
	}
	if(r>mid){
		add(p*2+1,l,r,k);
	}
	pushup(p);
}
inline void mul(int p,int l,int r,int k){
	if(l<=tree[p].l&&r>=tree[p].r){
		tree[p].sum=(tree[p].sum*k)%mod;
		tree[p].addlazy=(tree[p].addlazy*k)%mod;
		tree[p].mullazy=(tree[p].mullazy*k)%mod;
		return;
	}
	pushdown(p);
	int mid=(tree[p].l+tree[p].r)/2;
	if(l<=mid){
		add(p*2,l,r,k);
	}
	if(r>mid){
		add(p*2+1,l,r,k);
	}
	pushup(p);
}
inline long long query(int p,int l,int r){
	if(l<=tree[p].l&&r>=tree[p].r){
		return tree[p].sum;
	}
	long long ans=0;
	int mid=(tree[p].l+tree[p].r)/2;
	if(l<=mid){
		ans=(ans+query(p*2,l,r))%mod;
	}
	if(r>mid){
		ans=(ans+query(p*2+1,l,r))%mod;
	}
	return ans;
}
int main(){
	n=read(),m=read(),mod=read();
	for(int i=1;i<=n;i++){
		a[i]=read();
	}
	build(1,1,n);
	for(int i=1;i<=m;i++){
		int op;
		op=read();
		if(op==1){
			int x,y,k;
			x=read(),y=read(),k=read();
			mul(1,x,y,k%mod);
		}
		if(op==2){
			int x,y,k;
			x=read(),y=read(),k=read();
			add(1,x,y,k%mod);
		}
		if(op==3){
			int x,y;
			x=read(),y=read();
			printf("%lld\n",query(1,x,y));
		}
	}
	return 0;
}
2022/7/30 17:50
加载中...