分治 + 计算几何 + 闵可夫斯基和:ABC478G

G - Division Point Hull

image-20261004105700885


考虑用坐标点来表示 Qi,jQ_{i,j}:

Qi,j=qp+qPi+pp+qPjQ_{i,j}=\frac q{p+q}P_i+\frac p {p + q}P_j

我们考虑分治来处理。先递归得到左右两边 QQ 组成的凸包 QL,QRQ_L,Q_R

我们现在需要得到跨区间 QQ。根据上面的式子,我们发现其实我们只需要得到左右两边 PP 组成的凸包,然后左边缩小到 qp+q\dfrac q{p+q},右边缩小到 pp+q\dfrac p{p+q},然后做一个闵可夫斯基和合并即可。

时间复杂度 O(nlog⁡2n)O(n\log^2 n),当然如果实现比较好可以做到 O(nlog⁡n)O(n\log n).

还有,最后要输出有理数形式。我们可以一开始对所有 PP 先乘个 p+qp+q,然后最后凸包的面积不除2就行再约分。面积记得除 (p+q)2(p+q)^2。然后计算几何的板子改成long double就能过了。

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include<bits/stdc++.h>
#include<cassert>
using namespace std;bool Mbe;
#ifdef LOCAL
#define debug(...) fprintf(stdout, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#endif
namespace Geometry {
#define rep(i, l, r) for(int i = (l); i < (r); ++i)
#define pb push_back
typedef long double db;
const db EPS = 1e-9;
//由于硬件限制,浮点数运算有误差,eps用来消除误差
inline int sign(db a) {
return a < -EPS ? -1 : a > EPS;
}
//判断数符号,负数返回-1,0返回0,正数返回1
inline int cmp(db a, db b) {
return sign(a - b);
}
//比较两数大小
//点类,向量类
//因为有许多操作相似,所以并在一起
struct P {
db x, y;
//点表示坐标,向量表示向量
P() {}
P(db _x, db _y) : x(_x), y(_y) {}
//构造函数
P operator+(P p) {
return {x + p.x, y + p.y};
}
P operator-(P p) {
return {x - p.x, y - p.y};
}
P operator*(db d) {
return {x * d, y * d};
}
P operator/(db d) {
return {x / d, y / d};
}
//向量加减乘除
bool operator<(P p) const {
int c = cmp(x, p.x);
if (c)
return c == -1;
return cmp(y, p.y) == -1;
}
bool operator==(P o) const {
return cmp(x, o.x) == 0 && cmp(y, o.y) == 0;
}
//比较字典序
db dot(P p) {
return x * p.x + y * p.y;
}
//点积
db det(P p) {
return x * p.y - y * p.x;
}
//叉积
db distTo(P p) {
return (*this - p).abs();
}
//点距离
db alpha() {
return atan2(y, x);
}
void read() {
cin >> x >> y;
}
void write() {
cout << "(" << x << "," << y << ") ";
}
db abs() {
return sqrt(abs2());
}
db abs2() {
return x * x + y * y;
}
P rot90() {
return P(-y, x);
}
P unit() {
return *this / abs();
}
int quad() const {
return sign(y) == 1 || (sign(y) == 0 && sign(x) >= 0);
}
//判断点在极角坐标系上半边还是下半边,极点和极轴也算上半边
P rot(db an) {
return {x * cos(an) - y * sin(an), x * sin(an) + y * cos(an)};
}
//向量旋转
};
//线类,半平面类
struct L {
// ps[0] -> ps[1]
P ps[2];
P &operator[](int i) {
return ps[i];
}
P dir() {
return ps[1] - ps[0];
}
L(P a, P b) {
ps[0] = a;
ps[1] = b;
}
bool include(P p) {
return sign((ps[1] - ps[0]).det(p - ps[0])) > 0;
}
L push() {
// push eps outward
const long double eps = 1e-8;
P delta = (ps[1] - ps[0]).rot90().unit() * eps;
return {ps[0] + delta, ps[1] + delta};
}
};

#define cross(p1, p2, p3) ((p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y))
#define crossOp(p1, p2, p3) sign(cross(p1, p2, p3))
//叉积,可以用来求三角形面积(输入参数是三个点)
bool chkLL(P p1, P p2, P q1, P q2) {
db a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2);
return sign(a1 + a2) != 0;
}
//判断向量平行
P isLL(P p1, P p2, P q1, P q2) {
db a1 = cross(q1, q2, p1), a2 = -cross(q1, q2, p2);
return (p1 * a2 + p2 * a1) / (a1 + a2);
}
P isLL(L l1, L l2) {
return isLL(l1[0], l1[1], l2[0], l2[1]);
}
//求直线交点
bool intersect(db l1, db r1, db l2, db r2) {
if (l1 > r1)
swap(l1, r1);
if (l2 > r2)
swap(l2, r2);
return !(cmp(r1, l2) == -1 || cmp(r2, l1) == -1);
}
bool isSS(P p1, P p2, P q1, P q2) {
return intersect(p1.x, p2.x, q1.x, q2.x) && intersect(p1.y, p2.y, q1.y, q2.y) &&
crossOp(p1, p2, q1) * crossOp(p1, p2, q2) <= 0 && crossOp(q1, q2, p1) * crossOp(q1, q2, p2) <= 0;
}

