CF70D Professor’s task

省略号部分见 二维计算几何模板

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
struct Point {
double x, y, ang;
// ...
inline bool operator<(const Point sec) const {
return ang < sec.ang;
}
};
// ...

typedef set<Point>::iterator it;

set<Point> s;
inline it Prec(it x) {
return x == begin(s) ? --end(s) : --x;
}
inline it Nex(it x) {
return (++x) == end(s) ? begin(s) : x;
}
void Solve(Point p) {
pair<it, bool> pr = s.insert(p);
if (pr.second == 0) return;
if (s.size() <= 3) return;
it cur = pr.first;
it prec = Prec(cur), nex = Nex(cur);
if (sig((*cur - *prec) % (*nex - *cur)) <= 0) {
s.erase(cur);
return;
}
it i = Prec(cur), j = Prec(i);
while (s.size() > 3 && sig((*i - *j) % (*cur - *i)) <= 0) {
s.erase(i);
i = j; j = Prec(j);
}
i = Nex(cur), j = Nex(i);
while (s.size() > 3 && sig((*i - *cur) % (*j - *i)) <= 0) {
s.erase(i);
i = j; j = Nex(j);
}
}
bool Query(Point p) {
it nex = s.lower_bound(p);
if (nex == end(s)) nex = begin(s);
it prec = Prec(nex);
return sig((p - *prec) % (*nex - p)) <= 0;
}

int n;
signed main() {
read(n);
Point o{x: PI / 100.0, y: SQRT2 / 100.0, ang: 0.0}, ori[4];
for (int i = 1, opt, x, y; i <= 3; ++i) {
read(opt, x, y);
o.x += x; o.y += y;
ori[i].x = x * 3; ori[i].y = y * 3;
}
for (int i = 1; i <= 3; ++i) {
ori[i].ang = angle(o, ori[i]);
Solve(ori[i]);
}
Point p;
for (int i = 4, opt, x, y; i <= n; ++i) {
read(opt, x, y);
p.x = x * 3; p.y = y * 3;
p.ang = angle(o, p);
if (opt == 1) {
Solve(p);
} else {
puts(Query(p) ? "YES" : "NO");
}
}
return 0;
}

Point o{x: PI / 100.0, y: SQRT2 / 100.0, ang: 0.0} 这里使用了原点偏移法,给了原点一个无理数增量,这是为了避免极角相等的点的情况,如果不加这句可能会 W ⁣A{\color{red}\mathrm{W\!A}},还有一种不建议使用的避免 W ⁣A{\color{red}\mathrm{W\!A}} 的方法是增加排序关键字法,将代码第四行的函数改为:

1
2
3
4
5
6
inline bool operator<(const Point sec) const {
if (!cmp(ang, sec.ang)) {
return x < sec.x;
}
return ang < sec.ang;
}