/ Randle /

记录详情

Accepted


  
# 状态 耗时 内存占用
#1 Accepted 1ms 200.0 KiB
#2 Accepted 1ms 200.0 KiB
#3 Accepted 1ms 204.0 KiB
#4 Accepted 1ms 200.0 KiB
#5 Accepted 2ms 208.0 KiB
#6 Accepted 1ms 204.0 KiB
#7 Accepted 3ms 208.0 KiB
#8 Accepted 3ms 196.0 KiB
#9 Accepted 4ms 204.0 KiB
#10 Accepted 3ms 192.0 KiB

代码

#include <bits/stdc++.h>

int deep = 1, dep[33];

struct fastIn
{
	template<typename T>
	inline fastIn& operator>>(T &x)
	{
		x = 0; bool f = 1; register char c = getchar();
		for(; !std::isdigit(c); c = getchar()) f ^= c == '-';
		for(; std::isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
		x = f ? x : -x; return *this;
	}
} io;

inline int lowerBound(int x)
{
	int l = 1, r = 31;
	while(l < r)
	{
		int mid = (l + r) >> 1;
		if(dep[mid] >= x) r = mid;
		else l = mid + 1;
	}
	if(dep[l] == x) return l;
	return l - 1;
}

inline int LCA(int x, int y)
{
	int dx = lowerBound(x), dy = lowerBound(y);
	if(dx < dy) std::swap(x, y), std::swap(dx, dy);
	for(int i = 1; i + dy <= dx; ++i) x >>= 1;
	if(x == y) return dx - dy;
	
	int cnt = 0;
	for(; x != y; ++cnt) x >>= 1, y >>= 1;
	return (cnt << 1) + (dx - dy);
}

signed main()
{
	int n; io >> n;
	for(int i = 1; i <= 31; ++i) dep[i] = deep, deep <<= 1;
	for(int i = 1; i <= n; ++i)
	{
		int x, y; io >> x >> y;
		printf("%d\n", LCA(x, y));
	}
	return 0;
}

信息

递交者
类型
递交
题目
三向城T1
题目数据
下载
语言
C++
递交时间
2019-10-11 10:43:00
评测时间
2019-10-11 10:43:00
评测机
分数
100
总耗时
24ms
峰值内存
208.0 KiB