bool isSS_strict(P p1, P p2, P q1, P q2) {
return crossOp(p1, p2, q1) * crossOp(p1, p2, q2) < 0 && crossOp(q1, q2, p1) * crossOp(q1, q2, p2) < 0;
}
//判断线段相交,交在端点算不算分为严格不严格
bool isMiddle(db a, db m, db b) {
return sign(a - m) == 0 || sign(b - m) == 0 || (a < m != b < m);
}
bool isMiddle(P a, P m, P b) {
return isMiddle(a.x, m.x, b.x) && isMiddle(a.y, m.y, b.y);
}
bool onSeg(P p1, P p2, P q) {
return crossOp(p1, p2, q) == 0 && isMiddle(p1, q, p2);
}
bool onSeg_strict(P p1, P p2, P q) {
return crossOp(p1, p2, q) == 0 && sign((q - p1).dot(p1 - p2)) * sign((q - p2).dot(p1 - p2)) < 0;
}
//点在线段上判定
P proj(P p1, P p2, P q) {
P dir = p2 - p1;
return p1 + dir * (dir.dot(q - p1) / dir.abs2());
}
P reflect(P p1, P p2, P q) {
return proj(p1, p2, q) * 2 - q;
}
db nearest(P p1, P p2, P q) {
if (p1 == p2)
return p1.distTo(q);
P h = proj(p1, p2, q);
if (isMiddle(p1, h, p2))
return q.distTo(h);
return min(p1.distTo(q), p2.distTo(q));
}
//投影,反射,最近点
//最近点是线段外一点到线段上的点的最短距离
db disSS(P p1, P p2, P q1, P q2) {
if (isSS(p1, p2, q1, q2))
return 0;
return min(min(nearest(p1, p2, q1), nearest(p1, p2, q2)), min(nearest(q1, q2, p1), nearest(q1, q2, p2)));
}
//线段距离
db rad(P p1, P p2) {
return atan2l(p1.det(p2), p1.dot(p2));
}

db incircle(P p1, P p2, P p3) {
db A = p1.distTo(p2);
db B = p2.distTo(p3);
db C = p3.distTo(p1);
return sqrtl(A * B * C / (A + B + C));
}

// polygon
//简单多边形的问题只有判断点在多边形内,和多边形面积简单,其他只做凸多边形
db area(vector<P> ps) {
db ret = 0;
for(int i=0; i<ps.size(); ++i)
ret += ps[i].det(ps[(i + 1) % ps.size()]);
return ret;
}
//多边形面积
int contain(vector<P> ps, P p) {
// 2:inside,1:on_seg,0:outside
int n = ps.size(), ret = 0;
rep(i, 0, n) {
P u = ps[i], v = ps[(i + 1) % n];
if (onSeg(u, v, p))
return 1;
if (cmp(u.y, v.y) <= 0)
swap(u, v);
if (cmp(p.y, u.y) > 0 || cmp(p.y, v.y) <= 0)
continue;
ret ^= crossOp(p, u, v) > 0;
}
return ret * 2;
}
//判断点在多边形内
vector<P> convexHull(vector<P> ps) {
int n = ps.size();
if (n <= 1)
return ps;
sort(ps.begin(), ps.end());
vector<P> qs(n * 2);
int k = 0;
for (int i = 0; i < n; qs[k++] = ps[i++])
while (k > 1 && crossOp(qs[k - 2], qs[k - 1], ps[i]) <= 0)
--k;
for (int i = n - 2, t = k; i >= 0; qs[k++] = ps[i--])
while (k > t && crossOp(qs[k - 2], qs[k - 1], ps[i]) <= 0)
--k;
qs.resize(k - 1);
return qs;
}
vector<P> convexHullNonStrict(vector<P> ps) {
// caution: need to unique the Ps first
int n = ps.size();
if (n <= 1)
return ps;
sort(ps.begin(), ps.end());
vector<P> qs(n * 2);
int k = 0;
for (int i = 0; i < n; qs[k++] = ps[i++])
while (k > 1 && crossOp(qs[k - 2], qs[k - 1], ps[i]) < 0)
--k;
for (int i = n - 2, t = k; i >= 0; qs[k++] = ps[i--])
while (k > t && crossOp(qs[k - 2], qs[k - 1], ps[i]) < 0)
--k;
qs.resize(k - 1);
return qs;
}
//凸包

