求助,30分
查看原帖
求助,30分
904155
wxc_1楼主2024/3/19 13:07
#include<bits/stdc++.h>
int n,m,q;
const int N=100003;
#define ll long long 
ll a[N],add[N<<2],mul[N<<2],t[N<<2];
ll ls(int p) {return p<<1;}
ll rs(int p) {return p<<1|1;}

void push_up(ll p)
{
    t[p]=(t[rs(p)]+t[ls(p)])%m;
}

void build(ll l,ll r,ll p)
{
    add[p]=0;
    mul[p]=1;
    if(l==r) {t[p]=a[l]%m;return ;}
    ll mid=(l+r)>>1;
    build(l,mid,ls(p));
    build(mid+1,r,rs(p));
    push_up(p);
}

void addtag(ll l,ll r,ll p,ll d)
{
    add[p]=(add[p]+d)%m;
    t[p]=(t[p]+(r-l+1)*d)%m;
}

void multag(ll l,ll r,ll p,ll d)
{
    if(add[p])
        add[p]*=d%m;
    mul[p]*=d%m;
    t[p]*=d%m;
}

void push_down(ll l,ll r,ll p)
{
    if(mul[p]!=1)
    {
        ll mid=(l+r)>>1;
        multag(l,mid,ls(p),mul[p]);
        multag(mid+1,r,rs(p),mul[p]);
        mul[p]=1;
    }
    if(add[p])
    {
        ll mid=(l+r)>>1;
        addtag(l,mid,ls(p),add[p]);
        addtag(mid+1,r,rs(p),add[p]);
        add[p]=0;
    }
}

void update(ll L,ll R,ll p,ll l,ll r,ll x,ll d)
{
    if(L<=l&&R>=r) 
    {
        if(x==1)
            multag(l,r,p,d);
        if(x==2)
            addtag(l,r,p,d);
        return ;
    }
    push_down(l,r,p);
    ll mid=(l+r)>>1;
    if(L<=mid) update(L,R,ls(p),l,mid,x,d);
    if(R>mid) update(L,R,rs(p),mid+1,r,x,d);
    push_up(p);
}

ll query(ll L,ll R,ll l,ll r,ll p)
{
    if(L<=l&&R>=r) {return t[p];}
    ll ans=0;
    ll mid=(l+r)>>1;
    push_down(l,r,p);
    if(L<=mid) ans=(ans+query(L,R,l,mid,ls(p)))%m;
    if(R>mid) ans=(ans+query(L,R,mid+1,r,rs(p)))%m;
    return ans;
}

int main()
{
    scanf("%d%d%d",&n,&q,&m);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    build(1,n,1);
    while(q--)
    {
        ll x,b,c,d;
        scanf("%lld%lld%lld",&x,&b,&c);
        if(x==1 || x==2)
        {
            scanf("%lld",&d);
            update(b,c,1,1,n,x,d);
        }
        else 
        {
            std::cout << query(b,c,1,n,1)%m << std::endl;
        }
    }

    return 0;
}
2024/3/19 13:07
加载中...