线段树(该段求段+lazy)优化,洛谷之提高历练地,提高模板-nlogn数据结构

正文

      这个东西挺简单的吧,线段树就不细讲了,主要讲讲lazy。

      懒嘛~

      如果当前覆盖整个区间,那么我们就用lazy把它那个值记录下来,然后,如果不是完全覆盖当前区间,那么我们就把它下放,乘的话那你就用一下乘法分配律搞一下即可

#include<cstdio>
#include<cstdlib>
#include<cstring>

int n,m;
struct tree{int x,y,ls,rs;long long c,lazy;};
tree tr[200010];
int len=0;

void bt(int x,int y){
	len++;
	int i=len;
	tr[i].x=x;tr[i].y=y;
	tr[i].ls=-1;tr[i].rs=-1;
	tr[i].c=0;
	tr[i].lazy=0;
	if(x==y) return ;
	int mid=(x+y)/2;
	tr[i].ls=len+1;bt(x,mid);
	tr[i].rs=len+1;bt(mid+1,y);
}

void add(int p,int x,int y,int t){
	if(tr[p].x==x && tr[p].y==y){
		tr[p].lazy+=t;
		tr[p].c+=t*(y-x+1);
		return ;
	}
	int ls=tr[p].ls,rs=tr[p].rs;
	if(tr[p].lazy!=0){
		tr[ls].c+=tr[p].lazy*(tr[ls].y-tr[ls].x+1);
		tr[ls].lazy+=tr[p].lazy;
		tr[rs].c+=tr[p].lazy*(tr[rs].y-tr[rs].x+1);
		tr[rs].lazy+=tr[p].lazy;
		tr[p].lazy=0;
	}
	int mid=tr[tr[p].ls].y;
	if(y<=mid) add(tr[p].ls,x,y,t);
	else if(x>mid) add(tr[p].rs,x,y,t);
	else add(tr[p].ls,x,mid,t),add(tr[p].rs,mid+1,y,t);
	tr[p].c=tr[tr[p].ls].c+tr[tr[p].rs].c;
}

long long get_total(int p,int x,int y){
	if(tr[p].x==x && tr[p].y==y)
		return tr[p].c;
	int ls=tr[p].ls,rs=tr[p].rs;
	if(tr[p].lazy!=0){
		tr[ls].c+=tr[p].lazy*(tr[ls].y-tr[ls].x+1);
		tr[ls].lazy+=tr[p].lazy;
		tr[rs].c+=tr[p].l azy*(tr[rs].y-tr[rs].x+1);
		tr[rs].lazy+=tr[p].lazy;
		tr[p].lazy=0;
	}
	int mid=tr[ls].y;
	if(y<=mid) return get_total(ls,x,y);
	else if(x>mid) return get_total(rs,x,y);
	else return get_total(ls,x,mid)+get_total(rs,mid+1,y);
}


int main(){
	scanf("%d %d",&n,&m);
	bt(1,n);
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		add(1,i,i,x);
	}
	for(int i=1;i<=m;i++){
		int t,x,y;
		scanf("%d %d %d",&t,&x,&y);
		if(t==1) {
			int op;
			scanf("%d",&op);
			add(1,x,y,op);
		}
		else printf("%lld\n",get_total(1,x,y));
	}
}

乘法的话pushdown搞一搞

#include<cstdio>
#include<cstdlib>
#define ll long long

struct node{int l,r,lc,rc;ll c,lza,lzm;}tr[400010];
ll a[100010];
int n,m,len=0;
ll p;

void bt(int l,int r)
{
    len++;int now=len;
    tr[now].l=l;tr[now].r=r;tr[now].lc=tr[now].rc=-1;tr[now].c=tr[now].lza=0;tr[now].lzm=1;
    if(l<r)
    {
        int mid=(l+r)/2;
        tr[now].lc=len+1;bt(l,mid);
        tr[now].rc=len+1;bt(mid+1,r);
        tr[now].c=(tr[tr[now].lc].c+tr[tr[now].rc].c)%p;
    }
    else if(l==r) tr[now].c=a[l];
}

void pushdown(int now)
{
    int lc=tr[now].lc,rc=tr[now].rc;
    tr[lc].lzm=tr[lc].lzm*tr[now].lzm%p;
    tr[rc].lzm=tr[rc].lzm*tr[now].lzm%p;
    tr[lc].lza=(tr[lc].lza*tr[now].lzm+tr[now].lza)%p;
    tr[rc].lza=(tr[rc].lza*tr[now].lzm+tr[now].lza)%p;
    tr[lc].c=(tr[lc].c*tr[now].lzm+tr[now].lza*(tr[lc].r-tr[lc].l+1))%p;
    tr[rc].c=(tr[rc].c*tr[now].lzm+tr[now].lza*(tr[rc].r-tr[rc].l+1))%p;
    tr[now].lza=0;
    tr[now].lzm=1;
}

void mult(int now,int l,int r,ll k)
{
    if(tr[now].l==l && tr[now].r==r)
    {
        tr[now].lzm=tr[now].lzm*k%p;
        tr[now].lza=tr[now].lza*k%p;
        tr[now].c=tr[now].c*k%p;
        return;
    }
    pushdown(now);
    int lc=tr[now].lc,rc=tr[now].rc;
    int mid=(tr[now].l+tr[now].r)/2;
    if(r<=mid) mult(lc,l,r,k);
    else if(mid+1<=l) mult(rc,l,r,k);
    else
    {
        mult(lc,l,mid,k);
        mult(rc,mid+1,r,k);
    }
    tr[now].c=(tr[lc].c+tr[rc].c)%p;
}

void add(int now,int l,int r,ll k)
{
    if(tr[now].l==l && tr[now].r==r)
    {
        tr[now].lza=(tr[now].lza+k)%p;
        tr[now].c=(tr[now].c+(r-l+1)*k)%p;
        return;
    }
    pushdown(now);
    int lc=tr[now].lc,rc=tr[now].rc;
    int mid=(tr[now].l+tr[now].r)/2;
    if(r<=mid) add(lc,l,r,k);
    else if(mid+1<=l) add(rc,l,r,k);
    else
    {
        add(lc,l,mid,k);
        add(rc,mid+1,r,k);
    }
    tr[now].c=(tr[lc].c+tr[rc].c)%p;
}

ll findsum(int now,int l,int r)
{
    if(tr[now].l==l && tr[now].r==r) return tr[now].c%p;
    pushdown(now);
    int lc=tr[now].lc,rc=tr[now].rc;
    int mid=(tr[now].l+tr[now].r)/2;
    if(r<=mid) return findsum(lc,l,r)%p;
    else if(mid+1<=l) return findsum(rc,l,r)%p;
    else return (findsum(lc,l,mid)+findsum(rc,mid+1,r))%p;
}

int main()
{
    scanf("%d %d %lld",&n,&m,&p);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    bt(1,n);
    for(int i=1;i<=m;i++)
    {
        int k,x,y;
        scanf("%d %d %d",&k,&x,&y);
        if(k==1) 
        {
            ll z;
            scanf("%lld",&z);
            mult(1,x,y,z);
        }
        if(k==2)
        {
            ll z;
            scanf("%lld",&z);
            add(1,x,y,z);
        }
        if(k==3) printf("%lld\n",findsum(1,x,y));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值