BZOJ1798AHOI2009维护序列

版权声明:我这么弱的蒟蒻,虽然博文不是很好,但也请标明转发地址喵! https://blog.csdn.net/ACerAndAKer/article/details/80688491

线段树区间加乘裸题

别忘了mod就好

update 18-07-24

据说线段树用数组写比结构体快,然后我就试了一下,真的快,快了1kms

代码(结构体也保留了)

//By AcerMo
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lli long long int
using namespace std;
const lli M=1000500;
lli num[M];
lli add[M],mul[M];
int n,m,root,cnt,mod;
lli sum[M],ls[M],rs[M],lb[M],rb[M];
void pushup(int x)
{
    lb[x]=lb[ls[x]];rb[x]=rb[rs[x]];
    sum[x]=(sum[ls[x]]+sum[rs[x]])%mod;
    return ;
}
void built(int l,int r,int x)
{
    add[x]=0;mul[x]=1;
    if (r==l)
    {
        sum[x]=num[l];
        ls[x]=rs[x]=-1;
        lb[x]=rb[x]=l;
        return ;
    }
    ls[x]=++cnt;rs[x]=++cnt;
    lli mid=(l+r)>>1;
    built(l,mid,ls[x]);built(mid+1,r,rs[x]);
    return (void)(pushup(x));
}
void pushdown(int x)
{
    sum[ls[x]]=(sum[ls[x]]*mul[x]+add[x]*(rb[ls[x]]-lb[ls[x]]+1))%mod;
    sum[rs[x]]=(sum[rs[x]]*mul[x]+add[x]*(rb[rs[x]]-lb[rs[x]]+1))%mod;
    add[ls[x]]=(add[ls[x]]*mul[x]+add[x])%mod;
    add[rs[x]]=(add[rs[x]]*mul[x]+add[x])%mod;
    mul[ls[x]]=(mul[ls[x]]*mul[x])%mod;
    mul[rs[x]]=(mul[rs[x]]*mul[x])%mod;
    return (void)(mul[x]=1,add[x]=0);
}
void updateadd(int l,int r,int val,int x)
{
    if (l<=lb[x]&&rb[x]<=r) 
    {
        sum[x]=(sum[x]+val*(rb[x]-lb[x]+1))%mod;
        return (void)(add[x]=(val+add[x])%mod);
    }
    pushdown(x);
    lli mid=(lb[x]+rb[x])>>1;
    if (l<=mid) updateadd(l,r,val,ls[x]);
    if (r>mid) updateadd(l,r,val,rs[x]);
    return (void)(pushup(x));
}
inline void updatemul(int l,int r,int val,int x)
{
    if (l<=lb[x]&&rb[x]<=r)
    {
        sum[x]=(val*sum[x])%mod;
        add[x]=(val*add[x])%mod;
        mul[x]=(val*mul[x])%mod;
        return ;
    }
    pushdown(x);
    lli mid=(lb[x]+rb[x])>>1;
    if (l<=mid) updatemul(l,r,val,ls[x]);
    if (r>mid) updatemul(l,r,val,rs[x]);
    return (void)(pushup(x));
}
inline int query(int l,int r,int x)
{
    lli tot=0;
    if (l<=lb[x]&&rb[x]<=r) return sum[x]%mod;
    pushdown(x);
    int mid=(lb[x]+rb[x])>>1;
    if (l<=mid)	tot=query(l,r,ls[x])%mod;
    if (r>mid) tot+=query(l,r,rs[x])%mod;
    return tot%mod; 
}
inline int read()
{
    lli 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 (lli i=1;i<=n;i++) num[i]=read(),num[i]%=mod;
    built(1,n,root);m=read();
    while (m--)
    {
        int fl=read(),l=read(),r=read();
        if (fl==1)
        {
            int mu=read();updatemul(l,r,mu%mod,root);	
        }
        else if (fl==2)
        {
           	int ad=read();updateadd(l,r,ad%mod,root);
        }	
        else cout<<query(l,r,root)%mod<<endl;
    }	
    return 0;
}
//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;
}

 

阅读更多
想对作者说点什么? 我来说一句

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