线段树

//线段树 
//每个节点代表一段区间  除叶节点外均有左右子节点 
//左子节点:[L,(L+R)/2] 右子节点:[(L+R)/2+1,R]  叶节点长度为1 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int n,a[1001],cnt=1;//cnt树点标号 
struct uio{
    int num,pluslazy,mullazy;//num区间和 pluslazy延迟修改标记(加) mullazy延迟修改标记(乘) 
    int l,r;//该点所表示范围为[l,r] 
    int ls,rs;//左右子节点数组下标 
}tree[2001];//最大不超过2*n 
void pushup(int x)//更新节点信息 
{
    int lson=tree[x].ls;
    int rson=tree[x].rs;
    tree[x].num=tree[lson].num+tree[rson].num;
    tree[x].l=tree[lson].l;
    tree[x].r=tree[rson].r;
}
void pushdown_add(int x)//下载标记(增加)
{
    int lson=tree[x].ls;
    int rson=tree[x].rs;
    tree[lson].num+=tree[x].pluslazy*(tree[lson].r-tree[lson].l+1);
    tree[rson].num+=tree[x].pluslazy*(tree[rson].r-tree[rson].l+1);
    tree[lson].pluslazy+=tree[x].pluslazy;//标记下传 
    tree[rson].pluslazy+=tree[x].pluslazy;//标记下传
    tree[x].pluslazy=0; 
} 
void pushdown_mul(int x)//下载标记(乘积) 
{
    int lson=tree[x].ls;
    int rson=tree[x].rs;
    tree[lson].num*=tree[x].mullazy;
    tree[rson].num*=tree[x].mullazy;
    tree[lson].mullazy*=tree[x].mullazy;//标记下传
    tree[rson].mullazy*=tree[x].mullazy;//标记下传
    tree[lson].pluslazy*=tree[x].mullazy;//标记下传
    tree[rson].pluslazy*=tree[x].mullazy;//标记下传
    tree[x].mullazy=1;
    pushdown_add(x);
} 
void build(int L,int R,int now)//初始化 now当前节点 
{
    if(L==R)//区间长度为1 --> 叶节点 
    {
        tree[now].ls=-1;
        tree[now].rs=-1;
        tree[now].l=tree[now].r=L;
        tree[now].num=a[L];
        tree[now].mullazy=1;
        return;
    }
    int mid=(L+R)/2;
    tree[now].ls=++cnt;
    tree[now].rs=++cnt; 
    tree[now].mullazy=1;
    build(L,mid,tree[now].ls);
    build(mid+1,R,tree[now].rs);
    pushup(now);//更新节点信息 
} 
void update(int x,int y,int now)//单点修改 x为需修改位置 y为增加值 
{
    if(tree[now].l==tree[now].r)//找到该节点 
    {
        tree[now].num+=y;
        return;
    }
    int mid=(tree[now].l+tree[now].r)/2;
    if(x<=mid)
        update(x,y,tree[now].ls);
    else
        update(x,y,tree[now].rs);
    pushup(now);//更新节点信息 
}
int query(int L,int R,int now)//区间查询 
{
    if(L<=tree[now].l&&tree[now].r<=R)//当前节点代表区间为待查区间的子集 
        return tree[now].num;
    pushdown_mul(now);//下载标记(乘积) 
    int ans=0;
    int mid=(tree[now].l+tree[now].r)/2;
    if(L<=mid)
        ans+=query(L,R,tree[now].ls);
    if(mid+1<=R)
        ans+=query(L,R,tree[now].rs);
    return ans;
}
void update_section_add(int L,int R,int y,int now)//区间修改[L,R](增加)
{
    if(L<=tree[now].l&&tree[now].r<=R)//待查询区间完全覆盖当前节点表示区间 --> 更新区间信息与延迟标记 
    {
        tree[now].num+=y*(tree[now].r-tree[now].l+1);//y*K K为区间长度 
        tree[now].pluslazy+=y;
        return;
    }
    pushdown_mul(now);//下载标记(乘积) 
    int mid=(tree[now].l+tree[now].r)/2;
    if(L<=mid)
        update_section_add(L,R,y,tree[now].ls);
    if(mid+1<=R)
        update_section_add(L,R,y,tree[now].rs);
    pushup(now);//更新节点信息 
}
void update_section_multi(int L,int R,int y,int now)//区间修改[L,R](乘积)
{
    if(L<=tree[now].l&&tree[now].r<=R)//待查询区间完全覆盖当前节点表示区间 --> 更新区间信息与延迟标记 
    {
        tree[now].num*=y;
        tree[now].pluslazy*=y;
        tree[now].mullazy*=y;
        return;
    }
    pushdown_mul(now);//下载标记(乘积) 
    int mid=(tree[now].l+tree[now].r)/2;
    if(L<=mid)
        update_section_multi(L,R,y,tree[now].ls);
    if(mid+1<=R)
        update_section_multi(L,R,y,tree[now].rs);
    pushup(now);//更新节点信息 
} 
void do_something()
{
    return ;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    int root=1; 
    build(1,n,root);//root为根节点 
    do_something();
    return 0;
}
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;
int n,m,l,r;
LL p,w,a[100001],sum[400001],add[400001],mul[400001];
void pushdown(int L,int R,int now)
{
    int mid=(L+R)/2,lson=(now<<1),rson=((now<<1)|1);
    sum[lson]=(sum[lson]*mul[now]+(mid-L+1)*add[now])%p;
    sum[rson]=(sum[rson]*mul[now]+(R-mid)*add[now])%p;
    add[lson]=(add[lson]*mul[now]+add[now])%p;
    add[rson]=(add[rson]*mul[now]+add[now])%p;
    mul[lson]=(mul[lson]*mul[now])%p;
    mul[rson]=(mul[rson]*mul[now])%p;
    add[now]=0;
    mul[now]=1;
}
void build(int L,int R,int now)
{
    if(L==R)
    {
        sum[now]=a[L]%p;
        return;
    }
    int mid=(L+R)/2;
    build(L,mid,(now<<1));
    build(mid+1,R,((now<<1)|1));
    sum[now]=(sum[now<<1]+sum[(now<<1)|1])%p;
}
void mul_update(int L,int R,int now)
{
    if(l<=L&&R<=r)
    {
        sum[now]=(sum[now]*w)%p;
        add[now]=(add[now]*w)%p;
        mul[now]=(mul[now]*w)%p;
        return;
    }
    pushdown(L,R,now);
    int mid=(L+R)/2;
    if(l<=mid)
        mul_update(L,mid,(now<<1));
    if(mid+1<=r)
        mul_update(mid+1,R,((now<<1)|1));
    sum[now]=(sum[now<<1]+sum[(now<<1)|1])%p;
}
void add_update(int L,int R,int now)
{
    if(l<=L&&R<=r)
    {
        sum[now]=(sum[now]+w*(R-L+1))%p;
        add[now]=(add[now]+w)%p;
        return;
    }
    pushdown(L,R,now);
    int mid=(L+R)/2;
    if(l<=mid)
        add_update(L,mid,(now<<1));
    if(mid+1<=r)
        add_update(mid+1,R,((now<<1)|1));
    sum[now]=(sum[now<<1]+sum[(now<<1)|1])%p;
}
LL query(int L,int R,int now)
{
    if(l<=L&&R<=r)
        return sum[now];
    pushdown(L,R,now);
    int mid=(L+R)/2;
    LL ans=0;
    if(l<=mid)
        ans=(ans+query(L,mid,(now<<1)))%p;
    if(mid+1<=r)
        ans=(ans+query(mid+1,R,((now<<1)|1)))%p;
    return ans;
}
void do_something()
{
    for(int i=1;i<=m;i++)
    {
        int k;
        scanf("%d",&k);
        if(k==1)
        {
            scanf("%d%d%lld",&l,&r,&w);
            mul_update(1,n,1);
        }
        if(k==2)
        {
            scanf("%d%d%lld",&l,&r,&w);
            add_update(1,n,1);
        }
        if(k==3)
        {
            scanf("%d%d",&l,&r);
            printf("%lld\n",query(1,n,1)%p);
        }
    }
    return;
}
int main()
{
    scanf("%d%d%d",&n,&m,&p);
    for(int i=1;i<=2*n;i++)
        mul[i]=1;
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    build(1,n,1);
    do_something(); 
    return 0;
}

 

posted @ 2018-07-08 18:29  radishえらい  阅读(137)  评论(0编辑  收藏  举报