B. Polygons
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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).

Examples
input
Copy
6
-2 1
0 3
3 3
4 1
3 -2
2 -2
4
0 1
2 2
3 1
1 0
output
YES
input
Copy
5
1 2
4 2
3 -3
-2 -2
-2 1
4
0 1
1 2
4 1
2 -1
output
NO
input
Copy
5
-1 2
2 3
4 1
3 -2
0 -3
5
1 0
1 1
3 1
5 -1
2 -1
output
NO

题意:给一个凸多边形A,一个多边形B.问B是不是严格包含在A中

思路:A/B所有点算凸包,如果B有点在凸包上,则不行. 记住要特判最后一条边(求凸包不会考虑你最后一条是不是包含共线),用叉积判断

#include<stdio.h>
#include<string.h>
#include<iostream>
#include <math.h>
#include<algorithm>
#define PI acos(-1.0)
using namespace std;
typedef long long ll;

const int MAX_N=2e5+5;
const int MOD=1e9+7;
const int INF=0x3f3f3f3f;


struct node{
    ll x,y;
    int flag;
};
node stackk[MAX_N];
node vex[MAX_N];
int sx,sy,n,m;
bool cmp1(node a,node b){
    if(a.y==b.y)
        return a.x<b.x;
    else
        return a.y<b.y;
}
ll cross(node a,node b,node c){
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
double dis(node a,node b){
    return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

bool cmp2(node a,node b)
{
    if(cross(vex[0],a,b)==0)
    return dis(vex[0],a)<dis(vex[0],b);
    return cross(vex[0],a,b)>0;
}

node tb[MAX_N];

int graham(){
        sort(vex,vex+n+m,cmp1);
        stackk[0]=vex[0];
        sx=stackk[0].x;
        sy=stackk[0].y;
        sort(vex+1,vex+n+m,cmp2);
        stackk[1]=vex[1];
        int top=1;
        for(int i=2;i<n+m;i++){
            while(top>=1 && cross(stackk[top-1],stackk[top],vex[i])<0)
                top--;
            stackk[++top]=vex[i];
        }
        return top;
}

int main(void){
        cin >> n ;
        memset(stackk,0,sizeof stackk);
        memset(vex,0,sizeof vex);
        for(int i=0;i<n;i++)    scanf("%lld%lld",&vex[i].x,&vex[i].y);
        cin >> m;
        for(int i=n;i<=n+m-1;i++)    scanf("%lld%lld",&vex[i].x,&vex[i].y),tb[i-n]=vex[i],vex[i].flag=1;
        int top=graham();
        for(int i=0;i<=top;i++){
                if(stackk[i].flag){
                    cout <<"NO"<<endl ;
                    return 0;
                }
            }
        for(int i=0;i<m;i++){
            if(!cross(tb[i],stackk[0],stackk[top])){
                cout <<"NO"<<endl;
                return 0;
            }
        }
        cout <<"YES"<<endl;
    return 0;
}