题解 CF70D 【Professor's task】
这是一道动态凸包的模板题。
听罢昨天徐明宽的计算几何题,分享一下以坐标排序的凸包建立方法。个人认为相对于极角排序,坐标排序能够减小精度误差,尤其是这样一道坐标都为整数的题。
静态凸包的构建方法是:
- 将各点坐标按
x 从小到大排序 - 分别维护上凸壳和下凸壳。注意到如果之前有两点
A 和B ,当插入新节点C 时,以上凸壳为例,如果\overrightarrow{AB} \times \overrightarrow{AC} \ge 0 时,这说明B 在线段AC 的下方,就可以将B 删除。我们可以看出此时凸包是一个类似单调栈的结构。
排序过程是算法的瓶颈,效率
从上面的启发我们可以找到一种动态维护凸包的方法。
用
虽然插入一个点时可能删除若干个点,但注意到每个点只有在被删除时才会多贡献这
题外话:注意到这是摊还意义上的,所以单次操作的实际消耗时间可能远远不止
#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;
}