穿过圆
[Link](4202. 穿过圆 - AcWing题库)
题意
在一个二维平面上有 n n n 个点和 m m m 个圆。
点的编号为 1 ∼ n 1\sim n 1∼n。
不存在某个点恰好在某个圆的边上的情况。
任意两个圆之间没有公共点。
现在,请你回答 k k k个询问。
每个询问给定两个点 a i a_i ai、 b i b_i bi,并请你回答从点 a i a_i ai 出发沿任意连续路径到达点 a i a_i ai,至少需要穿过多少个圆。
题解
- 解法一 (bitset)
题目给出可以沿任意连续路径,因此假设 a , b a,b a,b两点之间间隔 k k k个圆(一个点在某个圆内,一个点在某个圆外),那么我至少需要穿过 k k k个圆。我每次沿着从近向远穿过一个间隔圆,就可以恰好穿过 k k k个圆。
但是 1 ≤ k ≤ 1 × 1 0 5 , 1 ≤ m ≤ 1000 1\le k\le 1\times 10^5,1\le m \le 1000 1≤k≤1×105,1≤m≤1000,复杂度为 O ( m k ) O(mk) O(mk),会 T T T,考虑用bitset优化,bitset每个元素占一个bit、一般会快30倍左右,复杂度为 O ( m k 30 ) O(\frac{mk}{30}) O(30mk),注意bitset s[size],s数组下标从1开始。
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#include <deque>
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1010, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
struct Circle {
int x, y, r;
Circle(){}
}c[N];
PII a[N];
bitset<N> s[N];
bool find(int i, int j) {
int dx = a[i].x - c[j].x, dy = a[i].y - c[j].y;
return (LL)dx * dx + (LL)dy * dy > (LL)c[j].r * c[j].r;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m >> k;
for (int i = 1; i <= n; i ++ ) cin >> a[i].x >> a[i].y;
for (int i = 0; i < m; i ++ ) cin >> c[i].r >> c[i].x >> c[i].y;
for (int i = 1; i <= n; i ++ )
for (int j = 0; j < m; j ++ )
s[i][j] = find(i, j);
while (k --) {
int a, b; cin >> a >> b;
cout << ((s[a] ^ s[b]).count()) << endl;
}
return 0;
}
- 解法二 (lca)
因为圆与圆之间只存在不相交或者包含关系,发现这些圆将平面分成了一些平面(每个圆包含的不被它包含的圆包含的平面),我们将这些平面映射成点并将整个二维平面当成一个超级源点,每个平面从包含他的最小的平面连一条边,发现是一个树形结构,我们求从 a a a到 b b b穿过多少个圆,就转化为从 a a a所在的平面所映射的点到 b b b所在的平面所映射的点在树上的距离,树上距离可以用lca来求,复杂度为 O ( k l o g n ) O(klogn) O(klogn)。
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#include <deque>
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1010, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
PII a[N];
struct Circle {
int x, y, r;
Circle(){};
}c[N];
int f[N][11], dep[N], fa[N];
bool check(PII p, Circle c) {
LL dx = p.x - c.x, dy = p.y - c.y;
return dx * dx + dy * dy < (LL)c.r * c.r;
}
void bfs(int u) {
memset(dep, 0x3f, sizeof dep);
dep[u] = 0, dep[0] = 0;
queue<int> q;
q.push(u);
while (q.size()) {
auto t = q.front(); q.pop();
for (int i = h[t]; ~i; i = ne[i] ) {
int j = e[i];
if (dep[j] > dep[t] + 1) {
dep[j] = dep[t] + 1;
q.push(j);
f[j][0] = t;
for (int k = 1; k <= 9; k ++ )
f[j][k] = f[f[j][k - 1]][k - 1];
}
}
}
}
int lca(int a, int b) {
if (dep[a] < dep[b]) return lca(b, a);
for (int i = 9; i >= 0; i -- )
if (dep[f[a][i]] >= dep[b])
a = f[a][i];
if (a == b) return a;
for (int i = 9; i >= 0; i -- )
if (f[a][i] != f[b][i])
a = f[a][i], b = f[b][i];
return f[a][0];
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m >> k; memset(h, -1, sizeof h);
for (int i = 1; i <= n; i ++ ) cin >> a[i].x >> a[i].y;
for (int i = 1; i <= m; i ++ ) cin >> c[i].r >> c[i].x >> c[i].y;
for (int i = 1; i <= n; i ++ ) {
int t = 0;
for (int j = 1; j <= m; j ++ )
if (check(a[i], c[j]))
if (!t || c[t].r > c[j].r)
t = j;
fa[i] = (t ? t : m + 1);
}
for (int i = 1; i <= m; i ++ ) {
int t = 0;
for (int j = 1; j <= m; j ++ )
if (i != j && check(PII(c[i].x, c[i].y), c[j]) && c[i].r < c[j].r)
if (!t || c[t].r > c[j].r)
t = j;
if (!t) t = m + 1;
add(t, i);
}
bfs(m + 1);
while (k --) {
int a, b, p; cin >> a >> b;
a = fa[a], b = fa[b];
p = lca(a, b);
cout << dep[a] + dep[b] - 2 * dep[p] << endl;
}
return 0;
}