跳到正文

算法·理论

树状数组&线段树 例题

yyyyc_2约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年5月25日
收录
2025年12月12日
更新
2026年9月27日
篇幅
约 6 分钟
获赞
1

版本

  1. v1当前

数列操作(加强版)

1<=n<=1000000000

数据太大了!所以不能直接maketree(4*n)的大小会爆

动态开点才能做

注意每个数都要加z

C++
void pushup(int p)
{
	tree[p].val=max(tree[tree[p].l].val,tree[tree[p].r].val);
}
void pushdown(int p,int l,int r)
{
	if(tree[p].l==-1) 
	{
		tree[p].l=++tot; 
	}
	if(tree[p].r==-1)
	{
		 tree[p].r=++tot;
	}
	if(tree[p].val) 
	{
		int k=tree[p].lazy;
		int L=tree[p].l,R=tree[p].r;
		tree[L].val+=k;
		tree[L].lazy+=k;
		tree[R].val+=k;
		tree[R].lazy+=k;
		tree[p].lazy=0;
	}
}
void add(int p,int l,int r,int x,int y,int k)
{
	if(x<=l&&r<=y)
	{
		tree[p].val+=k;
		tree[p].lazy+=k;
		return;
	}
	int mid=(l+r)/2;
	pushdown(p,l,r);
	if(x<=mid)
	{
		add(tree[p].l,l,mid,x,y,k);
	}
	if(y>mid)
	{
		add(tree[p].r,mid+1,r,x,y,k);
	}
	pushup(p);
}
int getsum(int p,int l,int r,int x,int y)
{
	if(x<=l&&y>=r)
	{
		return tree[p].val;
	}
	int mid=(l+r)>>1;
	pushdown(p,l,r);
	int ans=0;
	if(x<=mid) 
	{
		ans=max(ans,getsum(tree[p].l,l,mid,x,y));
	}
	if(y>mid) 
	{
		ans=max(ans,getsum(tree[p].r,mid+1,r,x,y));
	}
	return ans;
}

小白逛公园

不要被标题上的DP迷惑了 边算边改 这是线段树

大意:求一个区间内最大连续字段和

那么我们就需要将这个区间分段

tree[p].sum(和) tree[p].lmax(左边最大值) tree[p].rmax(右边最大值) tree[p].maxx(总的最大值) 我们需要通过lmax和rmax去算maxx

C++
void pushup(int p)
{
	tree[p].sum=tree[2*p].sum+tree[2*p+1].sum;
	tree[p].lmax=max(tree[2*p].lmax,tree[2*p].sum+tree[2*p+1].lmax);
	tree[p].rmax=max(tree[2*p+1].rmax,tree[2*p].rmax+tree[2*p+1].sum);
	tree[p].maxx=max(max(tree[2*p].maxx,tree[2*p+1].maxx),tree[2*p].rmax+tree[2*p+1].lmax); 
}

但这里我们又发现了一个问题
求的时候算出来的不能够将原有的数据改变 这里就需要用到构造函数了

C++
yyc push_up(yyc x,yyc y)
{
	yyc ans;
	ans.sum=x.sum+y.sum;
	ans.lmax=max(x.lmax,x.sum+y.lmax);
	ans.rmax=max(y.rmax,y.sum+x.rmax);
	ans.maxx=max(max(x.maxx,y.maxx),x.rmax+y.lmax);
	return ans;
}
//算的时候用了struct  getsum也需要用struct
yyc getsum(int p,int s,int t) //查找[s,t]区间的最大连续和 
{
	if(s<=tree[p].l&&tree[p].r<=t)
	{
		return tree[p];
	}
	if(t<=tree[p*2].r) //[s,t]完全在左儿子内部 
	{
		return getsum(p<<1,s,t);
	}
	if(s>=tree[p*2+1].l) //[s,t]完全在右儿子内部 
	{
		return getsum(p<<1|1,s,t);
	}
	yyc x,y,ans;
	x=getsum(p<<1,s,t);
	y=getsum(p<<1|1,s,t);
	ans=push_up(x,y);
	return ans;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	maketree(1,1,n);
	for(int i=1;i<=m;i++)
	{
		int k,x,y;
		cin>>k>>x>>y;
		if(k==1)
		{
			if(x>y)
			{
				swap(x,y);
			}
			yyc ans=getsum(1,x,y);
			cout<<ans.maxx<<endl;
		}
		else
		{
			add(1,x,y);
		}
	}
	return 0;
}

既然说了这道题,我们再来看一道同类的题 区间连续值

题目已经很明显了,这一额是一道求连续值的题 不过要注意 这里只有0和1 那么按照上一题的算法 0 并不影响算出来的大小 它会把区间内的所有1加上

所以我们需要将0改变成一个很小的数(因为序列中最大的数是1,所以它并不能够消掉很小的数,算出来的答案只有连续的1)

将上一题的主函数改一下

C++
signed main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		if(a[i]==0)
		{
			a[i]=-1e5;
		}
	}
	maketree(1,1,n);
	for(int i=1;i<=m;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		if(x>y)
		{
			swap(x,y);
		}
		yyc ans=getsum(1,x,y);
		if(ans.maxx<0)
		{
			ans.maxx=0;
		}
		printf("%d\n",ans.maxx);
	}
	return 0;
}
原文