db convexDiameter(vector<P> ps) {
int n = ps.size();
if (n <= 1)
return 0;
int is = 0, js = 0;
rep(k, 1, n) is = ps[k] < ps[is] ? k : is, js = ps[js] < ps[k] ? k : js;
int i = is, j = js;
db ret = ps[i].distTo(ps[j]);
do {
if ((ps[(i + 1) % n] - ps[i]).det(ps[(j + 1) % n] - ps[j]) >= 0)
(++j) %= n;
else
(++i) %= n;
ret = max(ret, ps[i].distTo(ps[j]));
} while (i != is || j != js);
return ret;
}
//凸包直径
vector<P> convexCut(const vector<P> &ps, P q1, P q2) {
vector<P> qs;
int n = ps.size();
rep(i, 0, n) {
P p1 = ps[i], p2 = ps[(i + 1) % n];
int d1 = crossOp(q1, q2, p1), d2 = crossOp(q1, q2, p2);
if (d1 >= 0)
qs.pb(p1);
if (d1 * d2 < 0)
qs.pb(isLL(p1, p2, q1, q2));
}
return qs;
}
//直线切割凸包,返回直线左边凸包的点
db min_dist(vector<P> &ps, int l, int r) {
if (r - l <= 5) {
db ret = 1e18;
for(int i=l; i<r; ++i)
for(int j=l; j<i; ++j)
ret = min(ret, ps[i].distTo(ps[j]));
return ret;
}
int m = (l + r) >> 1;
db ret = min(min_dist(ps, l, m), min_dist(ps, m, r));
vector<P> qs;
for(int i=l; i<r; ++i)
if (abs(ps[i].x - ps[m].x) <= ret)
qs.push_back(ps[i]);
sort(qs.begin(), qs.end(), [](P a, P b) -> bool
{ return a.y < b.y; });
for(int i=1; i<qs.size(); ++i)
for (int j = i - 1; j >= 0 && qs[j].y >= qs[i].y - ret; --j)
ret = min(ret, qs[i].distTo(qs[j]));
return ret;
}
//平面最近点对,[l,r),要求ps按x升序
int type(P o1, db r1, P o2, db r2) {
db d = o1.distTo(o2);
if (cmp(d, r1 + r2) == 1)
return 4;
if (cmp(d, r1 + r2) == 0)
return 3;
if (cmp(d, abs(r1 - r2)) == 1)
return 2;
if (cmp(d, abs(r1 - r2)) == 0)
return 1;
return 0;
}

vector<P> isCL(P o, db r, P p1, P p2) {
if (cmp(abs((o - p1).det(p2 - p1) / p1.distTo(p2)), r) > 0)
return {};
db x = (p1 - o).dot(p2 - p1), y = (p2 - p1).abs2(), d = x * x - y * ((p1 - o).abs2() - r * r);
d = max(d, (db)0.0);
P m = p1 - (p2 - p1) * (x / y), dr = (p2 - p1) * (sqrt(d) / y);
return {m - dr, m + dr}; // along dir: p1->p2
}

vector<P> isCC(P o1, db r1, P o2, db r2) {
// need to check whether two circles are the same
db d = o1.distTo(o2);
if (cmp(d, r1 + r2) == 1)
return {};
if (cmp(d, abs(r1 - r2)) == -1)
return {};
d = min(d, r1 + r2);
db y = (r1 * r1 + d * d - r2 * r2) / (2 * d), x = sqrt(r1 * r1 - y * y);
P dr = (o2 - o1).unit();
P q1 = o1 + dr * y, q2 = dr.rot90() * x;
return {q1 - q2, q1 + q2}; // along circle 1
}

