-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathperfume.cpp
116 lines (102 loc) · 2.65 KB
/
perfume.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
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int N = 207;
const double eps = 1e-9;
struct point {
double x, y;
point () {}
point (double x, double y):x(x), y(y) {}
point operator+ (const point &o) {
return point(x+o.x, y+o.y);
}
point operator- (const point &o) {
return point(x-o.x, y-o.y);
}
double operator* (const point &o) {
return x*o.x + y*o.y;
}
double operator^ (const point &o) {
return x*o.y - o.x*y;
}
bool operator< (const point &o) const {
if(x == o.x) return y < o.y + eps;
return x < o.x + eps;
}
bool operator== (const point &o) const {
return fabs(x-o.x) < eps && fabs(y-o.y) < eps;
}
};
int t, n, ps, q, cs;
point p[N];
point hull[N];
double xin, yin;
int sgn (double x) {
return (x > -eps) - (x < eps);
}
int orient (point a, point b, point c) {
return sgn((b-a)^(c-a));
}
double norm(point a) {
return sqrt(a.x*a.x + a.y*a.y);
}
int cvx_hull(){
ps = 0;
hull[ps++] = p[0];
if(n == 1) return ps;
hull[ps++] = p[1];
if(n == 2) return ps;
int ls = ps;
for(int i = 2; i < n; i++) {
while(ps >= ls && orient(hull[ps-2],hull[ps-1],p[i]) != 1) ps--;
hull[ps++] = p[i];
}
hull[ps++] = p[n-2];
ls = ps;
for(int i = n-3; i >= 0; i--) {
while(ps >= ls && orient(hull[ps-2],hull[ps-1],p[i]) != 1) ps--;
hull[ps++] = p[i];
}
return --ps;
}
bool insidesegment(point a, point b, point q) {
return fabs(norm(q-a)+norm(q-b)-norm(b-a)) < eps;
}
bool insidetriangle(point a, point b, point c, point q) {
if(insidesegment(a,b,q)) return true;
if(insidesegment(b,c,q)) return true;
if(insidesegment(a,c,q)) return true;
int s = sgn((b-a)^(q-a));
if(sgn((c-b)^(q-b)) != s) return false;
if(sgn((a-c)^(q-c)) != s) return false;
return true;
}
bool inside(point q) {
if(cs == 1) return q == hull[0];
if(cs == 2) return insidesegment(hull[0],hull[1],q);
for(int i = 1; i < cs-1; i++)
if(insidetriangle(hull[0],hull[i],hull[i+1],q)) return true;
return false;
}
int main() {
scanf("%d",&t);
bool blank = false;
for(int tc = 1; tc <= t; tc++) {
if(blank) putchar('\n');
blank = true;
scanf("%d",&n);
for(int i = 0; i < n; i++) {
scanf("%lf %lf",&xin,&yin);
p[i] = point(xin, yin);
}
sort(p,p+n);
cs = cvx_hull();
scanf("%d",&q);
while(q--) {
scanf("%lf %lf",&xin,&yin);
if(inside(point(xin,yin))) puts("Yes");
else puts("No");
}
}
}