代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#define MAXN 210000
#define EPS 1e-8
using namespace std;
typedef long long int LL;
struct Point
{
LL x,y;
Point(){}
Point(double _x,double _y):x(_x),y(_y){}
}polygon[MAXN];
bool operator==(Point a,Point b)
{
return a.x==b.x&&a.y==b.y;
}
Point operator-(Point a,Point b)
{
return Point(a.x-b.x,a.y-b.y);
}
LL cross(Point a,Point b)
{
return a.x*b.y-a.y*b.x;
}
int dcmp(double x)
{
if(fabs(x)<EPS) return 0;
if(x>EPS) return 1;
return -1;
}
struct Info
{
double ang;
int id;
}info[MAXN];
bool cmp(Info a,Info b)
{
return a.ang<b.ang;
}
bool onLeft(Point A,Point B,Point x)
{
if(x==A||x==B) return true;
return cross(B-A,x-A)>=0;
}
int main()
{
freopen("B.in","r",stdin);
freopen("B.out","w",stdout);
int n,q;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%I64d%I64d",&polygon[i].x,&polygon[i].y);
info[i].ang=atan2(polygon[i].y,polygon[i].x);
info[i].id=i;
}
sort(info+1,info+n+1,cmp);
int cnt=0;
LL lastx,lasty;
bool inPoly=false;
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
LL x,y;
scanf("%I64d%I64d",&x,&y);
if(i!=1)
{
if(inPoly)
{
x=lastx+x;
y=lasty+y;
}
else
{
x=lastx-x;
y=lasty-y;
}
}
lastx=x,lasty=y;
if(x==0&&y==0)
{
inPoly=true;
cnt++;
continue;
}
double ang=atan2(y,x);
if(dcmp(ang-info[1].ang)<=0||dcmp(ang-info[n].ang)>=0)
{
if(onLeft(polygon[info[n].id],polygon[info[1].id],Point(x,y)))
{
inPoly=true;
cnt++;
}
else inPoly=false;
continue;
}
int lowerBound=1,upperBound=n-1,ans=0;
while(lowerBound<=upperBound)
{
int mid=(lowerBound+upperBound)>>1;
if(dcmp(ang-info[mid].ang)>=0)
{
ans=mid;
lowerBound=mid+1;
}
else upperBound=mid-1;
}
if(onLeft(polygon[info[ans].id],polygon[info[ans+1].id],Point(x,y)))
{
inPoly=true;
cnt++;
}
else inPoly=false;
}
printf("%d\n",cnt);
return 0;
}