vector<P> tanCP(P o, db r, P p) {
db x = (p - o).abs2(), d = x - r * r;
if (sign(d) <= 0)
return {}; // on circle => no tangent
P q1 = o + (p - o) * (r * r / x);
P q2 = (p - o).rot90() * (r * sqrt(d) / x);
return {q1 - q2, q1 + q2}; // counter clock-wise
}

vector<L> extanCC(P o1, db r1, P o2, db r2) {
vector<L> ret;
if (cmp(r1, r2) == 0) {
P dr = (o2 - o1).unit().rot90() * r1;
ret.pb(L(o1 + dr, o2 + dr)), ret.pb(L(o1 - dr, o2 - dr));
} else {
P p = (o2 * r1 - o1 * r2) / (r1 - r2);
vector<P> ps = tanCP(o1, r1, p), qs = tanCP(o2, r2, p);
rep(i, 0, min(ps.size(), qs.size())) ret.pb(L(ps[i], qs[i])); // c1 counter-clock wise
}
return ret;
}

vector<L> intanCC(P o1, db r1, P o2, db r2) {
vector<L> ret;
P p = (o1 * r2 + o2 * r1) / (r1 + r2);
vector<P> ps = tanCP(o1, r1, p), qs = tanCP(o2, r2, p);
rep(i, 0, min(ps.size(), qs.size())) ret.pb(L(ps[i], qs[i])); // c1 counter-clock wise
return ret;
}

db areaCT(db r, P p1, P p2) {
vector<P> is = isCL(P(0, 0), r, p1, p2);
if (is.empty())
return r * r * rad(p1, p2) / 2;
bool b1 = cmp(p1.abs2(), r * r) == 1, b2 = cmp(p2.abs2(), r * r) == 1;
if (b1 && b2) {
if (sign((p1 - is[0]).dot(p2 - is[0])) <= 0 &&
sign((p1 - is[0]).dot(p2 - is[0])) <= 0)
return r * r * (rad(p1, is[0]) + rad(is[1], p2)) / 2 + is[0].det(is[1]) / 2;
else
return r * r * rad(p1, p2) / 2;
}
if (b1)
return (r * r * rad(p1, is[0]) + is[0].det(p2)) / 2;
if (b2)
return (p1.det(is[1]) + r * r * rad(is[1], p2)) / 2;
return p1.det(p2) / 2;
}

