Gym101128J
二分判断点是否在凸包内,模板更新
//Gym - 101128J
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
const double eps = 1e-8;
const double inf = 1e20;
const double pi = acos(-1.0);
const int maxp = 10110;
using namespace std;
int sgn(double x) {
if(fabs(x) < eps) return 0;
if(x < 0) return -1;
else return 1;
}
struct Point {
double x,y;
Point(){}Point(double _x,double _y){x=_x;y=_y;}
void input() {
scanf("%lf%lf",&x,&y);
}
bool operator == (Point b) const{
return sgn(x-b.x) == 0 && sgn(y-b.y) == 0;
}
bool operator < (Point b) const{
return sgn(x-b.x)==0?sgn(y-b.y)<0:x<b.x;
}
Point operator - (const Point &b) const {
return Point(x-b.x,y-b.y);
}
double operator ^ (const Point &b) const {
return x*b.y - y*b.x;
}
double operator * (const Point &b) const {
return x*b.x + y*b.y;
}
Point operator * (const double &k) const {
return Point(x*k,y*k);
}
Point operator / (const double &k) const {
return Point(x/k,y/k);
}
Point operator + (const Point &b) const {
return Point(x+b.x,y+b.y);
}
double len() {
return hypot(x,y);
}
double len2() {
return x*x+y*y;
}
double distance(Point p) {
return hypot(x-p.x,y-p.y);
}
};
struct Line {
Point s,e;
Line(){}Line(Point _s,Point _e){s=_s;e=_e;}
double length(){
return s.distance(e);
}
double dispointtoline(Point p) {
return fabs((p-s)^(e-s))/length();
}
Point lineprog(Point p) {
return s + ( ((e-s)*((e-s)*(p-s)))/(e-s).len2() );
}
int pointseg(Point p) { // update: 点在线段上
return sgn((p-s)^(e-s)) == 0 && min(s.x,e.x) <= p.x && p.x <= max(s.x,e.x) && min(s.y,e.y) <= p.y && p.y <= max(s.y,e.y);
}
};
struct polygon {
int n;
Point p[maxp];
void input(int _n) {
n = _n;
rep(i,0,n-1) p[i].input();
}
struct cmp{
Point p;
cmp(const Point &p0){p=p0;}
bool operator ()(const Point &aa,const Point &bb) {
Point a = aa, b = bb;
int d = sgn((a-p)^(b-p));
if(d == 0) {
return sgn(a.distance(p)-b.distance(p)) < 0;
}
return d > 0;
}
};
void norm() {
Point mi = p[0];
for(int i=1;i<n;++i) mi = min(mi,p[i]);
sort(p,p+n,cmp(mi));
}
void Graham(polygon &convex) {
norm();
int &top = convex.n;
top = 0;
if(n == 1) {
top = 1;
convex.p[0] = p[0];
return;
}
if(n == 2) {
top = 2;
convex.p[0] = p[0];
convex.p[1] = p[1];
if(convex.p[0] == convex.p[1]) --top;
return;
}
convex.p[0] = p[0];
convex.p[1] = p[1];
top = 2;
rep(i,2,n-1) {
while(top > 1 && sgn((convex.p[top-1]-convex.p[top-2])^(p[i]-convex.p[top-2])) <= 0)
--top;
convex.p[top++] = p[i];
}
if(convex.n == 2 && (convex.p[0]==convex.p[1]))convex.n--;
}
double getarea() {
double sum = 0;
rep(i,0,n-1)
sum += (p[i]^p[(i+1)%n]);
return fabs(sum)*0.5;
}
int inconvex(Point s) { //update: 逆时针凸包 边界返回2, 内部返回1,外部返回0
Point p0 = p[0];
Line l1(p0,p[1]),l2(p0,p[n-1]);
if(l1.pointseg(s) || l2.pointseg(s)) return 2;
int l = 1, r = n - 2;
while(l <= r) {
int mid = (l+r)/2;
int t1 = sgn((s-p0)^(p[mid]-p0));
int t2 = sgn((s-p0)^(p[mid+1]-p0));
if( t1 <= 0 && t2>=0 ) {
int t3 = sgn((s-p[mid])^(p[mid+1]-p[mid]));
if(t3 < 0) return 1; // 在内部
else if(t3 == 0) return 2; //在边上
return 0;
}
if(t1 > 0) r = mid-1;
else l = mid+1;
}
return 0;
}
};
int n,q,ans;
polygon P,T;
int main() {
scanf("%d",&n);
P.input(n);
P.Graham(T);
scanf("%d",&q);
while(q--) {
Point s;
s.input();
ans += !!(T.inconvex(s));
}
printf("%d\n",ans);
return 0;
}