Visible Black Areas

题目传送门

求出边与方框的交点,并记录边的朝向,得到出点和入点
方框内的图形在方框上的点一定是入点出点交错
先并查集合并相邻出点-入点,再合并入点-出点 即可
注意边界情况和各种特殊情况

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
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 5;
struct Point {
int x, y;
Point(int _x = 0, int _y = 0): x(_x), y(_y) {}
Point operator + (const Point &b) const {
return Point(x + b.x, y + b.y);
}
Point operator - (const Point &b) const {
return Point(x - b.x, y - b.y);
}
int operator ^ (const Point &b) const {
return (x * b.y - y * b.x);
}
} p[maxn];
Point a, b, c, d, e;
int w, h;
vector<P> f;
bool isPointOnSegment(Point p, Point s, Point e) {
return ((p - s) ^ (s - e)) == 0 && ((p.x - s.x) * (p.x - e.x)) <= 0 && ((p.y - s.y) * (p.y - e.y)) <= 0;
}
void pre_solve(Point u, Point v) {
if(u.x == v.x) {
if(u.x == a.x || u.x == b.x) return;
if(min(u.y, v.y) >= c.y || max(u.y, v.y) <= a.y) return;
if(abs(u.y - a.y) < abs(u.y - d.y)) {
e = Point(u.x, a.y);
if(isPointOnSegment(e, a, b) && isPointOnSegment(e, u, v)) f.push_back(P(e.x - a.x, ((b - a) ^ (v - a)) > 0));
e = Point(u.x, c.y);
if(isPointOnSegment(e, c, d) && isPointOnSegment(e, u, v)) f.push_back(P(w + h + c.x - e.x, ((d - c) ^ (v - c)) > 0));
} else {
e = Point(u.x, c.y);
if(isPointOnSegment(e, c, d) && isPointOnSegment(e, u, v)) f.push_back(P(w + h + c.x - e.x, ((d - c) ^ (v - c)) > 0));
e = Point(u.x, a.y);
if(isPointOnSegment(e, a, b) && isPointOnSegment(e, u, v)) f.push_back(P(e.x - a.x, ((b - a) ^ (v - a)) > 0));
}
} else {
if(u.y == a.y || u.y == d.y) return;
if(min(u.x, v.x) >= c.x || max(u.x, v.x) <= a.x) return;
if(abs(u.x - a.x) < abs(u.x - b.x)) {
e = Point(a.x, u.y);
if(isPointOnSegment(e, a, d) && isPointOnSegment(e, u, v)) f.push_back(P(w + w + h + d.y - e.y, ((a - d) ^ (v - d)) > 0));
e = Point(c.x, u.y);
if(isPointOnSegment(e, b, c) && isPointOnSegment(e, u, v)) f.push_back(P(w + e.y - b.y, ((c - b) ^ (v - b)) > 0));
} else {
e = Point(c.x, u.y);
if(isPointOnSegment(e, b, c) && isPointOnSegment(e, u, v)) f.push_back(P(w + e.y - b.y, ((c - b) ^ (v - b)) > 0));
e = Point(a.x, u.y);
if(isPointOnSegment(e, a, d) && isPointOnSegment(e, u, v)) f.push_back(P(w + w + h + d.y - e.y, ((a - d) ^ (v - d)) > 0));
}
}
}
int inConvexPoly(int n, Point a) {
p[n] = p[0];
for(int i = 0; i < n; i++) {
if(((p[i] - a) ^ (p[i + 1] - a)) < 0) return 0;
else if(isPointOnSegment(a, p[i], p[i + 1])) return 1;
}
return 1;
}
int check(int n) {
int vis = 1;
for(int i = 0; i < n; i++) {
if(a.x <= p[i].x && p[i].x <= c.x && a.y <= p[i].y && p[i].y <= c.y) vis &= 1;
else vis = 0;
}
if(!vis) {
vis = 1;
vis &= inConvexPoly(n, a);
vis &= inConvexPoly(n, b);
vis &= inConvexPoly(n, c);
vis &= inConvexPoly(n, d);
}
return vis;
}
int fp[maxn], tot;
int ff(int x) {
if(fp[x] != x) fp[x] = ff(fp[x]);
return fp[x];
}
void funion(int x, int y) {
x = ff(x);
y = ff(y);
if(x != y) {
fp[x] = y;
tot--;
}
}
int main() {
#ifdef CX_TEST
freopen("E:\\program--GG\\test_in.txt", "r", stdin);
#endif
int n, m, i;
scanf("%d%d%d%d", &a.x, &c.y, &c.x, &a.y);
b = Point(c.x, a. y);
d = Point(a.x, c. y);
w = c.x - a.x;
h = c.y - a.y;
scanf("%d", &n);
for(i = 0; i < n; i++) scanf("%d%d", &p[i].x, &p[i].y);
p[n] = p[0];
for(i = 0; i < n; i++) pre_solve(p[i], p[i + 1]);
tot = m = f.size();
if(m == 0) {
printf("%d\n", check(n));
return 0;
}
for(i = 0; i <= (w + h) * 2; i++) fp[i] = i;
for(i = 1; i < m; i++) {
if(f[i - 1].se == 1 && f[i].se == 0) funion(f[i - 1].fi, f[i].fi);
}
if(f[m - 1].se == 1 && f[0].se == 0) funion(f[m - 1].fi, f[0].fi);
sort(f.begin(), f.end());
f.push_back(f[0]);
for(i = 0; i < m; i++) {
if(f[i].se == 0 && f[i + 1].se == 1) funion(f[i].fi, f[i + 1].fi);
}
printf("%d\n", tot);
return 0;
}