没调出来 P2023

#include<iostream>
#include<cstdio>
#include<cstring>

#define ll long long
#define N 100010

using namespace std;
ll ans,mod;
int n,m;
struct tree
{
    int l,r;
    int mul_,sum_;
    long long sum;
}tr[N<<2];

void pushup(int k)
{
    tr[k].sum=(tr[k<<1].sum+tr[k<<1|1].sum)%mod;
}

void build(int k,int l,int r)
{
    tr[k].l=l;tr[k].r=r;tr[k].sum_=0;tr[k].mul_=1;
    if(l==r)
    {
        scanf("%lld",&tr[k].sum);
        return;
    }
    int mid=(l+r)>>1;
    build(k<<1,l,mid);build(k<<1|1,mid+1,r);
    pushup(k);
}

void down_sum(int k)
{
    tr[k<<1].sum_=(tr[k].sum_+tr[k<<1].sum)%mod;
    tr[k<<1|1].sum_=(tr[k].sum_+tr[k<<1|1].sum)%mod;
    tr[k<<1].sum+=(tr[k<<1].r-tr[k<<1].l+1)*tr[k<<1].sum_;
    tr[k<<1].sum%=mod;
    tr[k<<1|1].sum+=(tr[k<<1|1].r-tr[k<<1|1].l+1)*tr[k<<1|1].sum_;
    tr[k<<1|1].sum%=mod;
    tr[k].sum_=0;
}

void down_mul(int k)
{
    tr[k<<1].mul_=(tr[k<<1].mul_*tr[k].mul_)%mod;
    tr[k<<1|1].mul_=(tr[k<<1|1].mul_*tr[k].mul_)%mod;
    tr[k<<1].sum_=(tr[k<<1].sum_*tr[k].mul_)%mod;
    tr[k<<1].sum_=(tr[k<<1|1].sum_*tr[k].mul_)%mod;
    
    tr[k<<1].sum_=(tr[k<<1].sum*tr[k<<1].mul_)%mod;
    tr[k<<1].sum+=(tr[k<<1].r-tr[k<<1].l+1)*tr[k<<1].sum_;
    tr[k<<1].sum%=mod;
    
    tr[k<<1].sum_=(tr[k<<1|1].sum*tr[k<<1|1].mul_)%mod;
    tr[k<<1|1].sum+=(tr[k<<1|1].r-tr[k<<1|1].l+1)*tr[k<<1|1].sum_;
    tr[k<<1|1].sum%=mod;
    
    tr[k].mul_=1;
}

void changesum(int k,int l,int r,int c)
{
    if(l>r||r<l) return;
    if(tr[k].l==l && tr[k].r==r)
    {
        tr[k].sum_+=c;
        tr[k].sum+=(tr[k].r-tr[k].l+1)*tr[k].sum_;
        tr[k].sum%=mod;
        return;
    }
    if(tr[k].mul_!=1) down_mul(k);
    if(tr[k].sum_) down_sum(k);
    int mid=(tr[k].l+tr[k].r)>>1;
    if(r<=mid) changesum(k<<1,l,r,c);
    else if(l>mid) changesum(k<<1|1,l,r,c);
    else changesum(k<<1,l,mid,c),changesum(k<<1|1,mid+1,r,c);
    pushup(k);
}

void changemul(int k,int l,int r,int c)
{
    if(l>r||r<l) return;
    if(tr[k].l==l && tr[k].r==r)
    {
        tr[k].mul_*=c;
        tr[k].sum_*=c;
        tr[k].sum*=c%mod;
        //tr[k].sum+=(tr[k].r-tr[k].l+1)*tr[k].sum_;
        tr[k].sum%=mod;
        return;
    }
    if(tr[k].mul_!=1) down_mul(k);
    if(tr[k].sum_) down_sum(k);
    int mid=(tr[k].l+tr[k].r)>>1;
    if(r<=mid) changemul(k<<1,l,r,c);
    else if(l>mid) changemul(k<<1|1,l,r,c);
    else changemul(k<<1,l,mid,c),changemul(k<<1|1,mid+1,r,c);
    pushup(k);
}

int query(int k,int l,int r)
{
    if(l>r||r<l) return 0;
    if(tr[k].l==l && tr[k].r==r)
    return tr[k].sum%mod;//
    if(tr[k].mul_!=1) down_mul(k);
    if(tr[k].sum_) down_sum(k);
    int mid=(tr[k].r+tr[k].l)>>1;
    if(r<=mid) return query(k<<1,l,r)%mod;
    else if(l>mid) return query(k<<1|1,l,r)%mod;
    else return query(k<<1,l,mid)%mod+query(k<<1|1,mid+1,r)%mod;
}

int main()
{
    scanf("%d%lld",&n,&mod);
    build(1,1,n);
    scanf("%d",&m);
    int x,y,f,c;
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&f);
        if(f==1)
        {
            scanf("%d%d%d",&x,&y,&c);
            changemul(1,x,y,c);
        }
        else if(f==2)
        {
            scanf("%d%d%d",&x,&y,&c);
            changesum(1,x,y,c);            
        }
        else
        {
            scanf("%d%d",&x,&y);
            printf("%d\n",query(1,x,y)%mod);
        }
    }
    return 0;
}

 

posted @ 2017-07-30 15:22  安月冷  阅读(127)  评论(0编辑  收藏  举报