有个线段树问题
  • 板块学术版
  • 楼主cengzh
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/10/24 13:17
  • 上次更新2024/10/24 15:53:42
查看原帖
有个线段树问题
640816
cengzh楼主2024/10/24 13:17

下面是我P2572的查询代码,每次查询返回一颗树

但在返回结果给t2时,t2各个数据与传给t2的树的有错,怀疑是内存分配有问题,但不清楚具体原因。

其它没有问题,就这一段,求分析。


struct Tree Query_2(int node,int l,int r,int tl,int tr)
{
	//printf ("node:%d [%d,%d]:lmax:%d rmax:%d ans:%d\n",node,l,r,Tree[node].lmax1,Tree[node].rmax1,Tree[node].ans1);
	if (tl <= l && r <= tr)
	{
		return Tree[node];
	}
	
	int mid = (l+r)/2;
	struct Tree t1;
	bool c1=false,c2=false;

	push_down_tag1(node,l,r);
	push_down_tag2(node,l,r);
	
	if (mid >= tl)
	{
		t1 = Query_2(node*2,l,mid,tl,tr);
		
		c1 = true;
	}
	struct Tree t2;
	if (mid < tr)
	{
		printf ("%d",Query_2(node*2+1,mid+1,r,tl,tr).lmax1);
		c2 = true;		
	} 
	struct Tree t3;
	if (c1&&c2)
	{
		t3.ans1 = max(t1.rmax1 + t2.lmax1,max(t1.ans1,t2.ans1));
		if (t1.lmax1 == t1.len)
		{
			t3.lmax1 = t1.lmax1 + t2.lmax1;	
		}  
		else
		{
			t3.lmax1 = t1.lmax1;
		}
		
		if (t2.rmax1 == t2.len)
		{
			t3.rmax1 = t2.rmax1 + t1.rmax1;	
		} 
		else
		{
			t3.rmax1 = t2.rmax1;
		}	
	}
	else if(c1)
	{
		t3 = t1;	
	}
	else if(c2);
	{
		t3 = t2;
	}
	
	return t3;
}

2024/10/24 13:17
加载中...