蒟蒻求助,刚学线段树
查看原帖
蒟蒻求助,刚学线段树
388414
comcopy楼主2021/8/8 20:02

RT

样例过了但是全WA

#include<bits/stdc++.h>
#define ll long long
#define debug if(de)
using namespace std;
const int MX=1e5;
ll b[(MX<<2)+10];
ll a[MX];
ll d[(MX<<2)+10];
bool de=1;

void build(ll s,ll t,ll p)
{
	if(s==t)
		{
			d[p]=a[s];
			return;
		}
	ll m=s+((t-s)>>1);
	build(s,m,p*2);
	build(m+1,t,p*2+1);
	d[p]=d[p*2]+d[p*2+1];
}


void add(ll l,ll r,ll to,ll nowl,ll nowr,ll p)
{
	if(l<=nowl && nowr<=r)
		{
			d[p]+=(nowr-nowl+1)*to;
			b[p]+=to;
			return;
		}
	ll m=nowl+((nowr-nowl)>>1);
	if(b[p] && nowl!=nowr)	
		{
			d[p*2]+=b[p]*(m-nowl+1);
			d[p*2+1]+=b[p]*(nowr-m);
			b[p*2]+=b[p];
			b[p*2+1]+=b[p];
			b[p]=0;
		}
	if(l<=m) add(l,r,to,nowl,m,p*2);
	if(m<r) add(l,r,to,m+1,nowr,p*2+1);
	d[p]=d[p*2]+d[p*2+1];
	return;
}

void times(ll l,ll r,ll to,ll nowl,ll nowr,ll p)
{
	if(l<=nowl && nowr<=r)
		{
			d[p]*=to;
			b[p]*=to;
			return;
		}
	ll m=nowl+((nowr-nowl)>>1);
	if(b[p] && nowl!=nowr)
		{
			d[p*2]*=(b[p]*to)*(m-nowl+1);
			d[p*2+1]+=b[p]*to*(nowr-m);
			b[p*2]+=b[p];
			b[p*2+1]+=b[p];
			b[p]=0;
		}
	if(l<=m) times(l,r,to,nowl,m,p*2);
	if(m<r) times(l,r,to,m+1,nowr,p*2+1);
	d[p]=d[p*2]+d[p*2+1];
	return;
}

ll n,m,mod;

ll find_sum(ll l,ll r,ll nowl,ll nowr,ll p)
{
	if(l<=nowl && nowr<=r)
		{
			return d[p];
		}
	ll m=(nowl+((nowr-nowl)>>1));
	if(b[p] && nowl!=nowr )
		{
			d[p*2]+=b[p]*(m-nowl+1);
			d[p*2+1]+=b[p]*(nowr-m);
			b[p*2]+=b[p];
			b[p*2+1]+=b[p];
			b[p]=0;
		}
	ll sum(0);
	if(l<=m) 
		{
			sum+=find_sum(l,r,nowl,m,p*2)%mod;
			
		}
	if(m<r) 
		{
			sum+=find_sum(l,r,m+1,nowr,p*2+1)%mod;	
		}
	return sum%mod;
}

//void dfs(ll p,ll l,ll r)
//{
//	if(l==r)
//	{ 
//		cout<<d[p]<<endl;
//		return;
//	}
//	cout<<d[p]<<' ';
//	ll m=l+((r-l)>>1);
//	dfs(p*2,l,m);
//	dfs(p*2+1,m+1,r);
//	return;
//}

int main()
{
	cin>>n>>m>>mod;
	for(register int i=1;i<=n;++i)
		cin>>a[i];
	build(1,n,1);
	for(register int i=1;i<=m;++i)
		{
			ll c,u,v,k;
			cin>>c>>u>>v;
			
			switch(c)
				{
					case 2:cin>>k; add(u,v,k,1,n,1);break;
					case 1:cin>>k; times(u,v,k,1,n,1);break;
					case 3:
					{
//						cout<<"begin"<<endl;
//						dfs(1,1,n);
//						cout<<"end"<<endl;
						cout<<find_sum(u,v,1,n,1)%mod<<endl;
						break;
					} 
				}
		}
//	while(1);
	return 0;
}
2021/8/8 20:02
加载中...