BZOJ5317[JSOI2018]部落戰爭(計算幾何、閔可夫斯基和)
阿新 • • 發佈:2019-02-17
交集 stdin return spa stdout 兩個 putc put 存在
於是就可以把凸包上的每個點看作從\((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和公式總算沒涼了。。不用截圖了。。。
題目鏈接
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]部落戰爭(計算幾何、閔可夫斯基和)