惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
V
V2EX
爱范儿
爱范儿
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
罗磊的独立博客
小众软件
小众软件
Vercel News
Vercel News
博客园 - 司徒正美
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
J
Java Code Geeks
P
Proofpoint News Feed
MongoDB | Blog
MongoDB | Blog
B
Blog
美团技术团队
量子位

Tifa's Blog

随笔 - 关于 C++ 模板的部分特化 随笔 - Miller-Rabin + Pollard-Rho 分解质因子的时间复杂度分析 随笔 - 批量重命名 APK 文件的 Python 脚本 VP 记录 - 2021 CCPC 哈尔滨站 VP 记录 - 2023 ICPC 亚洲区域赛 (南京) VP 记录 - 2023 CCPC 哈尔滨站 VP 记录 - 2023 CCPC 桂林站 VP 记录 - 2021 ICPC 亚洲区域赛 (澳门) 题解 - [Luogu P7486] 「Stoi2031」彩虹 拟阵简介(unfin) 题解 - [Luogu P5824] 十二重计数法 随笔 - C++ 的高维向量 | Tifa's Blog 目录 - 算法竞赛模板 | Tifa's Blog 目录 - 学术垃圾 | Tifa's Blog 比赛记录 - Codeforces Round #842 (Div. 2) 比赛记录 - Hello 2023 | Tifa's Blog 比赛记录 - Codeforces Round #841 (Div. 2) and Divide by Zero 2022 随笔 - C++ 基于标签分发的线性筛 | Tifa's Blog VP 记录 - 2022 ICPC 亚洲区域赛 (杭州) VP 记录 - 2022 ICPC 亚洲区域赛 (济南)
VP 记录 - 2023 ICPC 亚洲区域赛 (网络预选赛 Ⅰ)
Tifa · 2023-10-08 · via Tifa's Blog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using i64 = ll;
using ull = unsigned long long;
using u64 = ull;
using ldb = long double;

#define for_(i, l, r) for (ll i = (l), i##ed__ = (r); i <= i##ed__; ++i)
#define Rep(i, l, r) for_(i, l, r)

constexpr ldb eps = 1e-6;

struct P {
ldb x, y;

explicit P(ldb x = 0, ldb y = 0): x(x), y(y) {}

bool operator==(const P &r) const {
return abs(x - r.x) < eps && abs(y - r.y) < eps;
}
P operator-(const P &r) const { return P{x - r.x, y - r.y}; }
P operator*(ldb d) const { return P{x * d, y * d}; }
ldb dist2() const { return x * x + y * y; }
ldb dot(const P &r) const { return x * r.x + y * r.y; }
ldb cross(const P &r) const { return x * r.y - y * r.x; }
ldb cross(const P &a, const P &b) const {
return (a - *this).cross(b - *this);
}
};

ldb segdist2(P const &s, P const &e, P const &p) {
if (s == e) return (p - s).dist2();
ldb d = (e - s).dist2(), t = min(d, max(0.l, (p - s).dot(e - s)));
return ((p - s) * d - (e - s) * t).dist2() * 1.l / d / d;
}

bool on_seg(P const &s, P const &e, P const &p) {
return segdist2(s, e, p) < eps;
}

int nxt(int x, int n) { return (++x) == n ? 0 : x; };
bool in_poly(vector<P> const &p, P const &a) {
int cnt = 0, n = p.size();
for_(i, 0, n - 1) {
P const &q = p[nxt(i, n)];
if (on_seg(p[i], q, a)) return true;
cnt ^= ((a.y < p[i].y) - (a.y < q.y)) * a.cross(p[i], q) > 0;
}
return cnt;
}

void solve() {
int n, q;
cin >> n >> q;

vector<P> poly(n);
for (auto &[x, y] : poly) cin >> x >> y;

for_(i, 1, q) {
P p1, p2;
cin >> p1.x >> p1.y >> p2.x >> p2.y;
P c{(p1.x + p2.x) / 2, (p1.y + p2.y) / 2};
ldb r2 = (p1 - p2).dist2() / 4;
if (in_poly(poly, c)) {
cout << r2 / 2 << '\n';
continue;
}
ldb d = 1e4514l;
for_(i, 0, n - 1) d = min(d, segdist2(poly[i], poly[nxt(i, n)], c));
cout << d + r2 / 2 << '\n';
}
}

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(10);
#ifdef MULCAS
int t = 0;
cin >> t;
for (int i = 0; i < t; ++i)
#endif
solve();
return 0;
}