codeforces 70 D. Professor's task 动态凸包
地址:http://codeforces.com/problemset/problem/70/D
题目:
1 second
256 megabytes
standard input
standard output
Once a walrus professor Plato asked his programming students to perform the following practical task.
The students had to implement such a data structure that would support a convex hull on some set of points S. The input to the program had q queries of two types:
1. Add a point with coordinates (x, y) into the set S. Note that in this case the convex hull of S could have changed, and could have remained the same.
2. Say whether a point with coordinates (x, y) belongs to an area limited by the convex hull, including the border.
All the students coped with the task. What about you?
The first line contains an integer q (4 ≤ q ≤ 105).
Then follow q lines in the following way: "t x y", where t is the query type (1 or 2), and (x, y) are the coordinates of the point ( - 106 ≤ x, y ≤ 106, x and y are integers).
There is at least one query of type 2.
It is guaranteed that the three queries of the first type follow first and the points given in the queries form a non-degenerative triangle. Also all the points added in S are distinct.
For each query of the second type print one string containing "YES", if the point lies inside the convex hull or on its border. Otherwise, print "NO".
8
1 0 0
1 2 0
1 2 2
2 1 0
1 0 2
2 1 1
2 2 1
2 20 -1
YES
YES
YES
NO
思路:动态凸包。
摘自http://blog.csdn.net/auto_ac/article/details/10664641
本题关键:在log(n)的复杂度内判断点在凸包 或 把点插入凸包
判断:平衡树log(n)内选出点所属于的区域
插入:平衡树log(n)内选出点所属于的区域, 与做一般凸包的时候类似,分别以该点向左右两边进行维护,
一直删除不满足凸包的点,直到所有点满足凸包为止。
水平序:
可以用2个平衡树分别维护上下2个半凸包,具体实现时可以把其中一个半凸包按y轴对称以后,那么2个半凸包的维护就是同一种方法,写2个函数就ok了。
具体平衡树可以用set或map,用STL以后边界处理有点烦,需要注意。
水平序的凸包有一个特点(如按x排序):对于上下凸包(分开来看),x相同的点只有一个。所以用set维护比较麻烦,用map维护相对容易一点。
极角序:
之前给你的3个点一定是插入的,可以选它们的中心点o作为之后的凸包中心,按o进行极角排序。
之后的做法就跟 “本题关键” 的做法一致。
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e5+;
const int mod=1e9+; /*******判断ta与tb的大小关系*******/
int sgn(double ta,double tb);
/** 基本几何结构 **/
struct Point
{
double x,y,a,b;
Point(double a=, double b=){x=a,y=b;}
bool operator<(const Point &ta)const
{
return sgn(a,ta.a)<||(sgn(a,ta.a)==&&sgn(b,ta.b)<);
}
};
typedef set<Point>::iterator iter;
double cross(Point ta,Point tb,Point tc);
double getdis2(const Point &ta,const Point &tb);
iter L(iter it);
iter R(iter it);
int q,n,ch;
Point o,tmp,pt[];
set<Point>st; int main(void)
{
cin>>q;
o.x=o.y=;
for(int i=;i<=;i++)
scanf("%d%lf%lf",&ch,&pt[i].x,&pt[i].y),o.x+=pt[i].x,o.y+=pt[i].y;
o.x/=3.0,o.y/=3.0;
for(int i=;i<=;i++)
{
pt[i].a=atan2(pt[i].y-o.y,pt[i].x-o.x);
pt[i].b=getdis2(o,pt[i]);
st.insert(pt[i]);
}
set<Point>::iterator ia,ib,ic;
for(int i=;i<=q;i++)
{ scanf("%d%lf%lf",&ch,&tmp.x,&tmp.y);
tmp.a=atan2(tmp.y-o.y,tmp.x-o.x);
tmp.b=getdis2(tmp,o);
ia=st.lower_bound(tmp);
if(ia==st.end())ia=st.begin();
ib=L(ia);
if(ch==)
{
if(sgn(cross(*ib,tmp,*ia),)<=)
continue;
st.insert(tmp);
ic=st.find(tmp);
ia=L(ic),ib=L(ia);
while(sgn(cross(*ib,*ia,*ic),)<=)
{
st.erase(ia);
ia=ib,ib=L(ia);
}
ia=R(ic),ib=R(ia);
while(sgn(cross(*ib,*ia,*ic),)>=)
{
st.erase(ia);
ia=ib,ib=R(ia);
}
}
else
{
if(sgn(cross(*ib,tmp,*ia),)<=)
printf("YES\n");
else
printf("NO\n");
}
}
return ;
} int sgn(double ta,double tb)
{
if(fabs(ta-tb)<eps)return ;
if(ta<tb) return -;
return ;
}
double cross(Point ta,Point tb,Point tc)
{
return (tb.x-ta.x)*(tc.y-ta.y)-(tb.y-ta.y)*(tc.x-ta.x);
}
double getdis2(const Point &ta,const Point &tb)
{
return (ta.x-tb.x)*(ta.x-tb.x)+(ta.y-tb.y)*(ta.y-tb.y);
}
iter L(iter it)
{
if(it==st.begin())it=st.end();
return --it;
}
iter R(iter it)
{
if(++it==st.end()) it=st.begin();
return it;
}
codeforces 70 D. Professor's task 动态凸包的更多相关文章
- codeforces 70D Professor's task(动态二维凸包)
题目链接:http://codeforces.com/contest/70/problem/D Once a walrus professor Plato asked his programming ...
- CF70D Professor's task(动态凸包)
题面 两种操作: 1 往点集S中添加一个点(x,y); 2 询问(x,y)是否在点集S的凸包中. 数据保证至少有一个2操作, 保证刚开始会给出三个1操作, 且这三个操作中的点不共线. 题解 动态凸包板 ...
- Codeforces Beta Round #64D - Professor's task
题意:两种操作1.加点2.查询点是否在之前给定点的凸包内 题解:set维护动态凸包,分别维护上下凸壳,对y取反就行,判断点是否在凸壳内,把点加进去看要不要删除就好了 //#pragma GCC opt ...
- 【BZOJ 2300】 2300: [HAOI2011]防线修建 (动态凸包+set)
2300: [HAOI2011]防线修建 Description 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可是A国上 ...
- BZOJ 2300: [HAOI2011]防线修建( 动态凸包 )
离线然后倒着做就变成了支持加点的动态凸包...用平衡树维护上凸壳...时间复杂度O(NlogN) --------------------------------------------------- ...
- [NOI2007]货币兑换Cash(DP+动态凸包)
第一次打动态凸包维护dp,感觉学到了超级多的东西. 首先,set是如此的好用!!!可以通过控制一个flag来实现两种查询,维护凸包和查找斜率k 不过就是重载运算符和一些细节方面有些恶心,90行解决 后 ...
- BZOJ [HAOI2011]防线修建(动态凸包)
听说有一种很高端的东西叫动态凸包维护dp就像学一下,不过介于本人还不会动态凸包就去学了下,还是挺神奇的说,维护上下凸包的写法虽然打得有点多不过也只是维护复制黏贴的事情而已罢了. 先说下动态凸包怎么写吧 ...
- 【BZOJ 1701】Cow School(斜率优化/动态凸包/分治优化)
原题题解和数据下载 Usaco2007 Jan 题意 小牛参加了n个测试,第i个测试满分是\(p_i\),它的得分是\(t_i\).老师去掉\(t_i/p_i\)最小的d个测试,将剩下的总得分/总满分 ...
- Code Chef TSUM2(动态凸包+点分治)
题面 传送门 题解 真是毒瘤随机化算法居然一分都不给 首先这种树上的题目一般想到的都是点分 我们考虑如何统计经过当前点的路径的贡献,设当前点\(u\)在序列中是第\(c\)个,那么一条路径的贡献就是 ...
随机推荐
- django头像上传详解
使用form组件上传头像 实现的效果如下: 将默认的上传文件框隐藏起来变成了一个默认的头像 而且可以点击上传 处理步骤 avatar = forms.FileField( widget = widge ...
- 解决error: Unable to find vcvarsall.bat【python 2.7/vs2010】
转自:http://blog.csdn.net/secretx/article/details/17472107 去下载安装VS2010(08版貌似也行,不过没必要用旧版,指不定哪个库又无法编译),给 ...
- Docker(1)在CentOS上的安装与卸载
一. Docker的安装 CentOS7 上安装: 1. 卸载旧版本 $ sudo yum remove docker \ docker-client \ docker-client-latest ...
- 教你在Ubuntu上体验Mac风格
导读 老实说,我是个狂热的 Ubuntu 迷,我喜欢 Ubuntu 默认的 Unity 主题样式外观.此外,还有很多关于 Ubuntu 14.04 的漂亮图标主题样式 可用来美化默认的外观.但正如我上 ...
- DocumentFragment 不支持 innerHTML
在需要多次使用 innerHTML 的地方,一般是推荐用 DocumentFragment 来缓存,最后一次性插入 body,从而减少浏览器的渲染,提高性能,不过最近也发现一个 bug: Docume ...
- Ubuntu 中 apache 开启 rewrite 模块
ubuntu14.04中安装好apache2.4之后默认rewrite模块是不开启的,项目public目录下的.htaccess文件就用不了,在浏览器中访问网页总是报500错误,原因就是这个. 执行下 ...
- 在Windows上手动安装php开发环境
安装MySQL 使用官方提供提供的安装包一键安装即可. 打开 mysql,选择Windows,MSI Installer点击下载.附:最新版mysql5.7.18下载地址 点击installer安装, ...
- JavaScript面向对象OOP思想Class系统
JavaScript的Class模块,纯天然无依赖,只有2k大小,快速高效,让我们优雅的面向对象... | |目录 1源码:jClass.js 2源码:jClass.min.js 3构建一个类 4访问 ...
- Struts2的OGNL的用法
1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1 ...
- Python全栈day18(三元运算,列表解析,生成器表达式)
一,什么是生成器 可以理解为一种数据类型,这种数据类型自动实现了迭代器协议(其他数据类型需要调用自己内置的__iter__方法),所以生成器是可迭代对象. 二,生成器分类在python中的表现形式 1 ...