/ Randle /

记录详情

Wrong Answer


  
# 状态 耗时 内存占用
#1 Accepted 1ms 196.0 KiB
#2 Wrong Answer 1ms 204.0 KiB
#3 Wrong Answer 1ms 212.0 KiB
#4 Wrong Answer 1ms 200.0 KiB
#5 Wrong Answer 1ms 204.0 KiB
#6 Wrong Answer 1ms 200.0 KiB
#7 Wrong Answer 3ms 212.0 KiB
#8 Wrong Answer 3ms 204.0 KiB
#9 Wrong Answer 4ms 200.0 KiB
#10 Wrong Answer 4ms 196.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) + (dy - dx);
}

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:32:56
评测时间
2019-10-11 10:32:56
评测机
分数
10
总耗时
24ms
峰值内存
212.0 KiB