Description
方师傅来到了一个二维平面。他站在原点上,觉得这里风景不错,就建了一个房子。这个房子是n个点的凸多边形
,原点一定严格在凸多边形内部。有m个人也到了这个二维平面。现在你得到了m个人的坐标,你要判断这m个人中
有多少人在房子内部。点在凸多边形边上或者内部都认为在房子里面。
Input
第一行一个数n,接下来n行,每行两个整数x,y。输入按照逆时针顺序输入一个凸包。
接下来一个数m,最后有m行,第一行两个整数 x,y,表示第一个人的坐标。
对于第i个询问(i>=2) ,输入两个数dx,dy。
如果上一个人在房子内部,x[i]=x[i-1]+dx,y[i]=y[i-1]+dy。否则x[i]=x[i-1]-dx,y[i]=y[i-1]-dy。
n <= 100000, m <= 200000,输入保证所有人的坐标,房屋的坐标都在[-1e9,1e9]之内。
Output
输出一个数,在房子内部人的个数。
Sample Input
4
-2 -2
2 -2
2 2
-2 2
3
5 5
4 4
0 3
Sample Output
1
题解
O(logn)判断一个点是否在凸包内的方法:从第一个点向其他点连线,把这个凸包分成n−2个三角形,然后二分,用叉积判断当前点是否在三角形内。总时间复杂度O(mlogn)。
Update9.8
其实我的代码(方法)是有问题的。。
有一些东西没有判好。。
题目数据水就过了
未更正
CODE:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int N=200005;
int n,m;
struct node{double x,y;}p1[N],p2[N];
int ans=0,now=1;
double mul (node a,node b,node c)
{
double x1=a.x-c.x,y1=a.y-c.y;
double x2=b.x-c.x,y2=b.y-c.y;
return x1*y2-x2*y1;
}
int main()
{
scanf("%d",&n);
for (int u=1;u<=n;u++) scanf("%lf%lf",&p1[u].x,&p1[u].y);
scanf("%d",&m);
p2[0].x=p2[0].y=0.0;
for (int u=1;u<=m;u++)
{
scanf("%lf%lf",&p2[u].x,&p2[u].y);
p2[u].x=p2[u-1].x+now*p2[u].x;
p2[u].y=p2[u-1].y+now*p2[u].y;
if (mul(p1[1],p1[2],p2[u])<0||mul(p1[1],p1[n],p2[u])>0) {now=-1;continue;}
int l=2,r=n;//我们要分的三角形
while (l<=r)
{
int mid=(l+r)>>1;
if (mul(p1[1],p1[mid],p2[u])<0)//逆时针
r=mid-1;
else l=mid+1;
}
if (mul(p1[l],p1[l-1],p2[u])<0) now=1,ans++;
else now=-1;
}
printf("%d\n",ans);
return 0;
}