-
Notifications
You must be signed in to change notification settings - Fork 2
/
HULL.cpp
147 lines (131 loc) · 3.44 KB
/
HULL.cpp
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
#include <cstdio>
#include <cmath>
#include <algorithm>
#define MAX 100000
#define A first
#define B second
using namespace std;
typedef long double ld;
typedef pair<ld,ld> pii;
int hull[MAX];
ld angle[MAX];
int s = 0, e = 0;
int red = 0;
int blue = 0;
ld left,right,up,down;
pii temp[MAX];
pii pnt[MAX];
pii other[MAX];
int num;
const ld PI = 3.14159265358979;
const ld LARGE = 50000;
pii mid;
ld A(pii in){ return atan2(in.B-mid.B,in.A-mid.A);} // return the A this point makes with middle
int comp(pii one,pii two){ return A(one)<A(two);} //comparator
void swap(){
for(int x = 0;x<num;x++){
temp[x] = pnt[x];
pnt[x] = other[x];
other[x] = temp[x];
}
}
ld cross(pii one, pii two){
return one.A*two.B-one.B*two.A;
}
ld cross(int one, int two, int thr){ return (pnt[two].A-pnt[one].A)*(pnt[thr].B-pnt[one].B)-(pnt[two].B-pnt[one].B)*(pnt[thr].A-pnt[one].A);}
//cross product
//negative if right turn
//positive if left turn
//return 0 if collinear
void build(){// built the hull
right = -LARGE;
left = LARGE;
up = -LARGE;
down = LARGE;
mid = pii(0,0);
for(int x = 0;x<num;x++){
mid.A+=pnt[x].A;
mid.B+=pnt[x].B;
}
mid.A/=num;
mid.B/=num;
sort(pnt,pnt+num,comp);
hull[0] = 0;
hull[1] = 1;
e = 2;
for(int x = 2;x<num;x++){
while(e>s+1&&cross(hull[e-2],hull[e-1],x)<=0) e--;
hull[e++] = x;
}
bool flag = true;
while(flag){
flag = false;
while(e>s+1&&cross(hull[e-2],hull[e-1],hull[s])<=0){
e--;
flag = true;
}
while(e>s+1&&cross(hull[e-1],hull[s],hull[s+1])<=0){
s++;
flag = true;
}
}
for(int x = s;x<e;x++){
angle[x] = A(pnt[hull[x]]);
}
for(int x = 0;x<num;x++){
if(pnt[x].A>right) right = pnt[x].A;
if(pnt[x].A<left) left = pnt[x].A;
if(pnt[x].B>up) up = pnt[x].B;
if(pnt[x].B<down) down = pnt[x].B;
}
hull[e] = hull[s];//wrap around
angle[e] = angle[s]+2*PI;
for(int x = s;x<e;x++){
// printf("%Lf %Lf\n",pnt[hull[x]].A,pnt[hull[x]].B);
}
// printf("%Lf %Lf %Lf %Lf\n",left,right,up,down);
}
bool same(pii one, pii two, pii first, pii sec){
pii BA = pii(sec.A-first.A,sec.B-first.B);
pii CA = pii(one.A-first.A,one.B-first.B);
pii DA = pii(two.A-first.A,two.B-first.B);
return cross(BA,CA)*cross(BA,DA)>=0;
}
bool query(pii cur){//check if point is within hull
if(cur.A>right||cur.A<left||cur.B>up||cur.B<down) return false;
ld ang = A(cur);
if(ang<angle[s]) ang+=2*PI;
int bot = s;
int top = e;
while(bot<top-1){
int mid = (bot+top)/2;
if(angle[mid]>ang) top = mid;
else bot = mid;
}
if(ang<angle[bot]||ang>angle[top]||bot+1!=top) return false;
// printf("%Lf %Lf %Lf\n",angle[bot],ang,angle[top]);
return same(mid,cur,pnt[hull[bot]],pnt[hull[top]]);
}
void print(pii in){
printf("%Lf %Lf\n",in.A,in.B);
}
int main(){
freopen("curling.in","r",stdin);
freopen("curling.out","w",stdout);
scanf("%d",&num);
int a,b;
for(int x = 0;x<num;x++){
scanf("%d%d",&a,&b);
pnt[x] = pii(a,b);
}
for(int x = 0;x<num;x++){
scanf("%d%d",&a,&b);
other[x] = pii(a,b);
}
build();
for(int x = 0;x<num;x++) if(query(other[x])) red++;
swap();
build();
for(int x = 0;x<num;x++) if(query(other[x])) blue++;
printf("%d %d\n",red,blue);
}