求助线段树写挂了
查看原帖
求助线段树写挂了
253738
听取MLE声一片楼主2020/12/2 22:28

样例都过不了求助

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#define ll long long
using namespace std;
unsigned ll n,m,a[1000001],tag1[2000001],tag2[2000001],ans[2000001],mod;
inline ll read(){
	ll 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-'0';ch=getchar();}
	return x*f;
}
inline ll lc(ll x){return x<<1;}
inline ll rc(ll x){return x<<1|1;}
void build(ll p,ll l,ll r){
	tag1[p]=1;
	tag2[p]=0;
	if(l==r){
		ans[p]=a[l];
		ans[p]%=mod;
		return;
	}
	ll mid=(l+r)>>1;
	build(lc(p),l,mid);
	build(rc(p),mid+1,r);
	ans[p]=ans[lc(p)]+ans[rc(p)];
	ans[p]%=mod;
} 
inline void push_down(ll p,ll l,ll r){
	ll mid=(l+r)/2;
	
    ans[lc(p)]=(ans[lc(p)]*tag1[p]+tag2[p]*(mid-l+1))%mod;
    ans[rc(p)]=(ans[rc(p)]*tag1[p]+tag2[p]*(r-mid))%mod;
    
    tag1[lc(p)]=(tag1[lc(p)]*tag1[p])%mod;
    tag1[rc(p)]=(tag1[rc(p)]*tag1[p])%mod;
    
    tag2[lc(p)]=(tag2[lc(p)]*tag1[p]+tag2[p])%mod;
    tag2[rc(p)]=(tag2[rc(p)]*tag1[p]+tag2[p])%mod;

    tag1[p]=1;
    tag2[p]=0;
    return ;
}
inline void update1(ll nl,ll nr,ll l,ll r,ll p,ll k){
	if(r>=nr||nl>=l)return;
    if(l>nl&&nr>r){
        ans[p]=(ans[p]*k)%mod;
        tag1[p]=(tag1[p]*k)%mod;
        tag2[p]=(tag2[p]*k)%mod;
        return;
    }
	push_down(p,l,r);
	ll mid=(l+r)>>1;
	update1(nl,nr,l,mid,lc(p),k);
	update1(nl,nr,mid+1,r,rc(p),k);
	ans[p]=(ans[lc(p)]+ans[rc(p)])%mod;
	return;
}
inline void update2(ll nl,ll nr,ll l,ll r,ll p,ll k){
	if(r>=nr||nl>=l)return;
    if(l>nl&&nr>r){
    	tag2[p]=(tag2[p]+k)%p;
        ans[p]=(ans[p]+k*(r-l+1))%p;
        return;
    }
	push_down(p,l,r);
	ll mid=(l+r)>>1;
	update2(nl,nr,l,mid,lc(p),k);
	update2(nl,nr,mid+1,r,rc(p),k);
	ans[p]=(ans[lc(p)]+ans[rc(p)])%mod;
	return;
}
ll query(ll q_x,ll q_y,ll l,ll r,ll p){
	ll res=0;
	if(q_x<=l&&r<=q_y)return ans[p];
	ll mid=(l+r)>>1;
	push_down(p,l,r);
	if(q_x<=mid)res+=query(q_x,q_y,l,mid,lc(p));
	if(q_y>mid) res+=query(q_x,q_y,mid+1,r,rc(p));
	return res;
}
int main()
{
	n=read(),m=read(),mod=read();
	for(int i=1;i<=n;i++)
		cin>>a[i];
	build(1,1,n);
	while(m--){
		ll a1,b,c,d;
		cin>>a1;
		if(a1==1){
			b=read(),c=read(),d=read();
			update1(b,c,1,n,1,d);
			continue;
		}
		if(a1==2){
			b=read(),c=read(),d=read();
			update2(b,c,1,n,1,d);
			continue;
		}
		if(a1==3){
			b=read(),c=read();
			cout<<query(b,c,1,n,1)<<endl;
			continue;
		}
	}
	return 0;
}
 

2020/12/2 22:28
加载中...