AcerMoOi之路

一只蒟蒻

AHOI2009维护序列

线段树区间加乘裸题

别忘了mod就好

//BY AcerMo
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const long long int M=1000500;
struct Tree
{
    unsigned long long int lson,rson,ladd,lmul;
    unsigned long long int lb,rb;
    unsigned long long int asum;
}tree[M*2];
long long int num[M];
long long int n,m,root=0,cnt=1,mod;
void pushup(long long int cur)
{
    long long int ls=tree[cur].lson;
    long long int rs=tree[cur].rson;
    tree[cur].lb=tree[ls].lb;
    tree[cur].rb=tree[rs].rb;
    tree[cur].asum=tree[ls].asum%mod+tree[rs].asum%mod;
    tree[cur].asum%=mod;
}
void built(long long int L,long long int R,long long int cur)
{
    tree[cur].ladd=0;tree[cur].lmul=1;
    if (R==L)
    {
        tree[cur].asum=num[L];
        tree[cur].lson=tree[cur].rson=-1;
        tree[cur].lb=tree[cur].rb=L;
        return ;
    }
    tree[cur].lson=cnt++;
    tree[cur].rson=cnt++;
    long long int mid=(L+R)>>1;
    built(L,mid,tree[cur].lson);
    built(mid+1,R,tree[cur].rson);
    pushup(cur);
}
void pushdown(long long int cur)
{
    long long int lson=tree[cur].lson, rson=tree[cur].rson;
    tree[lson].asum=(tree[lson].asum*tree[cur].lmul+tree[cur].ladd*(tree[lson].rb-tree[lson].lb+1))%mod;
    tree[rson].asum=(tree[rson].asum*tree[cur].lmul+tree[cur].ladd*(tree[rson].rb-tree[rson].lb+1))%mod;
    tree[lson].lmul=(tree[lson].lmul*tree[cur].lmul)%mod;
    tree[rson].lmul=(tree[rson].lmul*tree[cur].lmul)%mod;
    tree[lson].ladd=(tree[lson].ladd*tree[cur].lmul+tree[cur].ladd)%mod;
    tree[rson].ladd=(tree[rson].ladd*tree[cur].lmul+tree[cur].ladd)%mod;
    tree[cur].lmul=1;tree[cur].ladd=0;
}
void updateadd(long long int L,long long int R,long long int val,long long int cur)
{
    if (L<=tree[cur].lb&&tree[cur].rb<=R) 
    {
        tree[cur].asum+=((val%mod)*(tree[cur].rb-tree[cur].lb+1))%mod;
        tree[cur].asum%=mod;tree[cur].ladd+=(val%mod);tree[cur].ladd%=mod;
        return ;
    }
    pushdown(cur);
    long long int mid=(tree[cur].lb+tree[cur].rb)>>1;
    if (L<=mid) updateadd(L,R,val,tree[cur].lson);
    if (R>mid) updateadd(L,R,val,tree[cur].rson);
    pushup(cur);
}
void updatemul(long long int L,long long int R,long long int val,long long int cur)
{
    if (L<=tree[cur].lb&&tree[cur].rb<=R)
    {
        tree[cur].asum*=(val%mod);tree[cur].asum%=mod;
        tree[cur].ladd*=(val%mod);tree[cur].ladd%=mod;
        tree[cur].lmul*=(val%mod);tree[cur].lmul%=mod;
        return ;
    }
    pushdown(cur);
    long long int mid=(tree[cur].lb+tree[cur].rb)>>1;
    if (L<=mid) updatemul(L,R,val,tree[cur].lson);
    if (R>mid) updatemul(L,R,val,tree[cur].rson);
    pushup(cur);
}
long long int querysum(long long int L,long long int R,long long int cur)
{
    unsigned long long int tot=0;
    if (L<=tree[cur].lb&&tree[cur].rb<=R) return tree[cur].asum%mod;
    pushdown(cur);
    long long int mid=(tree[cur].lb+tree[cur].rb)>>1;
    if (L<=mid)	tot+=querysum(L,R,tree[cur].lson)%mod;
    if (R>mid) tot+=querysum(L,R,tree[cur].rson)%mod;
    return tot%mod; 
}
long long int read()
{
    long long 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-'0';ch=getchar();}
    return x*f;
}
int main()
{
    n=read();mod=read();
    for (long long int i=1;i<=n;i++) num[i]=read(),num[i]%=mod;
    built(1,n,root);
    m=read();
    while (m--)
    {
        long long int a,b,c,d;
        d=read();
        if (d==1)
        {
            a=read();b=read();c=read();
            updatemul(a,b,c,root);	
        }
        else if (d==2)
        {
            scanf("%d %d %d",&a,&b,&c);
            updateadd(a,b,c,root);
        }	
        else if (d==3)
        {
            scanf("%d %d",&a,&b);
            cout<<querysum(a,b,root)%mod<<endl;
        }
    }	
    return 0;
}

阅读更多
版权声明: https://blog.csdn.net/ACerAndAKer/article/details/80688491
个人分类: 数据结构-线段树
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