地址:http://codeforces.com/problemset/problem/70/D

题目:

D. Professor's task
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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?

Input

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.

Output

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".

Examples
input
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
output
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 动态凸包的更多相关文章

  1. codeforces 70D Professor's task(动态二维凸包)

    题目链接:http://codeforces.com/contest/70/problem/D Once a walrus professor Plato asked his programming ...

  2. CF70D Professor's task(动态凸包)

    题面 两种操作: 1 往点集S中添加一个点(x,y); 2 询问(x,y)是否在点集S的凸包中. 数据保证至少有一个2操作, 保证刚开始会给出三个1操作, 且这三个操作中的点不共线. 题解 动态凸包板 ...

  3. Codeforces Beta Round #64D - Professor's task

    题意:两种操作1.加点2.查询点是否在之前给定点的凸包内 题解:set维护动态凸包,分别维护上下凸壳,对y取反就行,判断点是否在凸壳内,把点加进去看要不要删除就好了 //#pragma GCC opt ...

  4. 【BZOJ 2300】 2300: [HAOI2011]防线修建 (动态凸包+set)

    2300: [HAOI2011]防线修建 Description 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可是A国上 ...

  5. BZOJ 2300: [HAOI2011]防线修建( 动态凸包 )

    离线然后倒着做就变成了支持加点的动态凸包...用平衡树维护上凸壳...时间复杂度O(NlogN) --------------------------------------------------- ...

  6. [NOI2007]货币兑换Cash(DP+动态凸包)

    第一次打动态凸包维护dp,感觉学到了超级多的东西. 首先,set是如此的好用!!!可以通过控制一个flag来实现两种查询,维护凸包和查找斜率k 不过就是重载运算符和一些细节方面有些恶心,90行解决 后 ...

  7. BZOJ [HAOI2011]防线修建(动态凸包)

    听说有一种很高端的东西叫动态凸包维护dp就像学一下,不过介于本人还不会动态凸包就去学了下,还是挺神奇的说,维护上下凸包的写法虽然打得有点多不过也只是维护复制黏贴的事情而已罢了. 先说下动态凸包怎么写吧 ...

  8. 【BZOJ 1701】Cow School(斜率优化/动态凸包/分治优化)

    原题题解和数据下载 Usaco2007 Jan 题意 小牛参加了n个测试,第i个测试满分是\(p_i\),它的得分是\(t_i\).老师去掉\(t_i/p_i\)最小的d个测试,将剩下的总得分/总满分 ...

  9. Code Chef TSUM2(动态凸包+点分治)

    题面 传送门 题解 真是毒瘤随机化算法居然一分都不给 首先这种树上的题目一般想到的都是点分 我们考虑如何统计经过当前点的路径的贡献,设当前点\(u\)在序列中是第\(c\)个,那么一条路径的贡献就是 ...

随机推荐

  1. python中paramiko的安装

    windows下安装并使用Python的SSH模块(paramiko+pycrypto+ecdsa) 2014-01-20 14:59 2223人阅读 评论(0) 收藏 举报 python+opens ...

  2. iOS 圆角投影

    self.backgroundColor = [UIColor whiteColor]; self.layer.shadowColor = [UIColor lightGrayColor].CGCol ...

  3. ArcGIS ArcMap 与 ArcServer关于Python的冲突

    一.问题描述 1.ArcMap 是32位,运行的Python也是32位: 2.ArcGIS Server 是64位,运行的Python是64位: 3.这样就导致注册表和环境变量起冲突,即如果Serve ...

  4. shell脚本学习总结06--数学计算

    在bash中可利用let,(())和[]执行基本的操作,高级操作将会使用expr和bc 运算符:+,—,*,/,**(幂) (()) [root@Director ~]# ((c=2**3-9%2)) ...

  5. 在VS2013下如何配置DirectX SDK的开发环境_百度经验

    jpg改rar

  6. egret.Capabilities 在pc和移动端输出值

    egret.log("Device:", egret.Capabilities.os, App.DeviceUtils.IsWeb, App.DeviceUtils.isMobil ...

  7. 第一个MapReduce的例子

    第一个MapReduce的例子 Hadoop Guide的第一个MapReduce的例子是处理气象数据的(数据来源ncdc),终于跑通了.总结一下步骤,安装hadoop不在本文中介绍 1 数据预处理 ...

  8. Hibernate的二级缓存(SessionFaction的外置缓存)-----Helloword

    1. 使用 Hibernate 二级缓存的步骤: 1). 加入二级缓存插件的 jar 包及配置文件: I. 复制 \hibernate-release-4.2.4.Final\lib\optional ...

  9. Struts2---输入验证

    1. Struts2 的验证 1). 验证分为两种: > 声明式验证* 需要解决的问题如下: >> 确定对哪个 Action 或 Model 的那个字段进行验证 >> 使 ...

  10. tcpdump linux抓http请求头

    sudo tcpdump -i eth0 port 80 -s 1024 -l -A