可怜巴巴求助本题线段树push_up
查看原帖
可怜巴巴求助本题线段树push_up
299810
issue_is_fw楼主2021/3/24 22:30

在线段树的askask函数加上这么一句whilewhile居然超时了...

为什么父节点会比左右儿子小啊....

其他代码都没问题,ac了,这是这个...

void ask(int &root,int l,int r,int L,int R)
{
	if( l>R || r<L )	return;
	if( root==0 )	return;
	if( l>=L&&r<=R )
	{
	//	update( root,mx,xuhao ); 
		if( max( sum[ls[root]],sum[rs[root]] )<sum[root] )
		{
			while( 1 )	int www;
		}
		if( sum[root]>mx )	mx = sum[root],xuhao = num[root];
		return;  
	}
	int mid = l+r>>1;
	ask( ls[root],l,mid,L,R); ask( rs[root],mid+1,r,L,R );
}

下面是线段树的代码

//下面是线段树 
int sum[N],num[N],rt[maxn],ls[N],rs[N],Line,mx,xuhao,n;
void update(int r,int &mx,int &xuhao)
{
	if( sum[ls[r]]>mx )	mx = sum[ls[r]], xuhao = num[ls[r]];
	if( sum[rs[r]]>mx )	mx = sum[rs[r]], xuhao = num[rs[r]];	
}
void add(int &root,int l,int r,int index)
{
	if( l>index || r<index )	return;
	if( !root )	root = ++Line;
	if( l==r&&l==index ){ sum[root]++; num[root] = l; return; }
	int mid = l+r>>1;
	add( ls[root],l,mid,index); add( rs[root],mid+1,r,index );
	update(root,sum[root],num[root]);
}
void ask(int &root,int l,int r,int L,int R)
{
	if( l>R || r<L )	return;
	if( root==0 )	return;
	if( l>=L&&r<=R )
	{
	//	update( root,mx,xuhao ); 
		if( max( sum[ls[root]],sum[rs[root]] )<sum[root] )
		{
			while( 1 )	int www;
		}
		if( sum[root]>mx )	mx = sum[root],xuhao = num[root];
		return;  
	}
	int mid = l+r>>1;
	ask( ls[root],l,mid,L,R); ask( rs[root],mid+1,r,L,R );
}
int merge(int x,int y,int l,int r)
{
	if( !x || !y )	return x|y;
	int p = ++Line;
	if( l==r )
	{
		sum[p] = sum[x]+sum[y];
		if( num[x] )	num[p] = num[x];
		if( num[y] )	num[p] = num[y];
		return p;
	}
	int mid = l+r>>1;
	ls[p] = merge( ls[x],ls[y],l,mid );
	rs[p] = merge( rs[x],rs[y],mid+1,r );
	update( p,sum[p],num[p] );
	return p;
}

完整代码这里LINK

不加那个判断是AC的,目测是线段树合并的锅??

2021/3/24 22:30
加载中...