圆与圆之间的距离是不能一概而论的

15 篇文章 0 订阅
12 篇文章 0 订阅

题目

传送门 to nowcoder

思路

话说怎么这么多短小精悍的代码啊,感觉我的做法好差劲。

首先这是个树形结构。所以我们求出这颗树就行了。问题是,怎么找到包含一个圆的最小圆?

我最初想到分治。画一条线,将圆分到左边、右边。然后发现根本做不动。但并不是全无所获。我发现,对于任意一条直线 l l l,只要找到每个圆与 l l l 相交的部分——也就是 l l l 上的一个线段,被圆完全包含在其中的——就可以快速得到父节点。

为蛤呢?先证明必要:如果圆 A A A 被圆 B B B 所包含,那么圆 A A A 对应的线段就会被圆 B B B 对应的线段包含,用数学语言写出来就是 A ⊆ B    ⇒    ( A ∩ l ) ⊆ ( B ∩ l ) A\subseteq B\;\Rightarrow\;(A\cap l)\subseteq(B\cap l) AB(Al)(Bl) ,这里的集合可以看做平面上的点集。再证明充分:如果圆 A A A 对应的线段被圆 B B B 对应的线段包含,那么圆 A A A 被圆 B B B 包含。用数学语言写出来就是 ( A ∩ l ) ⊊ ( B ∩ I )    ⇒    A ⊆ B (A\cap l)⊊(B\cap I)\;\Rightarrow\;A\subseteq B (Al)(BI)AB 。当然这里有个大前提就是 ( A ∩ B ) ∈ { A , B } (A\cap B)\in\{A,B\} (AB){A,B}

突然发现前面都是废话。反正记住这句话,圆的包含关系转化为了线段的包含关系。而线段包含是一维的,很好做了。

可是我们怎么获得这条线呢?答案就是 扫描线。现在我们只需要试着实现如下操作:

  • 动态加入和删除一个区间。
  • 查询一个长度最小的区间 [ l , r ] [l,r] [l,r] 满足 l ≤ x ≤ r l\le x\le r lxr

为啥是单个的 x x x 呢?因为扫描线与圆相切的时候,区间退化成了点。

第二条可以用 T r e a p \tt Treap Treap 维护。以 l l l 为关键字,存储子树内最大的 r r r 。用 s p l i t \tt split split 保证 l ≤ x l\le x lx ,再在平衡树上往下走,优先走 l l l 更大的部分,用维护的 max ⁡ r \max r maxr 成功剪枝。

复杂度 O ( n log ⁡ n ) \mathcal O(n\log n) O(nlogn) 。但是常数好像挺大。

代码

#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef long long int_;
inline int readint(){
	int a = 0; char c = getchar(), f = 1;
	for(; c<'0'||c>'9'; c=getchar())
		if(c == '-') f = -f;
	for(; '0'<=c&&c<='9'; c=getchar())
		a = (a<<3)+(a<<1)+(c^48);
	return a*f;
}

const int MaxN = 100005;

int line_x; // 扫描线的横坐标
struct Circle{
	int x, y, r;
	double range_l() const {
		int_ dx = x-line_x;
		return y-sqrt(1ll*r*r-dx*dx);
	}
	double range_r() const {
		int_ dx = x-line_x;
		return y+sqrt(1ll*r*r-dx*dx);
	}
	bool operator < (const Circle &t) const {
		return range_r() < t.range_r();
	}
};
Circle circ[MaxN]; // 所有的圆

