地址: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. hdu 1115:Lifting the Stone(计算几何,求多边形重心。 过年好!)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  2. MathType中输入不了汉字如何处理

    MathType作为一款常见的数学公式编辑器在编辑数学公式时,不仅可以输入英文字符,对中文也有很好的兼容性.但是有些用户在使用MathType编辑公式时,发现一些汉字是输入不进去的,这个时候我们就需要 ...

  3. 【黑金原创教程】【TimeQuest】【第三章】TimeQuest 扫盲文

    声明:本文为黑金动力社区(http://www.heijin.org)原创教程,如需转载请注明出处,谢谢! 黑金动力社区2013年原创教程连载计划: http://www.cnblogs.com/al ...

  4. jenkins multijob 插件使用

    如果你想要停止对下游/上游工作链定义的混乱 当您想要添加具有层次结构的任务时,按顺序执行或并行执行 安装multijob插件可以让jenkins任务按照分组.顺序执行 jenkins版本:2.80 1 ...

  5. 160407、java实现多线程同步

    多线程就不说了,很好理解,同步就要说一下了.同步,指两个或两个以上随时间变化的量在变化过程中保持一定的相对关系.所以同步的关键是多个线程对象竞争同一个共享资源. 同步分为外同步和内同步.外同步就是在外 ...

  6. Hadoop伪分布安装详解(二)

    目录: 1.修改主机名和用户名 2.配置静态IP地址 3.配置SSH无密码连接 4.安装JDK1.7 5.配置Hadoop 6.安装Mysql 7.安装Hive 8.安装Hbase 9.安装Sqoop ...

  7. oracle导入导出 dmp文件

    oracle导入导出 dmp文件: 打开cmd窗口,在cmd窗口下,按照个人需要输入以下对应的命令: 1.imp 用户名/密码@网络服务名 file=XXX.dmp fromuser=XXX tous ...

  8. 解决IOS7在TableView 被导航栏挡住的BUG!!

    self.edgesForExtendedLayout = UIRectEdgeNone; 就这么简单!

  9. Chrome cookies folder

    w本地存储数据2种形式. http://superuser.com/questions/292952/chrome-cookies-folder-in-windows-7 chrome://setti ...

  10. ora-04021 无法锁表的解决办法

    案例场景: 备库上有一张分区表,在做数据导入出了点问题,需要truncate掉重新导入,在执行truncate table时发生了04021错误. 错误分析: ora-04021的解释是等待锁定对象时 ...