题解:CF166B Polygons

· · 题解

发现多边形 B 在凸多边形 A 内部,等价于两个多边形的点集的并集的凸包就等于 A。但要求 B 的任意一个点不能在多边形 A 的边上,所以我们把凸包算法改一下,使得其允许两条边共线,再比较并集的凸包是否等于 A 即可。

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
pair<double, double> operator - (const pair<double, double> &a, const pair<double, double> &b) {
    return make_pair(a.first - b.first, a.second - b.second);
}
double operator *(const pair<double, double> &a, const pair<double, double> &b) {
    return a.first * b.second - a.second * b.first;
}
double dist(pair<double, double> a, pair<double, double> b) {
    return sqrt((a.first - b.first) * (a.first - b.first) 
        + (a.second - b.second) * (a.second - b.second));
}
int n, m, tp, stk[N];
bool used[N];
struct node {
    pair<double, double> vc;
    int pos;
}p[N];
signed main() {
    ios :: sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i ++) {
        cin >> p[i].vc.first >> p[i].vc.second;
        p[i].pos = i;
    }
    cin >> m;
    for(int i = n + 1; i <= n + m; i ++) {
        cin >> p[i].vc.first >> p[i].vc.second;
        p[i].pos = i;
    }
    sort(p + 1, p + 1 + n + m, [](node n1, node n2) {
        return n1.vc < n2.vc;
    });
    stk[++ tp] = 1;
    for(int i = 2; i <= n + m; i ++) {
        while(tp >= 2 && (p[stk[tp]].vc - p[stk[tp - 1]].vc) * (p[i].vc - p[stk[tp]].vc) < 0)
            used[p[stk[tp]].pos] = 0, tp --;
        stk[++ tp] = i;
        used[p[i].pos] = 1;
    }
    int tmp = tp;
    for(int i = n + m - 1; i; i --) {
        if(!used[p[i].pos]) {
            while(tp > tmp && (p[stk[tp]].vc - p[stk[tp - 1]].vc) * (p[i].vc - p[stk[tp]].vc) < 0)
                used[p[stk[tp]].pos] = 0, tp --;
            stk[++ tp] = i;
            used[p[i].pos] = 1;
        }
    }
    for(int i = 1; i <= n + m; i ++) 
        if((i <= n && !used[i]) || (i > n && used[i])) {
            cout << "NO";
            return 0;
        }
    cout << "YES";
    return 0;
}