bool parallel(L l0, L l1) {
return sign(l0.dir().det(l1.dir())) == 0;
}
bool cmp(P a, P b) {
if (a.quad() != b.quad()) {
return a.quad() < b.quad();
} else {
return sign(a.det(b)) > 0;
}
}
//极角排序
bool sameDir(L l0, L l1) {
return parallel(l0, l1) && sign(l0.dir().dot(l1.dir())) == 1;
}
bool operator<(L l0, L l1) {
if (sameDir(l0, l1)) {
return l1.include(l0[0]);
} else {
return cmp(l0.dir(), l1.dir());
}
}
bool check(L u, L v, L w) {
return w.include(isLL(u, v));
}
vector<P> halfPlaneIS(vector<L> &l) {
sort(l.begin(), l.end());
deque<L> q;
for (int i = 0; i < (int)l.size(); ++i) {
if (i && sameDir(l[i], l[i - 1]))
continue;
while (q.size() > 1 && !check(q[q.size() - 2], q[q.size() - 1], l[i]))
q.pop_back();
while (q.size() > 1 && !check(q[1], q[0], l[i]))
q.pop_front();
q.push_back(l[i]);
}
while (q.size() > 2 && !check(q[q.size() - 2], q[q.size() - 1], q[0]))
q.pop_back();
while (q.size() > 2 && !check(q[1], q[0], q[q.size() - 1]))
q.pop_front();
vector<P> ret;
for (int i = 0; i < (int)q.size(); ++i)
ret.push_back(isLL(q[i], q[(i + 1) % q.size()]));
return ret;
}
//半平面交
P inCenter(P A, P B, P C) {
long double a = (B - C).abs(), b = (C - A).abs(), c = (A - B).abs();
return (A * a + B * b + C * c) / (a + b + c);
}
//内心,角平分线的交点
P circumCenter(P a, P b, P c) {
P bb = b - a, cc = c - a;
long double db = bb.abs2(), dc = cc.abs2(), d = 2 * bb.det(cc);
return a - P(bb.y * dc - cc.y * db, cc.x * db - bb.x * dc) / d;
}
//外心,垂直平分线的交点
P orthoCenter(P a, P b, P c) {
P ba = b - a, ca = c - a, bc = b - c;
long double Y = ba.y * ca.y * bc.y,
A = ca.x * ba.y - ba.x * ca.y,
x0 = (Y + ca.x * ba.y * b.x - ba.x * ca.y * c.x) / A,
y0 = -ba.x * (x0 - c.x) / ba.y + ca.y;
return {x0, y0};
}
//垂心,垂线的交点
// 闵可夫斯基和
vector<P> minkowskiSum(vector<P> A, vector<P> B) {
// 1. 求凸包(dls 的 convexHull 返回不重复起点,从最左下点开始的逆时针凸包)
A = convexHull(A);
B = convexHull(B);
if (A.empty() || B.empty()) return {};

// 2. 退化情况:点或线段,直接暴力枚举所有组合
if (A.size() <= 2 || B.size() <= 2) {
vector<P> all;
all.reserve(A.size() * B.size());
for (auto &a : A)
for (auto &b : B)
all.push_back(a + b);
return convexHull(all);
}

// 3. 非退化:确保逆时针(dls 的 convexHull 已保证,这里防御性处理)
if (area(A) < 0) reverse(A.begin(), A.end());
if (area(B) < 0) reverse(B.begin(), B.end());

// 4. 找到各自字典序最小的点(最左下),并旋转到开头
// 这是 cp-algorithms 算法的关键步骤,确保边向量序列按极角有序
int ia = 0, ib = 0;
for (int i = 1; i < A.size(); ++i) if (A[i] < A[ia]) ia = i;
for (int i = 1; i < B.size(); ++i) if (B[i] < B[ib]) ib = i;
rotate(A.begin(), A.begin() + ia, A.end());
rotate(B.begin(), B.begin() + ib, B.end());

// 5. 首尾相连,使边向量序列完整
int n = A.size(), m = B.size();
A.push_back(A[0]);
A.push_back(A[1]);
B.push_back(B[0]);
B.push_back(B[1]);

// 6. 线性合并两个边向量序列(cp-algorithms 的核心逻辑)
vector<P> res;
res.reserve(n + m + 1);
int i = 0, j = 0;
while (i < n || j < m) {
res.push_back(A[i] + B[j]);
if (i == n) { j++; continue; }
if (j == m) { i++; continue; }
db cr = (A[i + 1] - A[i]).det(B[j + 1] - B[j]);
if (cr >= 0) i++;
if (cr <= 0) j++;
}

// 7. 去掉首尾相连产生的重复起点,并去共线点
return convexHull(res);
}
}
using namespace Geometry;

#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
//#define M
//#define mo
#define N 200010
int n, m, i, j, k, T;
int p, q;
P a[N];

vector<P> solve(int l, int r) {
if(l == r) return {};
int mid = (l + r) >> 1, i;
auto Q1 = solve(l, mid);
auto Q2 = solve(mid + 1, r);
vector<P>P1, P2;
for(i = l; i <= mid; ++i) P1.pb(a[i] * q / (p + q));
for(i = mid + 1; i <= r; ++i) P2.pb(a[i] * p / (p + q));
// debug("For [%d %d]\n", l, r);
// for(auto t : P1) t = t / (p + q), t.write();
// for(auto t : P2) t = t / (p + q), t.write();
auto Q3 = minkowskiSum(P1, P2);
// debug("----\n");
// for(auto t : Q3) t = t / (p + q), t.write();
for(auto t : Q1) Q3.pb(t);
for(auto t : Q2) Q3.pb(t);
return convexHull(Q3);
}

bool Med; signed main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
atexit([]{fprintf(stderr,"%.lfms\n%lfMB\n",clock()*1000./CLOCKS_PER_SEC,(&Mbe-&Med)/1024./1024);});
#endif
// srand(time(NULL));
// T = read();
// while(T--) {
//
// }
n = read(); p = read(); q = read();
for(i = 1; i <= n; ++i) a[i].read(), a[i] = a[i] * (p + q);
auto t = solve(1, n);
// debug("\n");
int x = area(t);
int y = 2 * Z(p + q);
int z = __gcd(x, y);
printf("%lld %lld", x / z, y / z);
return 0;
}