[置顶] Codeforces 70D 动态凸包 (极角排序 or 水平序)
题目链接:http://codeforces.com/problemset/problem/70/D
本题关键:在log(n)的复杂度内判断点在凸包 或 把点插入凸包
判断:平衡树log(n)内选出点所属于的区域
插入:平衡树log(n)内选出点所属于的区域, 与做一般凸包的时候类似,分别以该点向左右两边进行维护,
一直删除不满足凸包的点,直到所有点满足凸包为止。
水平序:
可以用2个平衡树分别维护上下2个半凸包,具体实现时可以把其中一个半凸包按y轴对称以后,那么2个半凸包的维护就是同一种方法,写2个函数就ok了。
具体平衡树可以用set或map,用STL以后边界处理有点烦,需要注意。
水平序的凸包有一个特点(如按x排序):对于上下凸包(分开来看),x相同的点只有一个。所以用set维护比较麻烦,用map维护相对容易一点。
极角序:
之前给你的3个点一定是插入的,可以选它们的中心点o作为之后的凸包中心,按o进行极角排序。
之后的做法就跟 “本题关键” 的做法一致。
以下给出3份不同的代码:
set代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef set <pii > ::iterator iter;
#define mp make_pair
#define X first
#define Y second
int q, op, x, y;
set <pii > up, down;
const int inf = 1e9;
ll cross(pii o, pii a, pii b) {
return (ll)(a.X-o.X)*(b.Y-o.Y) - (ll)(a.Y-o.Y)*(b.X-o.X);
}
bool inside(set<pii > &st, int x, int y) {
if(!st.size()) return 0;
if(x < st.begin()->X || x > st.rbegin()->X) return 0;
iter c = st.lower_bound(mp(x, -inf));//如果有相同x的点,比较y值即可
if(c != st.end() && c->X == x)
return y >= c->Y; iter r = st.lower_bound(mp(x, y));
iter l = r; l--;
return cross(*l, mp(x, y), *r) <= 0;
}
void add(set<pii > &st, int x, int y)
{
if(inside(st, x, y)) return;
iter c = st.lower_bound(mp(x, -inf));
if(c != st.end() && c->X == x) //如果有相同x的点必须删除
st.erase(c);
st.insert(mp(x, y));
iter cur = st.lower_bound(mp(x, y)), i, j;
for(i = cur, i--, j = i, j--; i != st.begin() && cur != st.begin(); i = j, j--)
if(cross(*cur, *i,*j) >= 0) st.erase(i);
else break;
for(i = cur, i++, j = i, j++; i != st.end() && j != st.end(); i = j, j++)
if(cross(*cur, *i, *j) <= 0) st.erase(i);
else break;
} int main() {
scanf("%d", &q);
while(q--) {
scanf("%d%d%d", &op, &x, &y);
if(op == 1) {
add(up, x, -y);
add(down, x, y);
}
else if(inside(up, x, -y) && inside(down, x, y))
puts("YES");
else puts("NO");
}
return 0;
}
map代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
#define X first
#define Y second
typedef map<int, int> mii;
typedef map<int, int>::iterator iter;
typedef long long ll;
ll cross(iter o, iter a, iter b) {
return (ll)(a->X-o->X)*(b->Y-o->Y)-(ll)(a->Y-o->Y)*(b->X-o->X);
}
mii up, down;
bool inside(mii &p, int x, int y) {
if(!p.size()) return 0;
if(x < p.begin()->X || x > p.rbegin()->X) return 0;
if(p.count(x)) return y >= p[x];
p[x] = y;
iter cur = p.lower_bound(x), i, j;
i = cur; j = cur; j++; i--;
bool ret = cross(i, cur, j) <= 0;
p.erase(x);
return ret;
}
void add(mii &p, int x, int y) {
if (inside(p, x, y))
return;
p[x] = y;
iter cur = p.lower_bound(x), i, j;
for (i = cur, i--, j = i, j--; i != p.begin() && cur != p.begin();i = j--)
if (cross(cur, i, j) >= 0) p.erase(i);
else break;
for (i = cur, i++, j = i, j++; i != p.end() && j != p.end();i = j++)
if (cross(cur, i, j) <= 0) p.erase(i);
else break;
}
int main() {
int i, j, Q, x, y, op;
scanf("%d", &Q);
while (Q--) {
scanf("%d%d%d", &op, &x, &y);
if (op == 1) {
add(up, x, -y);
add(down, x, y);
} else if (inside(up, x, -y) && inside(down, x, y))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
极角序代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
struct point {
int x, y;
ll d;
double z;
}o, a[5], p;
typedef set <point>:: iterator iter;
bool operator <(const point &a, const point &b) {
return a.z < b.z || (a.z == b.z && a.d < b.d);
} ll cross(point o, point a, point b) {
return (ll)(a.x-o.x)*(b.y-o.y)-(ll)(a.y-o.y)*(b.x-o.x);
}
set <point> st;
iter L(iter x) {
if(x == st.begin()) x = st.end();
x--;
return x;
}
iter R(iter x) {
x++;
if(x == st.end()) x = st.begin();
return x;
} int q, op;
int main() {
scanf("%d", &q);
for(int i = 0; i < 3; i++) {
scanf("%d%d%d", &op, &a[i].x, &a[i].y);
o.x += a[i].x;
o.y += a[i].y;
a[i].x *= 3; a[i].y *= 3;
}
for(int i = 0; i < 3; i++) {
a[i].d = (ll)(a[i].x-o.x)*(a[i].x-o.x)+(ll)(a[i].y-o.y)*(a[i].y-o.y);
a[i].z = atan2(a[i].y-o.y, a[i].x-o.x);
st.insert(a[i]);
}
q -= 3;
while(q--) {
scanf("%d%d%d", &op, &p.x, &p.y);
p.x *= 3; p.y *= 3;
p.z = atan2(p.y-o.y, p.x-o.x);
p.d = (ll)(p.x-o.x)*(p.x-o.x)+(ll)(p.y-o.y)*(p.y-o.y);
iter i = st.lower_bound(p), j;
if(i == st.end()) i = st.begin();
j = L(i);
if(op == 2) {
if(cross(*j,p,*i)<=0) puts("YES");
else puts("NO");
continue;
}
if(cross(*j,p,*i)<=0) continue;
st.insert(p);
iter cur = st.find(p); i = L(cur); j = L(i);
while(cross(*j, *i, *cur) <= 0) {
st.erase(i);
i = j; j = L(j);
} i = R(cur); j = R(i);
while(cross(*j, *i, *cur) >= 0) {
st.erase(i);
i = j; j = R(j);
} }
return 0;
}
[置顶] Codeforces 70D 动态凸包 (极角排序 or 水平序)的更多相关文章
- POJ 2007 Scrambled Polygon [凸包 极角排序]
Scrambled Polygon Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8636 Accepted: 4105 ...
- bzoj 2087: [Poi2010]Sheep【凸包+极角排序+dp】
首先处理处理出来哪些边能连--能把羊分成两个偶数部分的,实现是在凸包上枚举极点,极角排序,枚举凸包上点对判断两边羊的个数的奇偶即可,设可以连边为v[i][j]=1 然后设f[i][j]为从i到j个凸包 ...
- Gym 101986D Making Perimeter of the Convex Hull Shortest(凸包+极角排序)
首先肯定是构造一个完整的凸包包括所有的点,那么要使得刚好有两个点在外面,满足这个条件的只有三种情况. 1.两个在凸包上但是不连续的两个点. 2.两个在凸包上但是连续的两个点. 3.一个在凸包上,还有一 ...
- [置顶] Codeforces Round #198 (Div. 1)(A,B,C,D)
http://codeforces.com/contest/341 赛后做的虚拟比赛,40分钟出了3题,RP爆发. A计数问题 我们可以对每对分析,分别对每对<a, b>(a走到b)进行统 ...
- [置顶] java web 动态服务器
写了一个java web 动态服务器,主要通过内部类来实现,动态类使用了外部类,采用了 classforname 实例化,动态类的构造方法不能带参数, 效果都出来了,分享给有需要的 朋友.判断做的不够 ...
- [置顶] Codeforces Round #197 (Div. 2)(完全)
http://codeforces.com/contest/339/ 这场正是水题大放送,在家晚上限制,赛后做了虚拟比赛 A,B 乱搞水题 C 我是贪心过的,枚举一下第一个拿的,然后选使差值最小的那个 ...
- [置顶] 原创鼠标拖动实现DIV排序
先上效果图: 对比传统的排序,这是一个很不错的尝试,希望对大家有启发. 大家可以参考我的上一篇博文:http://blog.csdn.net/littlebo01/article/details/12 ...
- [置顶] Codeforces Round #190 (Div. 2)(完全)
好久没有写博客了,一直找不到有意义的题可以写,这次也不算多么有意义,只是今天是比较空的一天,趁这个时候写一写. A. B. 有一点贪心,先把每个拿去3的倍数,余下0或1或2,然后三个一起拿. 对于以上 ...
- codeforces 70D Professor's task(动态二维凸包)
题目链接:http://codeforces.com/contest/70/problem/D Once a walrus professor Plato asked his programming ...
随机推荐
- ASP.NET MVC 5 学习教程:生成的代码详解
原文 ASP.NET MVC 5 学习教程:生成的代码详解 起飞网 ASP.NET MVC 5 学习教程目录: 添加控制器 添加视图 修改视图和布局页 控制器传递数据给视图 添加模型 创建连接字符串 ...
- Hadoop实战实例
Hadoop实战实例 Hadoop实战实例 Hadoop 是Google MapReduce的一个Java实现.MapReduce是一种简化的分布式编程模式,让程序自动分布 ...
- BT基础知识简介
1. 蓝牙概述 无线局域网的通信 适用范围:10米到100米(根据发射功率的class不同有所差别,典型的class2为10m,而class1为100m,class3为1m) 应用: 局域网络 ...
- 字符串处理-AC自动机
估计在OJ上刷过题的都会对AC自动机这个名词很感兴趣,同样,记得去年ACM暑期集训的时候,在最后讲到字符串部分,听说了这个算法的名字之后就对于它心向往之,AC正好是Accept的简称,字面意义上的理解 ...
- python-Day4-迭代器-yield异步处理--装饰器--斐波那契--递归--二分算法--二维数组旋转90度--正则表达式
本节大纲 迭代器&生成器 装饰器 基本装饰器 多参数装饰器 递归 算法基础:二分查找.二维数组转换 正则表达式 常用模块学习 作业:计算器开发 实现加减乘除及拓号优先级解析 用户输入 1 - ...
- C语言数据结构-创建链表的四种方法
结点类型: typedef int datatype; typedef struct NODE{ datatype data; struct NODE *next; }Node,*LinkList; ...
- WCF技术剖析之二十四: ServiceDebugBehavior服务行为是如何实现异常的传播的?
原文:WCF技术剖析之二十四: ServiceDebugBehavior服务行为是如何实现异常的传播的? 服务端只有抛出FaultException异常才能被正常地序列化成Fault消息,并实现向客户 ...
- 服务列表 - Sina App Engine
服务列表 - Sina App Engine 短信服务 新浪无线短信服务是由新浪无线提供的综合性短信服务. 使用服务 下载SDK: php 服务首页 方法 新浪无线短信服务是由新浪无线提供的综合性短信 ...
- Tinyfool的2013年总结————在困惑和挣扎中试图前行
Tinyfool的2013年总结----在困惑和挣扎中试图前行 | Tinyfool的Blog Tinyfool的2013年总结----在困惑和挣扎中试图前行
- VMware Player 使用错误集锦
1.执行VMware Player 弹出"开机时出错:内部错误"的提示.虚拟机执行不了,例如以下图: 解决的方法: 以管理员身份执行. 可能如今登录windows的 ...