前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BZOJ2564: 集合的面积(闵可夫斯基和 凸包)

BZOJ2564: 集合的面积(闵可夫斯基和 凸包)

作者头像
attack
发布2019-03-04 16:17:23
1.1K0
发布2019-03-04 16:17:23
举报

题意

题目链接

Sol

这个东西的学名应该叫“闵可夫斯基和”。就是合并两个凸包

首先我们先分别求出给出的两个多边形的凸包。合并的时候直接拿个双指针扫一下,每次选最凸的点就行了。

复杂度\(O(nlogn + n)\)

代码语言:javascript
复制
#include<bits/stdc++.h>
#define LL long long 
//#define int long long 
using namespace std;
const int MAXN = 1e6 + 10;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, M;
struct Point {
    LL x, y;
    Point operator - (const Point &rhs) const {
        return {x - rhs.x, y - rhs.y};
    }
    Point operator + (const Point &rhs) const {
        return {x + rhs.x, y + rhs.y};
    }
    LL operator ^ (const Point &rhs) const {
        return x * rhs.y - y * rhs.x;
    }
    bool operator < (const Point &rhs) const {
        return x == rhs.x ? y < rhs.y : x < rhs.x;
    }
    bool operator == (const Point &rhs) const {
        return x == rhs.x && y == rhs.y;
    }
    bool operator != (const Point &rhs) const {
        return x != rhs.x || y != rhs.y;
    }
};
vector<Point> v1, v2;
Point q[MAXN];
int top;
void insert(Point a) {
    while(top > 1 && ((q[top] - q[top - 1]) ^ (a - q[top - 1])) < 0) top--;
    q[++top] = a;
}
void GetConHull(vector<Point> &v) {
    sort(v.begin(), v.end());
    q[++top] = v[0];
    for(int i = 1; i < v.size(); i++) if(v[i] != v[i - 1]) insert(v[i]);
    for(int i = v.size() - 2; i >= 0; i--) if(v[i] != v[i + 1]) insert(v[i]);
    v.clear(); 
    for(int i = 1; i <= top; i++) v.push_back(q[i]); top = 0;
}
void Merge(vector<Point> &a, vector<Point> &b) {
    vector<Point> c;
    q[++top] = a[0] + b[0]; 
    int i = 0, j = 0;
    while(i + 1 < a.size() && j + 1< b.size()) {
        Point n1 = (a[i] + b[j + 1]) - q[top], n2 = (a[i + 1] + b[j]) - q[top];
        if((n1 ^ n2) < 0) 
            q[++top] = a[i + 1] + b[j], i++;
        else 
            q[++top] = a[i] + b[j + 1], j++; 
    }
    for(; i < a.size(); i++) q[++top] = a[i] + b[b.size() - 1];
    for(; j < b.size(); j++) q[++top] = b[j] + a[a.size() - 1];
    for(int i = 1; i <= top; i++) c.push_back(q[i]);
    LL ans = 0;
    //for(auto &g : c) printf("%d %d\n", g.x, g.y);
    for(int i = 1; i < c.size() - 1; i++) 
        ans += (c[i] - c[0]) ^ (c[i + 1] - c[0]);
    cout << ans;
}
signed main() {
    N = read(); M = read();
    for(int i = 1; i <= N; i++) {
        int x = read(), y = read();
        v1.push_back({x, y});
    }
    for(int i = 1; i <= M; i++) {
        int x = read(), y = read();
        v2.push_back({x, y});
    }
    GetConHull(v1);
    GetConHull(v2);
    Merge(v1, v2);
    return 0;
}
/*
4 5
0 0 2 1 0 1 2 0
0 0 1 0 0 2 1 2 0 1
*/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-02-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档