int prio[MaxN], data[MaxN];
int son[MaxN][2], rt;
int mx[MaxN]; // 最大右端点对应的圆
void pushUp(int o){
	mx[o] = data[o];
	for(int i=0; i<2; ++i){
		if(son[o][i] == 0) continue;
		if(circ[mx[o]] < circ[mx[son[o][i]]])
			mx[o] = mx[son[o][i]];
	}
}
/** @brief 新建一个代表 @p i 号圆的节点 */
int newNode(int i){
	static int cntNode = 0;
	int &id = ++ cntNode;
	prio[id] = rand();
	data[id] = mx[id] = i;
	son[id][0] = son[id][1] = 0;
	return id;
}
struct PAIR{
	int a[2];
	PAIR(int F=0,int S=0){
		a[0] = F, a[1] = S;
	}
	int& operator[](const int &x){
		return a[x]; // 像数组一样
	}
};
bool equal_; // 相等时去哪边
/** @brief 将左端点不超过 @p big_l 的裂开 */
PAIR split(int o,int big_l){
	if(!o) return PAIR(0,0);
	if(circ[data[o]].range_l() < big_l ||
	(circ[data[o]].x+circ[data[o]].r == line_x
	&& circ[data[o]].y == big_l // 刚好相等时
	&& !equal_)){
		PAIR p = split(son[o][1],big_l);
		son[o][1] = p[0], pushUp(o);
		p[0] = o; return p;
	}
	PAIR p = split(son[o][0],big_l);
	son[o][0] = p[1], pushUp(o);
	p[1] = o; return p;
}
int merge(int a,int b){
	if(!a || !b) return a+b;
	if(prio[a] > prio[b]){
		son[a][1] = merge(son[a][1],b);
		pushUp(a); return a;
	}
	son[b][0] = merge(a,son[b][0]);
	pushUp(b); return b;
}
/** @return @p o 子树中右端点超过 @p sma_r 的,左端点最大的一个圆 */
int findFa(int o,int sma_r){
	/* 这里无需修正值 exp_ ,因为不会相等 */ ;
	if(!o || circ[mx[o]].range_r() < sma_r)
		return 0; // 查无此点
	int p = findFa(son[o][1],sma_r);
	if(p != 0) return p;
	if(circ[data[o]].range_r() > sma_r)
		return data[o]; // 就是此圆
	return findFa(son[o][0],sma_r);
}
/** @return @p i 号圆的父节点 */
int insert(int i){
	/* 这里不用设置 equal_ ,因为不会相等 */ ;
	PAIR p = split(rt,circ[i].y);
	int res = findFa(p[0],circ[i].y);
// printf("p[0] = %d\n",p[0]);
	p[0] = merge(p[0],newNode(i));
	rt = merge(p[0],p[1]); return res;
}
void erase(int i){
	equal_ = 0; // 左儿子
	PAIR r = split(rt,circ[i].y);
	equal_ = 1; // 右儿子
	PAIR l = split(r[0],circ[i].y);
	rt = merge(l[0],r[1]);
}

struct Cmd{
	int x, id; bool tag;
	operator int() const {
		return x;
	}
};
Cmd cmd[MaxN<<1];

struct Edge{
	int to, nxt;
};
Edge e[MaxN<<1];
int head[MaxN], cntEdge;
void addEdge(int a,int b){
	e[cntEdge].to = b;
	e[cntEdge].nxt = head[a];
	head[a] = cntEdge ++;
}

int fa[MaxN][20], dep[MaxN];
void dfs(int x){
// printf("x = %d\n",x);
	dep[x] = dep[fa[x][0]]+1;
	for(int j=0; fa[x][j]; ++j)
		fa[x][j+1] = fa[fa[x][j]][j];
	for(int i=head[x]; ~i; i=e[i].nxt){
		fa[e[i].to][0] = x;
		dfs(e[i].to);
	}
// printf("return x = %d\n",x);
}
int getLca(int a,int b){
	if(dep[a] < dep[b]) swap(a,b);
	for(int j=19; ~j; --j)
		if((dep[a]-dep[b])>>j&1)
			a = fa[a][j];
	if(a == b) return a;
	for(int j=19; ~j; --j)
		if(fa[a][j] != fa[b][j]){
			a = fa[a][j];
			b = fa[b][j];
		}
	return fa[a][0];
}

int main(){
	srand(5201314);
	int n = readint();
	for(int i=1; i<=n; ++i){
		circ[i].x = readint();
		circ[i].y = readint();
		circ[i].r = readint();
		cmd[2*i-1].x = circ[i].x-circ[i].r;
		cmd[2*i-1].tag = true; // add
		cmd[i<<1].x = circ[i].x+circ[i].r;
		cmd[i<<1].tag = false; // minus
		cmd[2*i-1].id = cmd[i<<1].id = i;
	}
	sort(cmd+1,cmd+2*n+1);
	for(int i=0; i<=n; ++i)
		head[i] = -1; // 为了建图
	for(int i=1; i<=2*n; ++i){
		line_x = cmd[i].x;
		if(cmd[i].tag)
			addEdge(insert(cmd[i].id),
				cmd[i].id); // 找到爹了
		if(!cmd[i].tag)
			erase(cmd[i].id);
// printf("%d circ %d\n",cmd[i].tag,cmd[i].id);
	}
	dfs(0); // 超级大圆
	int q = readint();
	for(int a,b; q--; ){
		a = readint(), b = readint();
		int lca = getLca(a,b);
		int ans = dep[a]+dep[b];
		ans -= (dep[lca]<<1|1);
		if(a != lca && b != lca)
			-- ans; // 再减一个
		if(a == b) ans = 0;
		printf("%d\n",ans);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值