题解 CF70D 【Professor's task】

· · 题解

这是一道动态凸包的模板题。

听罢昨天徐明宽的计算几何题,分享一下以坐标排序的凸包建立方法。个人认为相对于极角排序,坐标排序能够减小精度误差,尤其是这样一道坐标都为整数的题。

静态凸包的构建方法是:

  1. 将各点坐标按 x 从小到大排序
  2. 分别维护上凸壳和下凸壳。注意到如果之前有两点 A 和 B ,当插入新节点 C 时,以上凸壳为例,如果 \overrightarrow{AB} \times \overrightarrow{AC} \ge 0 时,这说明 B 在线段 AC 的下方,就可以将 B 删除。我们可以看出此时凸包是一个类似单调栈的结构。

排序过程是算法的瓶颈,效率 \Theta(n \log n)。而后面的构建是 \Theta(n) 的。

从上面的启发我们可以找到一种动态维护凸包的方法。

用 \texttt{std::map} 维护上下凸壳, x 为第一关键字, y 为第二关键字。在插入一个新的点到凸壳上时,检查它周围的点是否可以被删掉,用与静态凸包类似的方法判断。

虽然插入一个点时可能删除若干个点,但注意到每个点只有在被删除时才会多贡献这 \mathcal O(\log n) 的时间,所以本算法支持凸包的单次插入有摊还意义上的时间复杂度 \mathcal O(\log n) 。

题外话:注意到这是摊还意义上的,所以单次操作的实际消耗时间可能远远不止 \mathcal O(\log n) ,简单的构造方法:放一个点在 (+\infty, +\infty) ,这样就会把所有点除了最左点都顶掉,单次就变成了 \Theta(n) 的。(想一想,为什么不是 n\log n ?)所以这个方法不能直接转化为可持久化凸包。

#include <cstdio>
#include <cmath>

#include <algorithm>
#include <map>

typedef std::map<int, int> map;
typedef map::iterator iterator;
typedef int num;
typedef long long ll;

map top, down;

ll det(int x1, int y1, int x2, int y2);
bool check_top(int x, int y);
bool check_down(int x, int y);
bool remove_top(iterator it);
bool remove_down(iterator it);
void insert_top(int x, int y);
void insert_down(int x, int y);

int main() {
    int q, op, x, y;
    scanf("%d", &q);
    while (q--) {
        scanf("%d%d%d", &op, &x, &y);
        if (op == 1) {
            insert_top(x, y);
            insert_down(x, y);
        } else
            puts((check_top(x, y) && check_down(x, y)) ? "YES" : "NO");
    }
    return 0;
}

void insert_top(int x, int y) {
    if (check_top(x, y))
        return;
    top[x] = y;
    iterator it = top.find(x);
    iterator jt = it;
    if (it != top.begin()) {
        --jt;
        while (remove_top(jt++))
            --jt;
    }
    if (++jt != top.end())
        while (remove_top(jt--))
            ++jt;
}

ll det(int x1, int y1, int x2, int y2) {
    return (ll)x1 * y2 - (ll)x2 * y1;
}

void insert_down(int x, int y) {
    if (check_down(x, y))
        return;
    down[x] = y;
    iterator it = down.find(x);
    iterator jt = it;
    if (it != down.begin()) {
        --jt;
        while (remove_down(jt++))
            --jt;
    }
    if (++jt != down.end())
        while (remove_down(jt--))
            ++jt;
}

bool remove_top(iterator it) {
    if (it == top.begin())
        return false;
    iterator jt = it, kt = it;
    --jt;
    ++kt;
    if (kt == top.end())
        return false;
    if (det(it->first - jt->first, it->second - jt->second, kt->first - jt->first, kt->second - jt->second) >= 0) {
        top.erase(it);
        return true;
    }
    return false;
}

bool remove_down(iterator it) {
    if (it == down.begin())
        return false;
    iterator jt = it, kt = it;
    --jt;
    ++kt;
    if (kt == down.end())
        return false;
    if (det(it->first - jt->first, it->second - jt->second, kt->first - jt->first, kt->second - jt->second) <= 0) {
        down.erase(it);
        return true;
    }
    return false;
}

bool check_top(int x, int y) {
    iterator it = top.lower_bound(x);
    if (it == top.end())
        return false;
    if (it->first == x)
        return y <= it->second;
    if (it == top.begin())
        return false;
    iterator jt = it;
    --jt;
    return det(it->first - jt->first, it->second - jt->second, x - jt->first, y - jt->second) <= 0;
}

bool check_down(int x, int y) {
    iterator it = down.lower_bound(x);
    if (it == down.end())
        return false;
    if (it->first == x)
        return y >= it->second;
    if (it == down.begin())
        return false;
    iterator jt = it;
    --jt;
    return det(it->first - jt->first, it->second - jt->second, x - jt->first, y - jt->second) >= 0;
}