ime limit per test : 2 seconds
memory limit per test : 256 megabytes
You’ve got another geometrical task. You are given two non-degenerate polygons A and B as vertex coordinates. Polygon A is strictly convex. Polygon B is an arbitrary polygon without any self-intersections and self-touches. The vertices of both polygons are given in the clockwise order. For each polygon no three consecutively following vertices are located on the same straight line.
Your task is to check whether polygon B is positioned strictly inside polygon A. It means that any point of polygon B should be strictly inside polygon A. “Strictly” means that the vertex of polygon B cannot lie on the side of the polygon A.
Input
The first line contains the only integer n(3 ≤ n ≤ 105) — the number of vertices of polygon A. Then n lines contain pairs of integers xi, yi(|xi|, |yi| ≤ 109 ) — coordinates of the i-th vertex of polygon A. The vertices are given in the clockwise order.
The next line contains a single integer m(3 ≤ m ≤ 2⋅104) — the number of vertices of polygon B. Then following m lines contain pairs of integers xj, yj(|xj|, |yj| ≤ 109) — the coordinates of the j-th vertex of polygon B. The vertices are given in the clockwise order.
The coordinates of the polygon’s vertices are separated by a single space. It is guaranteed that polygons A and B are non-degenerate, that polygon A is strictly convex, that polygon B has no self-intersections and self-touches and also for each polygon no three consecutively following vertices are located on the same straight line.
Output
Print on the only line the answer to the problem — if polygon B is strictly inside polygon A, print “YES”, otherwise print “NO” (without the quotes).
Input1
6
-2 1
0 3
3 3
4 1
3 -2
2 -2
4
0 1
2 2
3 1
1 0
Output1
YES
Input2
5
1 2
4 2
3 -3
-2 -2
-2 1
4
0 1
1 2
4 1
2 -1
Output2
NO
Input3
5
-1 2
2 3
4 1
3 -2
0 -3
5
1 0
1 1
3 1
5 -1
2 -1
Output3
NO
题意:
给一个n个顶点的凸多边形A和一个m个顶点的凸多边形B,判断B是否在A内
NOTE:
B的顶点不能在A的点或者边上
题解:
先将凸多边形A的所有点按照极角排序,然后对于每一个B的顶点,我们只需要判断其是否在A中就可推知凸多边形B是否在A中。那么对于每一个B的定点
pb[i]
我们只需要二分查找和其极角序相邻的两个A的定点
pa[st−1],pa[st]
,然后判断这两个点和极角序基准点构成的三角形是否能够将
pb[i]
包起来就OK了。
#include<bits/stdc++.h>
#define LiangJiaJun main
#define ll long long
using namespace std;
int n,m;
struct Vex{
ll x,y;
}pa[100004],pb[20004];
ll operator*(Vex A,Vex B){return A.x*B.y-A.y*B.x;}
Vex operator-(Vex A,Vex B){
Vex t;
t.x=A.x-B.x;
t.y=A.y-B.y;
return t;
}
ll dis(Vex A,Vex B){return (A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y);}
inline bool dex(Vex A,Vex B){
ll sv = (A-pa[1])*(B-pa[1]);
if(sv == 0)return dis(A,pa[1])<dis(B,pa[1]);
return sv<0;
}
int For_unc(Vex G){
int l=2,r=n,ans=-1;
while(l<=r){
int mid=(l+r)>>1;
if((G-pa[1])*(pa[mid]-pa[1]) <= 0){
ans=mid;r=mid-1;
}
else l=mid+1;
}
return ans;
}
int LiangJiaJun(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%I64d%I64d",&pa[i].x,&pa[i].y);
scanf("%d",&m);
for(int i=1;i<=m;i++)scanf("%I64d%I64d",&pb[i].x,&pb[i].y);
int t=1;
for(int i=1;i<=n;i++)
if(pa[i].y<pa[t].y||(pa[i].y==pa[i].y&&pa[i].x<pa[t].x))t=i;
swap(pa[1],pa[t]);
sort(pa+2,pa+n+1,dex);
for(int i=1;i<=m;i++){
int st=For_unc(pb[i]);
if(st==2)return puts("NO"),0;
Vex Gr = pa[st],Gl=pa[st-1];
if((pb[i]-Gl)*(Gr-Gl)<=0)return puts("NO"),0;
if(st-1==2){
if((Gl-pa[1])*(pb[i]-pa[1])==0)return puts("NO"),0;
}
if(st==m){
if((Gr-pa[1])*(pb[i]-pa[1])==0)return puts("NO"),0;
}
}
puts("YES");
return 0;
}