首页 > Web开发 > 详细

BZOJ5317[JSOI2018]部落战争(计算几何、闵可夫斯基和)

时间:2019-02-16 22:38:51      阅读:228      评论:0      收藏:0      [点我收藏+]

题目链接

https://lydsy.com/JudgeOnline/problem.php?id=5318

前置知识

闵可夫斯基和:https://www.cnblogs.com/Creed-qwq/p/10317535.html

解析

不难发现部落的领地就是凸包
题目即是询问两个凸包经过平移是否有交集
每次都进行平移不现实,就考虑能不能求出按平面中哪些向量平移会有交集
设两个凸包分别是\(A,B\),即求是否存在向量\(\vec w\)使得\(a = b + {\vec w}, a \in A, b \in B\),一步转化的\({\vec w} = a - b = a + (-b)\)
然后我们知道闵可夫斯基和的形式是\({C = \{ \vec c} | {\vec c} = {\vec a} + {\vec b}, a \in A, b \in B \}\)
于是就可以把凸包上的每个点看作从\((0, 0)\)出发的向量,求出\(A\)\(-B\)的闵可夫斯基和\(C = \{ {\vec c} | {\vec c} = {\vec a} - {\vec b}, {\vec a} \in A, {\vec b} \in B \}\),对每个询问查询输入向量是否在\(C\)中即可
P.S.markdown和公式总算没凉了。。不用截图了。。。

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#define MAXN 100005

typedef long long LL;
struct Point {
    LL x, y;
    Point (LL _x = 0, LL _y = 0):x(_x), y(_y) {};
    LL len2() const { return x * x + y * y; }
    friend LL cross(const Point &p1, const Point &p2) { return p1.x * p2.y - p1.y * p2.x; }
    bool operator <(const Point &p) const { return (cross(*this, p) > 0 || (cross(*this, p) == 0 && len2() < p.len2())); }
    Point operator -(const Point &p) const { return Point(x - p.x, y - p.y); }
    Point operator +(const Point &p) const { return Point(x + p.x, y + p.y); }
} ans[MAXN], conv1[MAXN], conv2[MAXN], qry;
int N, M, Q, tot;

char gc();
LL read();
void print(LL);
void calc_convex(Point *, int, int &);//求凸包
void Minkowski(Point *, int, Point *, int, Point *, int &);//求闵可夫斯基和
bool cmp(const Point &, const Point &);
int check();
int main() {
    //freopen("war.in", "r", stdin);
    //freopen("A.out", "w", stdout);
    N = read(), M = read(), Q = read();
    for (int i = 0; i < N; ++i)
        conv1[i].x = read(), conv1[i].y = read();
    for (int i = 0; i < M; ++i)
        conv2[i].x = -read(), conv2[i].y = -read();
    Minkowski(conv1, N, conv2, M, ans, tot);
    while (Q--) {
        qry.x = read(), qry.y = read();
        print(check());
        putchar('\n');
    }
}
inline char gc() {
    static char buf[1000000], *p1, *p2;
    if (p1 == p2) p1 = (p2 = buf) + fread(buf, 1, 1000000, stdin);
    return p1 == p2 ? EOF : *p2++;
}
inline LL read() {
    LL res = 0, op;
    char ch = gc();
    while (ch != '-' && (ch < '0' || ch > '9')) ch = gc();
    op = (ch == '-' ? ch = gc(), -1 : 1);
    while (ch >= '0' && ch <= '9')
        res = (res << 1) + (res << 3) + ch - '0', ch = gc();
    return res * op;
}
inline void print(LL x) {
    static int buf[30];
    if (!x) putchar('0');
    else {
        if (x < 0) x = -x, putchar('-');
        while (x) buf[++buf[0]] = x % 10, x /= 10;
        while (buf[0]) putchar('0' + buf[buf[0]--]);
    }
}
void calc_convex(Point *conv, int n, int &sz) {
    std::sort(conv, conv + n, cmp);
    sz = 0;
    Point base = conv[0];
    for (int i = 0; i < n; ++i)
        conv[i] = conv[i] - base;
    std::sort(conv + 1, conv + n);
    for (int i = 0; i < n; ++i) {
        while (sz > 1 && cross(conv[sz - 1] - conv[sz - 2], conv[i] - conv[sz - 1]) <= 0) --sz;
        conv[sz++] = conv[i];
    }
    for (int i = 0; i < sz; ++i)
        conv[i] = conv[i] + base;
    conv[sz++] = conv[0];
}
void Minkowski(Point *c1, int sz1, Point *c2, int sz2, Point *res, int &sz) {
    calc_convex(c1, sz1, sz1);
    calc_convex(c2, sz2, sz2);
    for (int i = sz1 - 1; i; --i)
        c1[i] = c1[i] - c1[i - 1];
    for (int i = sz2 - 1; i; --i)
        c2[i] = c2[i] - c2[i - 1];
    int p1 = 1, p2 = 1;
    sz = 0;
    res[sz++] = c1[0] + c2[0];
    while (p1 < sz1 || p2 < sz2)
        if (p1 == sz1) res[sz] = res[sz - 1] + c2[p2++], ++sz;
        else if (p2 == sz2) res[sz] = res[sz - 1] + c1[p1++], ++sz;
        else if (cross(c1[p1], c2[p2]) >= 0) res[sz] = res[sz - 1] + c1[p1++], ++sz;
        else res[sz] = res[sz - 1] + c2[p2++], ++sz;
    calc_convex(ans, tot, tot);
}
int check() {
    int l = 1, r = tot - 1;
    if (cross(qry - ans[0], ans[l] - ans[0]) > 0 || cross(qry - ans[0], ans[r] - ans[0]) < 0) return 0;
    while (l + 1 < r) {
        int mid = (l + r) >> 1;
        if (cross(qry - ans[0], ans[mid] - ans[0]) == 0)
            return (qry - ans[0]).len2() <= (ans[mid] - ans[0]).len2();
        if (cross(qry - ans[0], ans[mid] - ans[0]) < 0) l = mid;
        else r = mid;
    }
    return cross(qry - ans[l], ans[r] - ans[l]) <= 0;
}
bool cmp(const Point &a, const Point &b) {
    return a.x == b.x ? a.y < b.y : a.x < b.x;
}
//Rhein_E

BZOJ5317[JSOI2018]部落战争(计算几何、闵可夫斯基和)

原文:https://www.cnblogs.com/Rhein-E/p/10389457.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!