三向城(city)

这题是线段树?!!

----------------------------------------------

这题中编号为x(x>1)的路口连出3条道路通向编号为x*2,x*2+1和x/2(向下取整),不断找两个点的父亲。

则答案为(int)log2(head)+(int)log2(last)-2*(int)log2(a)

线段树(LCA)

#include<cstdio>
#include<cmath>
int n,head,last;

int main()
{
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d%d",&head,&last);
		int a=head,b=last;
		while(a!=b&&a!=1&&b!=1)
			if(a>b)
				a/=2;
			else
				b/=2;
		a=a>b?b:a;
		printf("%d\n",((int)log2(head)+(int)log2(last)-2*(int)log2(a)));
	}
	return 0;
}

猜你喜欢

转载自www.cnblogs.com/lfyz1703/p/8966218.html